PHPMailer is an open source library of php to send mail based on Object Oriented Programming concept. PHPMailer is most popular library to send mail now a days. We can say that, it is an alternative of php’s mail() function. 

In this blog we will see how to send mail using PHPMailer with plain text, HTML body, cc/bcc and attachments.

Sending Email with only plain text using PHPMailer

Step 1: First of all you have to start XAMPP or WAMP. After that create one folder into C:\xampp\htdocs\ with appropriate name. Create file sendmail.php in folder and write below code in it. Please read my comments to understand code properly.

sendmail.php

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <title>Login</title>

    <style>

        input[type=text],input[type=email],input[type=password]{

            width:100% !important;

        }

        table{

            border: 1px solid rgb(202,207,210);

        }

        form {

            margin: 10% auto 0;

        }

    </style>

</head>

<body>

    <form method="post">

       <center><h1>Send Mail</h1></center>

        <table align="center" border="0">

            <tr>

                <td>Email Address:</td>

                <td><input type="email" name="txtuemail"></td>

            </tr>

            <tr>

                <td colspan="2" align="center"><input type="submit" value="Send" name="btnsend"></td>

            </tr>

        </table>

    </form>

    <?php

    /* Send mail Code Start */

    if (isset($_POST["btnsend"])) /* Send button click event */ 

    {

        $Toemail = $_POST["txtuemail"]; /* Take textbox value whom you want to send mail */

  

        require 'phpmail/PHPMailerAutoload.php';

        require 'phpmail/class.phpmailer.php';

        include 'phpmail/class.smtp.php';

  

        $mailer = new PHPMailer;

        $mailer->isSMTP();

        $mailer->SMTPDebug = 0;

        $mailer->Debugoutput = 'html';

        $mailer->Host = 'smtp.gmail.com';

        $mailer->Port = 587;

        $mailer->SMTPSecure = 'tls';

        $mailer->SMTPAuth = true;

        $mailer->Username = "FromEmailAddress@xxx.com"; /* Enter sender emailid of gmail */

        $mailer->Password = "FromEmailAddress Password"; /* Enter sender password of gmail */

        $mailer->setFrom('FromEmailAddress@xxx.com');

        $mailer->addAddress($Toemail);

        $mailer->Subject = "Check mail using PHPMailer";

        $mailer->Body = 'Checking mail came using PHPMailer..!!';

        if ($mailer->Send())

        {

            echo "<script>alert('Mail sent successfully..!!')</script>";

        }

        else

        {

            echo "<script>alert('Something getting wrong while sending mail...')</script>" . $mailer->ErrorInfo;

        }

    }

    /* Send mail Code End */

?>

</body>

</html>

If mail sent than "Mail sent successfully..!!" message will display otherwise "Something getting wrong while sending mail..!!" message will display.

If your mail send successfully then you have to check your mail account which you type in textbox and you got one mail and it look like.

Sending Email with CC and BCC

You have to follow same steps as above ie. Create .php file etc… Only the mailing code will be change which is given below. $mailer->addCC() is used to set CC and $mailer->addBCC() is used to set BCC in mail.

<?php

    $Toemail = "ToEmailAddress@xxx.com";

  

    require 'phpmail/PHPMailerAutoload.php';

    require 'phpmail/class.phpmailer.php';

    include 'phpmail/class.smtp.php';

  

    $mailer = new PHPMailer;

    $mailer->isSMTP();

    $mailer->SMTPDebug = 0;

    $mailer->Debugoutput = 'html';

    $mailer->Host = 'smtp.gmail.com';

    $mailer->Port = 587;

    $mailer->SMTPSecure = 'tls';

    $mailer->SMTPAuth = true;

    $mailer->Username = "FromEmailAddress@xxx.com";

    $mailer->Password = "FromEmailAddress Password";

    $mailer->setFrom('FromEmailAddress@xxx.com');

    $mailer->addAddress($Toemail);

    $mailer->addCC("ccEmailAddress@xxx.com"); /* Enter receiver emailid in CC */

    $mailer->addBCC("bccEmailAddress@xxx.com"); /* Enter receiver emailid in BCC */

    $mailer->Subject = "Check mail using PHPMailer";

    $mailer->Body    = 'Checking mail came using PHPMailer..!!';

    if ($mailer->Send())

    {

        echo "<script>alert('Mail sent successfully..!!')</script>";

    }

    else

    {

        echo "<script>alert('Something getting wrong while sending mail...')</script>" . $mailer->ErrorInfo;

    }

?>

Output

Sending Email with HTML body

Let us see how to send HTML in email body. To send html body in mail we have to set $mailer->isHTML(true) for mailer.

<?php

    $Toemail = "ToEmailAddress@xxx.com";

  

    require 'phpmail/PHPMailerAutoload.php';

    require 'phpmail/class.phpmailer.php';

    include 'phpmail/class.smtp.php';

  

    $mailer = new PHPMailer;

    $mailer->isSMTP();

    $mailer->SMTPDebug = 0;

    $mailer->Debugoutput = 'html';

    $mailer->Host = 'smtp.gmail.com';

    $mailer->Port = 587;

    $mailer->SMTPSecure = 'tls';

    $mailer->SMTPAuth = true;

    $mailer->Username = "FromEmailAddress@xxx.com";

    $mailer->Password = "FromEmailAddress Password";

    $mailer->setFrom('FromEmailAddress@xxx.com');

    $mailer->addAddress($Toemail);

    $mailer->Subject = "Check mail using PHPMailer";

    $mailer->Body    = '<div style="margin: 0 auto;display:table;">

                           <h1>This is HTML format</h1>

                              <table border="1" style="width: 100%">

                                <tr>

                                  <td><b>This is table format with b tag</b></td>

                                </tr>

                                <tr>

                                  <td><i>This is table format with i tag</i></td>

                                </tr>

                                <tr>

                                  <td><p>This is p tag used for paragraph.</p></td>

                                </tr>

                              </table>

                        </div>'; /* Set html body */

    $mailer->isHTML(true); /* Set isHTMl to true to send html in email body */

    if ($mailer->Send())

    {

        echo "<script>alert('Mail sent successfully..!!')</script>";

    }

    else

    {

        echo "<script>alert('Something getting wrong while sending mail...')</script>" . $mailer->ErrorInfo;

    }

?>

Output

Sending Email with an attachment

Let us see code to send mail with attachment of any file ie. .doc, .jpg, .txt etc… Add attachment in mail using $mailer->AddAttachment('Filepath','filename'). Filename parameter is optional.

<?php

    $Toemail = "ToEmailAddress@xxx.com";

  

    require 'phpmail/PHPMailerAutoload.php';

    require 'phpmail/class.phpmailer.php';

    include 'phpmail/class.smtp.php';

    

    $mailer = new PHPMailer;

    $mailer->isSMTP();

    $mailer->SMTPDebug = 0;

    $mailer->Debugoutput = 'html';

    $mailer->Host = 'smtp.gmail.com';

    $mailer->Port = 587;

    $mailer->SMTPSecure = 'tls';

    $mailer->SMTPAuth = true;

    $mailer->Username = "FromEmailAddress@xxx.com";

    $mailer->Password = "FromEmailAddress Password";

    $mailer->setFrom('FromEmailAddress@xxx.com');

    $mailer->addAddress($Toemail);

    $mailer->Subject = "Check mail using PHPMailer";

    $mailer->Body = 'Checking mail came using PHPMailer..!!';

    $mailer->AddAttachment('images/Jellyfish.jpg', 'Jellyfish.jpg'); /* Set file path and file name for attachment */

    $mailer->AddAttachment('doc1.docx'); /* Set file path. File name is optional */

    if ($mailer->Send())

    {

        echo "<script>alert('Mail sent successfully..!!')</script>";

    }

    else

    {

        echo "<script>alert('Something getting wrong while sending mail...')</script>" . $mailer->ErrorInfo;

    }

?>

Output

I hope I have covered all mailing code. But if there is something missed please comment below.