Current Location: Home> Latest Articles> How to Send Emails Using PHP: Complete Tutorial and Sample Code

How to Send Emails Using PHP: Complete Tutorial and Sample Code

gitbox 2025-07-15

In web applications, email sending functionality is often used for user registration validation, automated notifications, and other tasks. PHP, being an efficient and widely-used scripting language, provides multiple ways to send emails. In this article, we will walk you through simple code examples to help you learn how to send emails in PHP.

Why Choose PHP for Sending Emails

PHP provides the built-in mail() function, which makes it easy for developers to send emails. The flexibility of PHP and the widespread support for PHP environments on most servers make it an ideal choice for email sending. Whether you're sending notifications, feedback, or validation emails, PHP is an efficient solution.

Basic Setup for PHP Email Sending

To ensure that email sending works smoothly, you'll need to configure your server environment. Typically, the following two steps are required:

  • Enable the PHP mail() function.
  • Configure the server's mail service, especially the SMTP settings.

Sending Emails with the mail() Function

The PHP built-in mail() function is suitable for simple email sending tasks. Here is a basic example:

$to = '[email protected]';
$subject = 'Test Email';
$message = 'This is a test email sent using PHP mail function.';
$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 'Email sending failed.';
}

Sending Emails with PHPMailer

PHPMailer is a powerful library that offers more flexibility than the mail() function, especially with SMTP support and HTML email capabilities. Below are the steps for using PHPMailer to send emails:

Installing PHPMailer

You can install PHPMailer via Composer. Run the following command:

composer require phpmailer/phpmailer

Sample Code for Sending Emails with PHPMailer

Here is an example of sending an email using PHPMailer:

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'vendor/autoload.php';

$mail = new PHPMailer(true);
try {
    $mail->isSMTP();
    $mail->Host = 'smtp.example.com';
    $mail->SMTPAuth = true;
    $mail->Username = '[email protected]';
    $mail->Password = 'yourpassword';
    $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
    $mail->Port = 587;

    $mail->setFrom('[email protected]', 'Mailer');
    $mail->addAddress('[email protected]', 'Joe User');
    $mail->Subject = 'Here is the subject';
    $mail->Body    = '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}";
}

Conclusion

In this tutorial, you have learned how to send emails using PHP. Whether using the built-in mail() function or the more advanced PHPMailer library, both options can meet your needs. Ensure that you configure the correct mail service and server settings to improve the success rate of email sending. If you encounter any issues, you can refer to relevant documentation or seek help from the community.