Bootstrap FreeKB - PHPMailer - Send email using PHP
PHPMailer - Send email using PHP

Updated:   |  PHPMailer articles

As the name suggests, PHPMailer can be used to send email using PHP.

Download the PHPMailer package from https://github.com/PHPMailer/PHPMailer. This will download a file named PHPMailer-master.zip to your local PC. Upload PHPMailer-master.zip to your PHP system. Let's say your PHP system is a Linux system, and PHPMailer-master.zip is uploaded to the /tmp directory. The unzip command can be used to extract PHPMailer-master.zip to the directory where you want PHPMailer to reside. In this example, PHPMailer will be extracted to /var/www/PHPMailer-master.

unzip /tmp/PHPMailer-master.zip -d /var/www

 

Remove the .zip file.

rm /tmp/PHPMailer-master.zip

 

Optionally, renamed the PHPMailer-master directory if you would like.

mv /var/www/PHPMailer-master /var/www/phpmailer

 

PHPMailer uses the sendmail daemon. The which command can be used to determine if sendmail is installed. If sendmail is not installed, something like this should be returned.

~]# which sendmail
/usr/bin/which: no sendmail in (/root/.local/bin:/root/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin)

 

On a Debian distribution (Debian, Ubuntu, Mint), apt-get can be used to install sendmail. On a Red Hat distribution (CentOS, Fedora, Red Hat), dnf or yum can be used to install sendmail.

dnf install sendmail

 

To send an email with no authentication and no encryption.

<?php
  use PHPMailer\PHPMailer\PHPMailer;
  use PHPMailer\PHPMailer\Exception;
  use PHPMailer\PHPMailer\SMTP;

  require "/var/www/phpmailer/src/PHPMailer.php";
  require "/var/www/phpmailer/src/Exception.php";
  require "/var/www/phpmailer/src/SMTP.php";

  $mail = new PHPMailer(true);
  try {
    $mail->isMail();
    $mail->isHTML(true);
    $mail->Host = "mx1.example.com";
    $mail->Port = 25;
    $mail->setFrom("webmaster@example.com", "Webmaster");
    $mail->addAddress("John.Doe@example.com", "John Doe");
    $mail->addReplyTo("no-reply@example.com", "No Reply");
    $mail->Subject = "Here is the subject";
    $mail->Body    = "This is the message body";
    $mail->send();
  }
  catch (Exception $e) {
    echo "Emailcould not be sent. Error: {$mail->ErrorInfo}";
  }
?>

 

To send an email with authentication and encryption, you will probably be using port 465 or 587. OpenSSL can be used to determine if you are able to connect to the email server over port 465 or 587.

~]$ openssl s_client -connect mx1.example.com:587
CONNECTED(00000003)
Verify return code: 0 (ok)

 

Assuming you can connect to the email server over port 465 or 587, here is the PHP you would use.

<?php
   require "/var/www/phpmailer/PHPMailerAutoload.php";
   $mail = new PHPMailer;
   $mail->isSMTP();
   $mail->Host = "mx1.example.com";
   $mail->SMTPAuth = true;
   $mail->Username = "user1";
   $mail->Password = "Password123";
   $mail->SMTPSecure = "tls";
   $mail->Port = 587;
   
   $mail->setFrom("webmaster@example.com", "Webmaster");
   $mail->addAddress("John.Doe@example.com", "John Doe");
   $mail->addReplyTo("no-reply@example.com", "No Reply");

   $mail->Subject = "Here is the subject";
   $mail->Body    = "This is the message body";

   if(!$mail->send()) 
   {
       echo "Message could not be sent.";
       echo "Mailer Error: " . $mail->ErrorInfo;
   }
   else 
   {
       echo "Message has been sent";
   }
?>

 


Debugging

If you run into problems, add the following to display verbose information.

$mail->SMTPDebug = 4;

 


SELinux

Use the sestatus command to determine if SELinux is enforcing, permissive, or disabled.

~]# sestatus
SELinux status: enforcing

 

If SELinux is enforcing, use the getsebool command to determine if the httpd_can_sendemail and httpd_can_network_connect boolens are turned on or off.

~]# getsebool httpd_can_sendmail
httpd_can_sendmail --> off

~]# getsebool httpd_can_network_connect
httpd_can_network_connect --> off

 

If the booleans are off, use the setsebool command to turn the booleans on.

[root@server1 ~]# setsebool -P httpd_can_sendmail 1
[root@server1 ~]# setsebool -P httpd_can_network_connect 1

 

It's important to recognize that the PHP web server will be sending the email to the Email server. 

 

If a Linux web server is being used, such as Apache, HTTPD, or NGINX, the web server's SELinux context will need to allow PHP to send an email to the Email server. 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 277682 in the box below so that we can be sure you are a human.