jump to navigation

A Simple Example : Sending Email with Attachment Using Phpmailer November 8, 2011

Posted by Tournas Dimitrios in PHP.

PHPMailer is a PHP class that provides a package of functions to send email . The two primary features are sending HTML Email and e-mails with attachments . PHPMailer supports nearly all possiblities to send email : mail() , Sendmail , qmail & direct to SMTP server (ie sending email via your Google mail-account ) . You can use any feature of SMTP-based e-mail , multiple recepients via : to , CC , BCC , etc. In short: PHPMailer is an efficient way to send e-mail within PHP .

As you may know, it is simple to send mails with the PHP mail() function . So why use PHPMailer ? Isn’t it slower ? Yes that’s true , but PHPMailer makes it easy to send e-mail , makes it possible to attach files , send HTML e-mail , etc . With PHPMailer you can even use your own SMTP server and avoid Sendmail routines used by the mail() function on *nix platforms .

The following code snippet demonstrates how to implement the class into your script or website and how to build an e-mail application . If you just want to send simple e-mail and you have no problems with the mail() function , it’s suggested that you continue to use mail() , instead of the phpmailer class .

Update (20/06/2013) : PHPMailer is now also available as a Composer package . Its source code can also be cloned from GitHub .

Before continuing , please be sure that PHPMailer class is installed correctly . Just download the phpmailer class from code.google.com , unzip the file and upload only the mailer class ( class.phpmailer.php) to your server . You can upload the class in the same directory as the HTML webpage that use mail functionality , or alternatively , upload the class into your lib folder (my lib is outside the publicly available directory ) . If you’re still not sure, you can verify that you’ve installed PHPMailer correctly with this little script :

<?php require("class.phpmailer.php"); $mail = new PHPMailer(); ?>

Save it as a PHP document in the same directory where you’ve saved class.phpmailer.php . If no errors result from running the script , your installation has been done correctly . If you switched error messages and warnings off , then go ahead and set it on ; refer to your PHP manual for more on this topic .

If the previous step was completed , try to send a message with an attachement :

require_once 'class.phpmailer.php';
$mail = new PHPMailer();
// Now you only need to add the necessary stuff

// HTML body

$body = "</pre>
$body .= " Hello Dimitrios
$body .= "<i>Your</i> personal photograph to this message.
$body .= "Sincerely,
$body .= "phpmailer test message ";
$body .= "</div>" ;

// And the absolute required configurations for sending HTML with attachement

$mail->AddAddress("sendemailto@mail.zz", "My-webpage Website");
$mail->Subject = "test for phpmailer-3";
if(!$mail->Send()) {
echo "There was an error sending the message";
echo "Message was sent successfully";


PHPmailer without attachment :

<?php require_once 'class.phpmailer.php'; $mail = new PHPMailer(true);  $to = "sendmailto@gmail.com"; $subject = "this is a test from phpmailer" ; $message = "This message was send with the PHP-mailer library and uses the defauld (mail) "; try {  $mail--->AddAddress($to, 'Example To');
$mail->SetFrom('testexample@example.com', 'Example');
$mail->AddReplyTo('example@example.com', 'Example');
$mail->Subject = $subject;
$mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; // optional - MsgHTML will create an alternate automatically
echo "Message Sent OK\n";
} catch (phpmailerException $e) {
echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
echo $e->getMessage(); //Boring error messages from anything else!


Reference :


1. Reggie Gewant - November 17, 2011

I used to be more than happy to find this web-site.I wanted to thanks to your time for this glorious read!! I undoubtedly enjoying each little bit of it and I’ve you bookmarked to check out new stuff you blog post.

tournasdimitrios1 - November 17, 2011

@Reggie Gewant
I’m glad you like it , thanks for your comments .

2. Sending HTML-email with Attachment Using PHP’s mail() Function « Tournas Dimitrios - November 18, 2011

[…] (wrapper classes) to make our job more pleasant . My previous article demonstrated how the phpMailer class sends attachments with just a few lines of code . PhpMailer can do much more in a  simpler way […]

3. Sending HTML-Mail With Attachement Using PHP’s PEAR::Mail Package « Tournas Dimitrios - January 3, 2012

[…] A Simple Example : Sending Email with Attachment Using Phpmailer […]

4. umi - February 29, 2012

mucho gracias, helped a lot.

5. Jay Jhones - August 2, 2012

Hi, how can I do to send an attachment from my computer? for example, “D:\document.docx” ?

I am trying to do something like:
$mail->AddAttachment(“D:\\document.docx” );

But it don’t works =(

tournasdimitrios1 - August 2, 2012

@Jay Jhones
Simply , you can’t send email from your Windows development box . You should have a mail server installed on your PC (most likely this isn’t the case) .
Read my article “How to send a virtual mail from Wamp” . Pay attention on the word “virtual” , it just emulates a mail server (it wont send mail outside your windows box ) .

Jay Jhones - August 2, 2012

But i am able to send emails with this form, i am just unable send it with attachment =(.

tournasdimitrios1 - August 2, 2012

Hi, how can I do to send an attachment from my computer? for example, “D:\document.docx” ?

From your first post : I made the assumption that you are using a Windows machine (local development box). Describe me your development environment (what server , PHP version …)

6. Rave - August 5, 2012

is it possible to upload new file & send as attachment using phpmailer? thx🙂

tournasdimitrios1 - August 5, 2012

Of course ,the uploading process should be preceded .An if/else statement should ensure that the uploading process is finished , which then will call a function with the phpmailer functionality .

7. Bhawna Gupta - August 27, 2012

I’m new in web developing application…. i need more understanding about SMTP Server.

Can you plz tell me in detail …how to use class.phpmailer.php(means where i put this class).
i’m using wamp2.2.when i was doing some editing in test_smtp_gmail_basic.php which is given in the examples of phpmailer_5.2.1.it worked.but at that time …i run by giving the whole address in the url i.e.http://localhost/PHPMailer_5.2.1/PHPMailer_5.2.1/examples/test_smtp_gmail_basic.php

but when i tried to sending the mail through form and attachment of php file(in which coding of sending the mail is given) is given in action of form.i failed …broswer show HTTP 404 found error.

i’m using wamp with dreamweaver.

tournasdimitrios1 - August 27, 2012

Simply , you can’t send email from your Windows development box . Read my previous comments .
Usually , libraries or Classes are put on a folder outside the publicly available directory , but PHP-should be notified for the location of this folder . This is done via PHP’s configuration file “php.ini” . In this file all configurations are defined in the form of directives , for example the include path is defined by the “inlude_path” directive . On my development box it’s “include_path = “.;C:\phpLib;C:\phpLib\library;C:\phpLib\PEAR” . Each time you modify this file the server should be restarted .That’s not a problem on your local development box . But on a production server , that hosts hundreds websites on one box , you don’t have access to this file . Could you imagine what could happen when each individual website restarted the server each time a new configuration where made ? There are other techniques to pass configuration directives on a production machine . This is accomplished by an “.htaccess” file or through in_set statements directly into the PHP-code . My blog has over 120 articles dedicated to PHP (beginners to advanced level) .
Welcome to the wonderful world of web-development .

8. Hidayt Rahman - March 19, 2014

I want to use from Upload files using html input type file.
how can i use that
please help me…

i tired😦

tournasdimitrios1 - March 19, 2014

@Hidayt Rahman
To add an attachment to the mail object use the addAttachment function . Have a look at the code of the class . Adding multiple attachments can be done by calling the aforementioned function multiple times .

9. Alexander Sheppard - April 2, 2015

Out of all the different examples and tutorials I’ve been looking at for attaching files to an email like this, this is the only one that has actually worked! Thanks a bunch!

Tournas Dimitrios - April 6, 2015

@Alexander Sheppard
You are welcome🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s