Sending Emails With Attachments via PHP

If you ever played around with the native PHP mail function, you will probably know that it’s functionality is very basic. Plain text emails work great, but if you want to do HTML email, you need to figure out what mail headers to send with your message. And if you want to send an attachment you need to base64 encode it yourself.

Good news is that you only need to implement it once. Even better news is, that almost every PHP developer out there already implemented their own email class at one point or another. Therefore, you should not be wasting time on this - just go out there and steal some code.

If you are shopping around for a good PHP email functionality I recommend PHPMailer. Why do I like it? Let me count the ways:

  1. Easy to use
  2. No Setup involved - just drop the class files in your include path
  3. Let’s you sen attachments, html emails and etc without any thinking
  4. Works with PHP4
  5. Widely used (included in projects like Mambo, Moodle, PostNuke, Xoops and etc..
  6. Well documented
  7. Distributed under GPL

How easy to use is it? Let me show you. Here is a simple script that will send an email with an attachment:

<?php
require("class.phpmailer.php");
 
$mail = new PHPMailer();
$mail->From     = "you@example.com";
$mail->FromName = "Your Full Name";
$mail->Host     = "smtp.example.com";
$mail->Mailer   = "smtp";
 
$mail->AddAddress("someone@example.com");
$mail->AddCC("supervisor@example.com");
$mail->AddBCC("secret@example.com");
$mail->Subject = "This is Your Subject";
$mail->Body    = "This is your message";
$mail->AddAttachment("~/files/something.zip", "new_name.zip");  // optional name
 
if(!$mail->Send())
{
     echo "There was an error sending the message";
     exit;
}
 
echo "<p>Message was sent successfully</p>";
?>

How is that for simple? It’s a piece of cake! I have been using it for a while now, and I haven’t had any problems with it. So go and steal it today.

Related Posts:

  • PHP4 Mail Function: Sent Time is 1 Hour Ahead
  • Religious Forwards
  • Winmail.dat Files
  • The emails I get…
  • Windows Task Scheduler: Kill Running Tasks
  • God! Why people don’t mak their emails NSFW!
  • December Summary
  • Email Annoyances
  • Thompson in Contempt
  • Drag and Drop Outlook Emails onto .NET Application

  • 2 Responses to “Sending Emails With Attachments via PHP”

    1. Gravatar Travis McCrea UNITED STATES Says: Reply to this comment

      I have been wanting to do this for a while… now all i need is a way to upload the file and then have the uploaded file emailed.
      Want to hear my idea? I don’t care if someone steals it I said it here first we all know its my idea and if someone steals it they are just a dirty bastard and we all know who the smart guy is.

      Annon. Youtube videos:
      Some videos you want to post but you don’t want to put your name to it or something. You can upload it with the site i make, and it will be emailed off and eventually approved maybe. I am thinking of an approval system though, as to stop like porn and stuff.

      Posted using Mozilla Firefox Mozilla Firefox 2.0.0.6 on Windows Windows XP
    2. Gravatar Luke UNITED STATES Says: Reply to this comment

      Well, file uploads in php are easy. The only problem with this is that movie files tend to be big. Email is just not the best medium for shipping around files few hundred megs in size.

      Btw, what’s wrong with creating a one-use-only Youtube account for the purpose of posting something?

      Posted using Mozilla Firefox Mozilla Firefox 2.0.0.6 on Windows Windows XP

    Leave a Reply

    XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <pre lang=""> <em> <i> <strike> <strong>

    [Quote selected]