How to Send Email from PHP

28/12/2020
php
For any website, sending email by PHP script is a very common requirement.  You can send email using PHP by using PHP mail function or by using a PHP library named PHPMailer. The main limitation of mail() function is that it can’t send email from local server. mail() function only works on live server and in many cases , the email sent by mail() function is stored in spam folder. So it is better to use PHPMailer to send email from local server using PHP.  In this tutorial you will learn how you can use PHPMailer to send email using different options.

Sending email using PHPMailer

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

$ sudo apt-get install libphp-phpmailer

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

Example-1:

The following script shows the way to send simple email using PHPMailer. In this script, four essential parts of any email (from, to, subject, body) are assigned. setFrom and addAddress methods are used to set sender and receiver email address. Subject and Body properties are used to set subject and body of the email. Set other configurations of gmail account properly using methods and properties of PHPMailer class. If the email can send properly then the success message will print and if the email can’t send then the failure message will print.

<?php
require ‘/usr/share/php/libphp-phpmailer/class.phpmailer.php’;
require ‘/usr/share/php/libphp-phpmailer/class.smtp.php’;
$mail = new PHPMailer;
$mail->setFrom([email protected]);
$mail->addAddress([email protected]);
$mail->Subject = ‘Message sent by PHPMailer’;
$mail->Body = ‘Hello! use PHPMailer to send email using PHP’;
$mail->IsSMTP();
$mail->SMTPSecure = ‘ssl’;
$mail->Host = ‘ssl://smtp.gmail.com’;
$mail->SMTPAuth = true;
$mail->Port = 465;

//Set your existing gmail address as user name
$mail->Username = <a href="mailto:’[email protected]">[email protected]</a>’;

//Set the password of your gmail address here
$mail->Password = ‘password’;
if(!$mail->send()) {
  echo ‘Email is not sent.’;
  echo ‘Email error: ‘ . $mail->ErrorInfo;
} else {
  echo ‘Email has been sent.’;
}
?>

Output:

if the check the receiver email address then following output will be shown

Example-2:

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/class.smtp.php’;
$mail = new PHPMailer;
$mail->setFrom([email protected]);
$mail->addAddress([email protected]);
$mail->Subject = ‘Message sent by PHPMailer’;
$mail->Body = ‘<h2>Hello!</h2><p style="color:Blue;">
I am using PHPMailer to send email</p>’
;

$mail->isHTML(true);
$mail->AltBody = "This message is generated by plain text !";
$mail->IsSMTP();
$mail->SMTPSecure = ‘ssl’;
$mail->Host = ‘ssl://smtp.gmail.com’;
$mail->SMTPAuth = true;
$mail->Port = 465;
$mail->Username = [email protected];
$mail->Password = ‘password’;
if(!$mail->send()) {
  echo ‘Email is not sent.’;
  echo ‘Email error: ‘ . $mail->ErrorInfo;
} else {
  echo ‘Email has been sent.’;
}
?>

Output:

if you check the receiver email address then the following HTML formatted output will be shown.

Example-3:

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

addAttachment($path, $name, $encoding, $type);

Only mandatory parameter is used in the script which is used to attach ‘employees.json’ file with mail.

<?php
require ‘/usr/share/php/libphp-phpmailer/class.phpmailer.php’;
require ‘/usr/share/php/libphp-phpmailer/class.smtp.php’;
$mail = new PHPMailer;
$mail->setFrom([email protected]);
$mail->addAddress([email protected]);
$mail->Subject = ‘Message sent by PHPMailer’;
$mail->Body = ‘Hello! use PHPMailer to send email using PHP’;
$mail->addAttachment(’employees.json’);
$mail->IsSMTP();
$mail->SMTPSecure = ‘ssl’;
$mail->Host = ‘ssl://smtp.gmail.com’;
$mail->SMTPAuth = true;
$mail->Port = 465;
$mail->Username = [email protected];
$mail->Password = ‘password’;
if(!$mail->send()) {
  echo ‘Email is not sent.’;
  echo ‘Email error: ‘ . $mail->ErrorInfo;
} else {
  echo ‘Email has been sent.’;
}
?>

Output:

When you check the receiver email address then the attached file  will be shown if the file is attached properly with the email.

Example-4:

if you want to attach content from remote location then you can use addStringAttachment() method for this purpose. This method has two parameters. First parameter is used to get content from particular URL location and 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 is set in the first parameter  and ‘myfile.jpg’ is set in the second parameter of this method to set the name of the attach file.

<?php
require ‘/usr/share/php/libphp-phpmailer/class.phpmailer.php’;
require ‘/usr/share/php/libphp-phpmailer/class.smtp.php’;
$mail = new PHPMailer;
$mail->setFrom([email protected]);
$mail->addAddress([email protected]);
$mail->Subject = ‘Message sent by PHPMailer’;
$mail->Body = ‘Hello! use PHPMailer to send email using PHP’;
$mail->addStringAttachment(file_get_contents(‘http://localhost/f3.jpg’), ‘myfile.jpg’);
$mail->IsSMTP();
$mail->SMTPSecure = ‘ssl’;
$mail->Host = ‘ssl://smtp.gmail.com’;
$mail->SMTPAuth = true;
$mail->Port = 465;
$mail->Username = [email protected];
$mail->Password = ‘password’;
if(!$mail->send()) {
  echo ‘Email is not sent.’;
  echo ‘Email error: ‘ . $mail->ErrorInfo;
} else {
  echo ‘Email has been sent.’;
}
?>

Output:

The following message will be appeared if the script runs properly.

When you check the receiver email address then the attached file will be shown if the file is attached properly with the email.

After practicing the above examples, you will be able to send email from localhost by using your gmail account and PHPMailer class according to your requirements in PHP.

Also please check the video below for more information:

ONET IDC thành lập vào năm 2012, là công ty chuyên nghiệp tại Việt Nam trong lĩnh vực cung cấp dịch vụ Hosting, VPS, máy chủ vật lý, dịch vụ Firewall Anti DDoS, SSL… Với 10 năm xây dựng và phát triển, ứng dụng nhiều công nghệ hiện đại, ONET IDC đã giúp hàng ngàn khách hàng tin tưởng lựa chọn, mang lại sự ổn định tuyệt đối cho website của khách hàng để thúc đẩy việc kinh doanh đạt được hiệu quả và thành công.
Bài viết liên quan

How to Send Email from PHP

For any website, sending email by PHP script is a very common requirement.  You can send email using PHP by using PHP...
28/12/2020

Building a Contact Form in PHP

How to create contact form using HTML, CSS and PHP A contact form is a very essential part of any website. The visitors...
28/12/2020

PHP Tutorial For Beginners

If you are new in PHP then this tutorials will help you to learn PHP from the beginning. PHP Basics: Hello World Comments Variables...
29/12/2020
Bài Viết

Bài Viết Mới Cập Nhật

Mua proxy v4 chạy socks5 để chơi game an toàn, tốc độ cao ở đâu?
18/05/2024

Thuê mua proxy Telegram trọn gói, tốc độ cao, giá siêu hời
18/05/2024

Thuê mua proxy Viettel ở đâu uy tín, chất lượng và giá tốt? 
14/05/2024

Dịch vụ thuê mua proxy US UK uy tín, chất lượng số #1
13/05/2024

Thuê mua proxy Việt Nam: Báo giá & các thông tin MỚI NHẤT
13/05/2024