php

How to Send an Email Using PHP

For any website, sending an email by PHP script is a very common requirement. For example, if the website contains any registration option for the user, then a confirmation email is required to send the user for email verification. You can send emails using PHP by utilizing the PHP mail() function or any PHP library. Two primarily used PHP libraries for sending email are PHPMailer and Swiftmailer. The way of sending an email using the mail() function was discussed in another tutorial. The uses of the PHPMailer library to send emails in PHP have been demonstrated in this tutorial.

Sending an Email Using PHPMailer:

PHPMailer is a very useful class library that contains a collection of functions to send email using PHP. You can send emails in various ways from the local server using this library. Run the following command to install PHPMailer in the Ubuntu operating system:

$ sudo apt-get install libphp-phpmailer

Prerequisites:

You must have a Gmail account to use the email sending facility of PHPMailer. After installing PHPMailer, you have to enable the option “Allow less secure apps” of your Gmail account.

Different Examples of Sending Email by Using PHPMailer:

Here are the following uses of the PHPMailer library to send email in different ways:

Example 1: Sending Text Email

The following script shows the way to send a simple email using PHPMailer. This script assigns four essential parts of any email (from, to, subject, body). setFrom and addAddress methods are used to set sender and receiver email addresses. Subject and Body properties are used to set the subject and body of the email. If the email is sent correctly, then the success message will be printed, and if the email isn’t sent, the failure message will be printed.

<?php

require '/usr/share/php/libphp-phpmailer/src/PHPMailer.php';

require '/usr/share/php/libphp-phpmailer/src/SMTP.php';

 

//Declare the object of PHPMailer

$email = new PHPMailer\PHPMailer\PHPMailer();

//Set up necessary configuration to send email

$email->IsSMTP();

$email->SMTPAuth = true;

$email->SMTPSecure = 'ssl';

$email->Host = "smtp.gmail.com";

$email->Port = 465;

//Set the gmail address that will be used for sending email

$email->Username = "[email protected]";

//Set the valid password for the gmail address

$email->Password = "password";

//Set the sender email address

$email->SetFrom("[email protected]");

//Set the receiver email address

$email->AddAddress("[email protected]");

//Set the subject

$email->Subject = "Testing Email";

//Set email content

$email->Body = "Hello! use PHPMailer to send email using PHP";


if(!$email->Send()) {

  echo "Error: " . $email->ErrorInfo;

} else {

  echo "Email has been sent.";

}

?>

Output:

The following output will appear if the email has been sent successfully:


If you check the receiver’s email address, then the following similar output will be shown:

Example 2: Sending HTML Formatted Email

You can send HTML formatted email using PHPMailer. You have to use isHTML() with true parameter value for sending HTML formatted email. Here, another method AltBody() is used to sent email content as plain text if the user are not able to receive the email with HTML content.

<?php

require '/usr/share/php/libphp-phpmailer/src/PHPMailer.php';

require '/usr/share/php/libphp-phpmailer/src/SMTP.php';

 

//Declare the object of PHPMailer

$email = new PHPMailer\PHPMailer\PHPMailer();

//Set up necessary configuration to send email

$email->IsSMTP();

$email->SMTPAuth = true;

$email->SMTPSecure = 'ssl';

$email->Host = "smtp.gmail.com";

$email->Port = 465;

$email->IsHTML(true);

//Set the gmail address that will be used for sending email

$email->Username = "[email protected]";

//Set the valid password for the gmail address

$email->Password = "password";

//Set the sender email address

$email->SetFrom("[email protected]");

//Set the receiver email address

$email->AddAddress("[email protected]");

$email->Subject = 'Message sent by PHPMailer';

$email->Body = '<h2>Hello!</h2><p style="color:Blue;">I am using PHPMailer to send email</p>';

$email->AltBody = ' This message is generated by plain text!';

$email->isHTML(true);

if(!$email->Send()) {

  echo "Error: " . $email->ErrorInfo;

} else {

  echo "Email has been sent.";

}

?>

Output:

If you check the receiver’s email address, then the following HTML formatted output will be shown:

Example 3: Sending an Email by Attaching a Local File

Many methods are available in PHPMailer class to send attached content with the email. AddAttachment() method is one of them used in the following script. This method has one mandatory parameter and three optional parameters.

addAttachment($path, $name, $encoding, $type);
<?php

require '/usr/share/php/libphp-phpmailer/src/PHPMailer.php';

require '/usr/share/php/libphp-phpmailer/src/SMTP.php';

 

//Declare the object of PHPMailer

$email = new PHPMailer\PHPMailer\PHPMailer();

//Set up necessary configuration to send email

$email->IsSMTP();

$email->SMTPAuth = true;

$email->SMTPSecure = 'ssl';

$email->Host = "smtp.gmail.com";

$email->Port = 465;

$email->IsHTML(true);

//Set the gmail address that will be used for sending email

$email->Username = "[email protected]";

//Set the valid password for the gmail address

$email->Password = "password";

//Set the sender email address

$email->SetFrom("[email protected]");

//Set the receiver email address

$email->AddAddress("[email protected]");

$email->addAddress('[email protected]');

$email->Subject = 'Message sent by PHPMailer';

$email->Body = 'Hello! use PHPMailer to send email using PHP';

$email->addAttachment('fruits.txt');


if(!$email->Send()) {

  echo "Error: " . $email->ErrorInfo;

} else {

  echo "Email has been sent.";

}

?>

Output:

When you check the receiver’s email address, then the attached file will be shown if the file is attached correctly with the email as shown below:

Example 4: Sending an Email by Attaching a Remote File

If you want to attach content from a remote location, then you can use the addStringAttachment() method for this purpose. This method has two parameters. The first parameter is used to get content from a particular URL location, and the second parameter is used to set the name of the attached file.

addStringAttachment(file_get_contents($url), 'filename.pdf');

In the following script, the URL address of an image file has been set in the first parameter, and “myfile.jpg” has been set in the second parameter of this method to set the attached file’s name.

<?php

require '/usr/share/php/libphp-phpmailer/src/PHPMailer.php';

require '/usr/share/php/libphp-phpmailer/src/SMTP.php';

 

//Declare the object of PHPMailer

$email = new PHPMailer\PHPMailer\PHPMailer();

//Set up necessary configuration to send email

$email->IsSMTP();

$email->SMTPAuth = true;

$email->SMTPSecure = 'ssl';

$email->Host = "smtp.gmail.com";

$email->Port = 465;

$email->IsHTML(true);

//Set the gmail address that will be used for sending email

$email->Username = "[email protected]";

//Set the valid password for the gmail address

$email->Password = "password";

//Set the sender email address

$email->SetFrom("[email protected]");

//Set the receiver email address

$email->AddAddress("[email protected]");

$email->addAddress('[email protected]');

$email->Subject = 'Message sent by PHPMailer';

$email->Body = 'Hello! use PHPMailer to send email using PHP';

$email->addStringAttachment(file_get_contents('http://localhost/phpcode/flower.jpg'), 'myfile.jpg');


if(!$email->Send()) {

  echo "Error: " . $email->ErrorInfo;

} else {

  echo "Email has been sent.";

}

?>

Output:

When you check the receiver’s email address, then the attached file will be shown if the file is attached correctly with the email as shown below:

Example 5: Sending Email With a Debug Message

The SMTPDebug property of the PHPMailer class is used to enable the debug option before sending the email. After executing the script, the value of this property has been set to 1 in this script to display the debug message.

<?php

require '/usr/share/php/libphp-phpmailer/src/PHPMailer.php';

require '/usr/share/php/libphp-phpmailer/src/SMTP.php';

 

//Declare the object of PHPMailer

$email = new PHPMailer\PHPMailer\PHPMailer();

//Enable debug output

$email->SMTPDebug = 1;

//Set up necessary configuration to send email

$email->IsSMTP();

$email->SMTPAuth = true;

$email->SMTPSecure = 'ssl';

$email->Host = "smtp.gmail.com";

$email->Port = 465;

//Set the gmail address that will be used for sending email

$email->Username = "[email protected]";

//Set the valid password for the gmail address

$email->Password = "password";

//Set the sender email address

$email->SetFrom("[email protected]example.com");

//Set the receiver email address

$email->AddAddress("[email protected]");

//Set the subject

$email->Subject = "Testing Email";

//Set email content

$email->Body = "Hello! use PHPMailer to send email using PHP";


if(!$email->Send()) {

  echo "Error: " . $email->ErrorInfo;

} else {

  echo "Email has been sent.";

}

?>

Example 6: Sending an Email to Multiple Addresses

The way of sending an email to multiple email addresses by using PHPMailer has been shown in the following script. The addCC() and addBCC() methods of this class have been used to send an email to multiple receivers at a time.

<?php

require '/usr/share/php/libphp-phpmailer/src/PHPMailer.php';

require '/usr/share/php/libphp-phpmailer/src/SMTP.php';

 

//Declare the object of PHPMailer

$email = new PHPMailer\PHPMailer\PHPMailer();

 

//Set up necessary configuration to send email

$email->IsSMTP();

$email->SMTPAuth = true;

$email->SMTPSecure = 'ssl';

$email->Host = "smtp.gmail.com";

$email->Port = 465;

//Set the gmail address that will be used for sending email

$email->Username = "[email protected]";

//Set the valid password for the gmail address

$email->Password = "password";

//Set the sender email address

$email->SetFrom("[email protected]");

//Set the receiver email addresses

$email->addAddress('[email protected]', 'Jafar Iqbal');

$email->addAddress('[email protected]');

$email->addReplyTo('[email protected]');

$email->addCC('[email protected]');

$email->addBCC('[email protected]');

//Set the subject

$email->Subject = "Testing Email";

//Set email content

$email->Body = "Hello! use PHPMailer to send email using PHP";


if(!$email->Send()) {

  echo "Error: " . $email->ErrorInfo;

} else {

  echo "Email has been sent.";

}

?>

Output:

If you check the receiver’s email address, then the following information will be shown:

Conclusion:

Most of the email sending options using the PHPMailer library were discussed in this tutorial. Also, several examples were provided to help the PHP users learn the process of sending emails easily by using this library in different ways. We hope you found this article helpful. Check the other Linux Hint articles for more tips and information.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.