Email sending functionality is a crucial part of many web applications. From account verification emails to forgotten password reminders, email helps build communication bridges between your application and its users. One of the most popular libraries for sending emails from PHP applications is PHPMailer. It has a host of features that make it a robust and flexible solution for managing your application’s email functionality. This article will provide a practical approach to implementing PHPMailer in your applications.
What is PHPMailer?
PHPMailer is an open-source library in PHP that provides a simple and flexible way to send emails from within PHP programs. It supports several ways of sending emails, including mail(), Sendmail, and SMTP. This makes it a highly versatile tool that can adapt to different hosting environments. PHPMailer also supports HTML email, attachments, and advanced security protocols, providing a robust and secure way to handle email communications.
Setup PHPMailer
To use PHPMailer, you first need to install it in your project. The easiest way to do this is by using Composer, a tool for dependency management in PHP. If you have Composer installed, you can add PHPMailer to your project with the following command:
composer require phpmailer/phpmailer
Step 2 – Sending Emails using PHP and PHPMailer
Now create a simple PHP script sendMail.php in your web document root and add the below content. The below script is using Gmail SMTP server for sending emails. You may use any other SMTP server like Amazon SES, Sendgrid, Mailchimp or Mandril App, etc.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | <?php use PHPMailer\PHPMailer\PHPMailer; use PHPMailer\PHPMailer\Exception; require 'vendor/autoload.php'; $mail = new PHPMailer(true); try { //Server settings $mail->SMTPDebug = 2; // Enable verbose debug output $mail->isSMTP(); // Set mailer to use SMTP $mail->Host = 'smtp.example.com'; // Specify main and backup SMTP servers $mail->SMTPAuth = true; // Enable SMTP authentication $mail->Password = 'secret'; // SMTP password $mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted $mail->Port = 587; // TCP port to connect to //Recipients //$mail->addCC('[email protected]'); //Uncomment to add CC recipient //$mail->addBCC('[email protected]'); //Uncomemnt to add BCC recipient //Attachments //$mail->addAttachment('./assets/invoice.pdf'); //Attach file to email //Content $mail->isHTML(true); // Set email format to HTML $mail->Subject = 'Here is the subject'; $mail->Body = 'This is the HTML message body'; $mail->AltBody = 'This is the body in plain text for non-HTML mail clients'; $mail->send(); echo 'Message has been sent'; } catch (Exception $e) { echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}"; } |
In this code, the SMTP server settings are defined, and the sender’s and receiver’s email addresses are specified. The email’s subject, body, and alternative text body for non-HTML mail clients are also set, and the email is sent using the send() method.
Conclusion
PHPMailer offers a powerful, flexible, and practical approach to sending emails from your PHP applications. It provides a host of features such as SMTP support, HTML emails, attachments, and security protocols, making it a robust solution for your application’s email needs.
By using PHPMailer, you can easily manage your application’s email communications, providing a better experience for your users and potentially saving development time and effort. Whether you’re developing a small personal project or a large-scale commercial application, PHPMailer has the capabilities to meet your email requirements.
Remember, though, that while PHPMailer is a powerful tool, it also needs to be used responsibly. Be mindful of your users’ privacy and ensure you adhere to best practices when handling sensitive information.
9 Comments
I am also receiving the same blank screen when i run the sendMail.php. The apache error log entry is
“GET /sendMail.php HTTP/1.1” 500 185 “-” “Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:78.0) Gecko/20100101 Firefox/78.0”
Simple instruction with good example code. Sending attachments is now easy. Thanks you!
It is also possible to use PHPMailer to send emails from local web server. For this you will have to define following variables
$mail->From
$mail->FromName
$mail->addAddress
$mail->isHTML(true)
$mail->Subject
$mail->Body
$mail->AltBody
Source: How to use PHPmailer
this line writen in my page when i go to
http://mydomain/sendmail.php
” b0nano 2.5.3xvrootvultr.ubuntusendmail.php ”
with no mail sent to mailbox i input in the sendmail.php
so. whats next and thank you. sir
Why I need this myfile.tar.gz? It’s making and error. I suppose it is because my server is Windows, but I don’t know what to do anyway..
“Could not access file: /backup/myfile.tar.gz
A mensagem não pode ser enviada.Mailer Error: Could not access file: /backup/myfile.tar.gz”
Hi Pachak,
I have setup php 7 and try to use phpmailer 5.2 version. But its provide following error :
Connection: opening to host(this is my email host):465
Mailer Error: SMTP connect() failed.
I have used 465,25 and 587.
Is phpmailer 5.2 not compatible with PHP 7?
Looking for your help.
Best,
Amit
Same for me
I tried with your code, but it returned blank page. It didn’t show “Message sent” nor any errors.
Hi,
Is there any error in Apache logs ?