PHPMailer ultimate tutorial

PHP have inbuilt mail() function to send email right from the code. This is easy to use and effective too but it has one serious problem. It gives load to production server and also provide no guarantee about email delivery.

PHPMailer is one of the popular and easy to use email library for php. This solves the issue of production load and guarantee of email delivery by allowing us to configure SMTP server.

How to use it?

For your ease i have created a custom function which of course uses PHPMailer and SMTP. Here is a function.

    function sendmail($to,$subject,$message,$name)
                  $mail             = new PHPMailer();
                  $body             = $message;
                  $mail->SMTPAuth   = true;
                  $mail->Host       = "";
                  $mail->Port       = 587;
                  $mail->Username   = "";
                  $mail->Password   = "your gmail password";
                  $mail->SMTPSecure = 'tls';
                  $mail->SetFrom('', 'Your name');
                  $mail->AddReplyTo("","Your name");
                  $mail->Subject    = $subject;
                  $mail->AltBody    = "Any message.";
                  $address = $to;
                  $mail->AddAddress($address, $name);
                  if(!$mail->Send()) {
                      return 0;
                  } else {
                        return 1;

To use this you need two files, class.phpmailer.php and class.smtp.php. You can download and copy these two files from link given below.

External link: Download PHPMailer library.

Once download place those two files plus the one contains above function. Here is a link of the file.

Internal link : Download custom phpmailer function.

      $to       =   "some email";
      $subject  =   "Hello";
      $message  =   "hello how are you.";
      $name     =   "Shahid Shaikh";
      $mailsend =   sendmail($to,$subject,$message,$name);
        echo '

email sent.

        echo '

There are some issue.


Performance study:

Recently i was assigned to one project in my company and in that we were using php mail() function. This function used to take like 30 seconds to send one email. Reason was the heavy production server but obviously this is not what client expects.

So i changed it to PHPMailer and after using the Google app smtp i am able to send email within 3 seconds at top. Reduction of 27 seconds and guaranteed delivery of email.

Further study:

28 thoughts on “PHPMailer ultimate tutorial”

  1. Alexander Kachowski

    So when somebody will not be lazy to look at the source of the form for filling. Here you go – my email address and password to get in? Couldn’t you move these sensitive things at least in the other fileslike bla-bla-smtp.php or bla-bla-*mailer.php at least?

    1. Oops. It mistake, thanks for pointing out. Updating it. However code will work because PHP is interpreter and it will take latest value of $host.

  2. Hi, thanks for this tutorial 🙂
    What about when you are being contacted from the contact form?
    $mail->SetFrom() should contain the email of the internet user, will that still work??

  3. ps. you are first site where i disabled adblock + 1 click, to earn you some money.
    see you.

  4. Hi.
    Thanks for this tutorial.
    Can you please tell me how i can make html form & link it with this php mailer code?
    I think it’s better to if complete your tutorial with html form example.

    Thank you.

  5. Plz can you help me by giving step to step tutorial in building a mailer as a beginner

  6. ne biçim anlatmışsınız kardeşim bi recaptcha uygulamasına bakıyım dedim yarım yamalak anlatılmış bu da öyle eksik !

  7. SMTP Error: Could not connect to SMTP host.
    There are some issue.

    i m using the filezilla or online server but this error show when i send email using the phpmailer

  8. Gökhan Yılmaz

    ” require_once(‘class.phpmailer.php’); ”

    ” require_once(‘PHPMailerAutoload.php’); ”

    PHPMailerAutoload.php is in the same library.
    With out this you can get SMTP Class error messages.

    Additional you can add
    ” $mail->CharSet = ‘UTF-8’; ”
    right after
    ” $mail = new PHPMailer(); ”
    for more İnternational support.

    1. Satender Singh Rathore

      it is not working in ununtu.
      it is comming : there are some issue.
      please help. Thank You.

    2. Satender Singh Rathore

      it is not woarking in ununtu
      send mail function is returning 0

      please help . Thank you

  9. If you get the SMTP class error you need to edit code from:


    Make sure the path is correct…

  10. What’s the best way to have phpmailer to redirect to another php-page after a success sent message. Before I was using the Sendmail and then I just put i a new header but that’s not possible any longer. I don’t just what to have a white page whit the text “message sent”.

  11. Message could not be sent.Mailer Error: SMTP connect() failed.
    I got error like this
    I am using XAMPP

    1. If you haven’t solved it yet try setting your debug on
      $mail->SMTPDebug = 3;

      In this you will the error you are looking for

  12. this code doesn’t work for me:
    the value of Port and host they are static ??
    if they are not static , so What is the princip of filling this parameters.

Comments are closed.