Current Location: Home> Latest Articles> How to Send Emails Using PHP via SMTP Server: Complete Tutorial

How to Send Emails Using PHP via SMTP Server: Complete Tutorial

gitbox 2025-07-01

Introduction

Sending emails is a common requirement in web development. PHP provides various methods to send emails, and using an SMTP (Simple Mail Transfer Protocol) server is one of the most reliable solutions. This article will guide you through how to use PHP to send emails via SMTP, including example code, configuration steps, and common troubleshooting tips.

Choosing an SMTP Server

Before sending emails through an SMTP server, you need to choose a reliable one. Common SMTP providers include Google's Gmail, Microsoft's Outlook, and third-party services like SendGrid. For individual developers, Gmail's SMTP server is a good choice for small projects.

Configuring the SMTP Server

Before you can send emails via SMTP, you need to configure the SMTP server settings. Here are the common SMTP configuration details:

  • SMTP Server Address: For example: smtp.gmail.com
  • SMTP Server Port: Typically 587 (used for TLS encryption)
  • SMTP Username: The username used to log in to the SMTP server
  • SMTP Password: The password used to log in to the SMTP server
  • Sender Email Address: The email address used to send the email

Depending on your chosen SMTP service provider, you can find and confirm these settings in the control panel or settings page, ensuring they are accurate.

Sending Email Using PHP

Once you have configured the SMTP settings, you can use PHP's PHPMailer library to send emails. Below is a PHP code example:


// Include the PHPMailer library
require 'phpmailer/PHPMailerAutoload.php';

// Create a PHPMailer instance
$mail = new PHPMailer;

// Configure SMTP server
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = '[email protected]';
$mail->Password = 'your_password';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;

// Set sender and recipient information
$mail->setFrom('[email protected]', 'Your Name');
$mail->addAddress('[email protected]', 'Recipient Name');

// Set email subject and body
$mail->Subject = 'Hello from PHPMailer';
$mail->Body    = 'This is a test email message sent from PHPMailer.';

// Send email
if (!$mail->send()) {
    echo 'Email sending failed: ' . $mail->ErrorInfo;
} else {
    echo 'Email sent successfully!';
}

With the code above, you can easily send emails using an SMTP server. First, load the PHPMailer library and create an instance. Then, configure the SMTP server's address, port, username, and password. Next, set the sender and recipient information, followed by the subject and body of the email. Finally, call the `send()` method to send the email.

Things to Keep in Mind

When sending emails via an SMTP server, the following points are essential:

  • Ensure the SMTP server configuration is accurate, including the address, port, username, and password.
  • When setting the sender email using `setFrom()`, make sure the email address is authorized by the SMTP server for sending emails.
  • When adding recipients with `addAddress()`, ensure the email addresses are valid.
  • You can also use other methods in PHPMailer to set the email format, attach files, add CC or BCC recipients, and more before sending the email.
  • If the email fails to send, use `$mail->ErrorInfo` to get the specific error message and troubleshoot.

By following these steps, you can easily integrate email sending functionality into your PHP projects, whether it's for website registration confirmation emails, password reset emails, or system notification emails using an SMTP server.