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:

From     = "";
$mail->FromName = "Your Full Name";
$mail->Host     = "";
$mail->Mailer   = "smtp";

$mail->Subject = "This is Your Subject";
$mail->Body    = "This is your message";
$mail->AddAttachment("~/files/", "");  // optional name

     echo "There was an error sending the message";

echo "

Message was sent successfully

"; ?>

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.

[tags]php, phpmailer, mail, php email, php email attachments, attachment[/tags]

This entry was posted in programming and tagged . Bookmark the permalink.

3 Responses to Sending Emails With Attachments via PHP

  1. 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.

    Reply  |  Quote
  2. Luke UNITED STATES Mozilla Firefox Windows says:

    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?

    Reply  |  Quote
  3. Reagan SWEDEN Mozilla Firefox Windows says:

    I every time emailed this webpage post page to all my
    associates, as if like to read it next my contacts will too.

    Reply  |  Quote

Leave a Reply

Your email address will not be published. Required fields are marked *