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.

<?php
    require_once('class.phpmailer.php');
    function sendmail($to,$subject,$message,$name)
    {
                  $mail             = new PHPMailer();
                  $body             = $message;
                  $mail->IsSMTP();
                  $mail->SMTPAuth   = true;
                  $mail->Host       = "smtp.gmail.com";
                  $mail->Port       = 587;
                  $mail->Username   = "[email protected]";
                  $mail->Password   = "your gmail password";
                  $mail->SMTPSecure = 'tls';
                  $mail->SetFrom('[email protected]', 'Your name');
                  $mail->AddReplyTo("[email protected]","Your name");
                  $mail->Subject    = $subject;
                  $mail->AltBody    = "Any message.";
                  $mail->MsgHTML($body);
                  $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.

<?php
      include("sendmail.php");
      $to       =   "some email";
      $subject  =   "Hello";
      $message  =   "hello <i>how are you.</i>";
      $name     =   "Shahid Shaikh";
      $mailsend =   sendmail($to,$subject,$message,$name);
      if($mailsend==1){
        echo '<h2>email sent.</h2>';
      }
      else{
        echo '<h2>There are some issue.</h2>';
      }
?>

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:

Shahid

Founder of Codeforgeek. Technologist. Published Author. Engineer. Content Creator. Teaching Everything I learn!

Related Posts

28 Comments

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

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

  4. 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 !

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

  6. replace
    ” require_once(‘class.phpmailer.php’); ”
    with

    ” 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. it is not working in ununtu.
      it is comming : there are some issue.
      please help. Thank You.

  7. If you get the SMTP class error you need to edit code from:
    require_once(‘class.phpmailer.php’);

    to:
    require_once(‘PHPMailerAutoload.php’);

    Make sure the path is correct…

  8. 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”.

    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

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

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.