All TalkersCode Topics

Follow TalkersCode On Social Media - A Social Media Network for developers Join Now ➔

Send Email Using SMTP In PHP

Last Updated : Mar 11, 2024

Send Email Using SMTP In PHP

In this article we will show you the solution of send email using smtp in PHP, to send emails, a mechanism known as SMTP is utilised over the internet.

You must connect to an SMTP server & send an email using the SMTP protocol in order to send an email using PHP.

PHP is a well-known server-side programming language that may be used to create web applications, such as sending emails using SMTP (Simple Mail Transfer Protocol).

Emails are sent over the internet using the SMTP protocol. The concept of sending emails over SMTP in PHP will be covered in this lesson, along with a comprehensive PHP code sample showing how to do so.

Now move to the concept of send email using smtp in php.

Step By Step Guide On Send Email Using Smtp In PHP :-

You must connect to the SMTP server & send an email by using SMTP protocol in order to send an email using PHP.

A sender's email, the recipient's email, the message's content, as well as any attachments are all mandatory components of the SMTP protocol.

$mail = new PHPMailer;
$mail->Host = '';
$mail->SMTPAuth = true;
$mail->Username = '';
$mail->Password = 'your-email-password';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->setFrom('', 'Your Name');
$mail->Subject = 'Test Email';
$mail->Body = 'This is a test email sent via SMTP using PHPMailer.';
if(!$mail->send()) {
    echo 'Message couldn’t be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent.';
  1. Have included PHPMailer library: The PHPMailer library must be included in the first line for SMTP email sending in PHP.
  2. Build a new PHPMailer object: In the next line, a new PHPMailer object is created, which is used to set and send a email message.
  3. Configure the SMTP connection: The SMTP server addresses, port number, authentication information, and encryption options are all specified in the following a few lines to configure the SMTP connection.
  4. In this illustration, TLS encryption and the SMTP server from Google are used.
  5. Set up the email message: The last few lines set up the email message by declaring the email addresses of the sender and the recipient, together with the email title, body, and any attachments.
  6. Send an email message: The last line of code uses the PHPMailer send() method to deliver the email message.
  7. The script will display "Message has been sent" if the email was sent successfully.
  8. If an email sending error occurs, the script will show "Message could not be sent." along with the PHPMailer error message.

Conclusion :-

As a result, we have successfully acquired the knowledge necessary to send emails using smtp in PHP.

Also, we discovered that this PHP code sample sends an email using SMTP by creating an SMTP connection, setting up the email message, and delivering the email through the SMTP protocol.

This code enables sending emails through SMTP of any email account that supports the protocol by altering the SMTP settings & email message information.

I hope this article on send email using smtp in PHP helps you and the steps and method mentioned above are easy to follow and implement.