Send SMTP mail in PHP

You can send SMTP mail in PHP using the built-in mail() function or by using a third-party library like PHPMailer.

Here’s an example of using the mail() function to send an email:

$to = '[email protected]';
$subject = 'Test email';
$message = 'This is a test email sent using PHP';
$headers = 'From: [email protected]' . "\r\n" .
           'Reply-To: [email protected]' . "\r\n" .
           'X-Mailer: PHP/' . phpversion();

if (mail($to, $subject, $message, $headers)) {
    echo 'Email sent successfully';
} else {
    echo 'An error occurred while sending the email';
}

In this example, you set the recipient email address in the $to variable, the subject of the email in the $subject variable, and the message content in the $message variable. You then set the headers for the email, including the sender email address, using the $headers variable. Finally, you call the mail() function with the recipient email address, subject, message, and headers as parameters.

If the email is sent successfully, the message “Email sent successfully” will be displayed. If an error occurs while sending the email, the message “An error occurred while sending the email” will be displayed.

Alternatively, you can use a third-party library like PHPMailer to send SMTP mail in PHP. Here’s an example of using PHPMailer:

use PHPMailer\PHPMailer\PHPMailer;

// Include the PHPMailer autoload file
require_once 'path/to/PHPMailer/src/PHPMailer.php';

// Create a new PHPMailer object
$mail = new PHPMailer();

// Set the SMTP credentials
$mail->isSMTP();
$mail->Host = 'smtp.example.com';
$mail->SMTPAuth = true;
$mail->Username = '[email protected]';
$mail->Password = 'password';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;

// Set the sender and recipient email addresses, subject, and message content
$mail->setFrom('[email protected]', 'Sender Name');
$mail->addAddress('[email protected]', 'Recipient Name');
$mail->Subject = 'Test email';
$mail->Body = 'This is a test email sent using PHPMailer';

// Send the email
if ($mail->send()) {
    echo 'Email sent successfully';
} else {
    echo 'An error occurred while sending the email: ' . $mail->ErrorInfo;
}

In this example, you first include the PHPMailer autoload file and create a new PHPMailer object. You then set the SMTP credentials for your email server, including the SMTP host, username, password, and port. You also set the sender and recipient email addresses, subject, and message content.

Finally, you call the send() method on the PHPMailer object to send the email. If the email is sent successfully, the message “Email sent successfully” will be displayed. If an error occurs while sending the email, the message “An error occurred while sending the email” will be displayed, along with the error message returned by PHPMailer.

How can I send HTML mail with PHPMailer?

To send an HTML email using PHPMailer, you can set the isHTML property of the PHPMailer object to true and include the HTML content of the email in the Body property.

Here’s an example of sending an HTML email with PHPMailer:

use PHPMailer\PHPMailer\PHPMailer;

// Include the PHPMailer autoload file
require_once 'path/to/PHPMailer/src/PHPMailer.php';

// Create a new PHPMailer object
$mail = new PHPMailer();

// Set the SMTP credentials
$mail->isSMTP();
$mail->Host = 'smtp.example.com';
$mail->SMTPAuth = true;
$mail->Username = '[email protected]';
$mail->Password = 'password';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;

// Set the sender and recipient email addresses, subject, and HTML content
$mail->setFrom('[email protected]', 'Sender Name');
$mail->addAddress('[email protected]', 'Recipient Name');
$mail->Subject = 'Test HTML email';
$mail->isHTML(true);
$mail->Body = '<html><body><h1>Hello world!</h1><p>This is an HTML email sent using PHPMailer.</p></body></html>';

// Send the email
if ($mail->send()) {
    echo 'Email sent successfully';
} else {
    echo 'An error occurred while sending the email: ' . $mail->ErrorInfo;
}

In this example, you first include the PHPMailer autoload file and create a new PHPMailer object. You then set the SMTP credentials for your email server, including the SMTP host, username, password, and port. You also set the sender and recipient email addresses, subject, and HTML content of the email using the setFrom, addAddress, Subject, isHTML, and Body properties.

The HTML content of the email is enclosed in the <html> and <body> tags, and includes a heading and a paragraph of text.

Finally, you call the send() method on the PHPMailer object to send the email. If the email is sent successfully, the message “Email sent successfully” will be displayed. If an error occurs while sending the email, the message “An error occurred while sending the email” will be displayed, along with the error message returned by PHPMailer.

Published by

mustafabugra

Systems Engineer

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.