◀ Back to examples

Advanced Example using SMTP

This demonstrates sending out one email messages with binary attachments with multipart/alternative support with Exception Handling.

<?php

require('assets/phpmailer2/PHPMailer Pro.php');

$mail = new codeworxtech\PHPMailer Pro();

try {

  $mail->SMTP_Debug     = 2; // 0 = off, 1 = basic, 2 = advanced
  $mail->SMTP_Host      = "mail.yourdomain.com";
  $mail->SetSMTPAccount( [ "yourname@yourdomain.com" => "yourpassword" ] );

  $mail->SetSender( ["name@yourdomain.com" => "First Last" ] );
  $mail->AddRecipient( ["johndoe@otherdomain.com" => "John Doe" ] );
  $mail->AddBCC( ["whogetsBCC@yourdomain.com" => "First Last" ] );

  $mail->Subject        = "PHPMailer Pro Test Subject via SMTP, basic with authentication";
  $mail->MessageHTML    = file_get_contents('contents.html');
  $mail->MessageText    = "To view the message, please use an HTML compatible email viewer!";

  $mail->AddAttachment($_SERVER['DOCUMENT_ROOT'] . "/phpmailer2/data/phpmailer2.gif");
  $mail->AddAttachment($_SERVER['DOCUMENT_ROOT'] . "/phpmailer2/data/phpmailer2_mini.gif");

  if ($mail->Send()) {
    echo 'Message has been sent.';
  }
} catch (Exception $e) {
  echo $e->errorMessage();
}

?>
Download example