Current Location: Home> Latest Articles> PHP Mail Sending Techniques: Detailed Usage of mail, SMTP, and PHPMailer Functions

PHP Mail Sending Techniques: Detailed Usage of mail, SMTP, and PHPMailer Functions

M66 2025-07-18

Introduction

Email has become an indispensable communication tool in modern society, and it plays an essential role in web development. Email functionalities are widely used in scenarios such as user registration, password resets, system notifications, etc. PHP, as a commonly used scripting language, offers multiple methods for sending emails. This article will provide a detailed explanation of the commonly used PHP email sending functions, including mail, SMTP, and PHPMailer, with relevant code examples to help developers implement email functionalities effectively.

mail Function

Function Overview

The mail function is a built-in PHP function used for sending simple plain-text emails. The basic syntax is as follows:

<span class="fun">bool mail(string $to, string $subject, string $message, string $additional_headers = '', string $additional_parameters = '')</span>

Parameter Description:

  • $to: The recipient's email address. Multiple addresses can be separated by commas.
  • $subject: The subject of the email.
  • $message: The body of the email.
  • $additional_headers: Additional email headers, such as sender, reply-to address, etc.
  • $additional_parameters: Extra parameters, such as SMTP server or authentication details.

Usage Example

Here is an example code that demonstrates how to send a simple plain-text email using the mail function:

<span class="fun">$to = "example@example.com";</span>
<span class="fun">$subject = "Test Email";</span>
<span class="fun">$message = "This is a test email.";</span>
<span class="fun">$headers = "From: sender@example.com";</span>
<span class="fun">if(mail($to, $subject, $message, $headers)) {</span>
<span class="fun">    echo "Email sent successfully!";</span>
<span class="fun">} else {</span>
<span class="fun">    echo "Email sending failed!";</span>
<span class="fun">}</span>

smtp Function

Function Overview

The smtp function uses the SMTP protocol for sending emails, and is suitable for sending more complex emails such as HTML emails and emails with attachments. To use this function, you need to enable the related PHP extensions and configure the SMTP server settings in the PHP configuration file.

Usage Example

Here is an example code that demonstrates how to send an HTML email using the smtp function:

<span class="fun">require 'smtp.php'; // Include the SMTP library</span>
<span class="fun">$smtp = new Smtp();</span>
<span class="fun">$smtp->server = "smtp.example.com";</span>
<span class="fun">$smtp->user = "username";</span>
<span class="fun">$smtp->password = "password";</span>
<span class="fun">$to = "example@example.com";</span>
<span class="fun">$subject = "HTML Email Test";</span>
<span class="fun">$message = "<html><body><h1>This is a test email</h1><p>This is the body of the test email.</p></body></html>";</span>
<span class="fun">$headers = "MIME-Version: 1.0" . "\r\n" . "Content-type:text/html;charset=UTF-8";</span>
<span class="fun">if($smtp->sendmail($to, $subject, $message, $headers)) {</span>
<span class="fun">    echo "Email sent successfully!";</span>
<span class="fun">} else {</span>
<span class="fun">    echo "Email sending failed!";</span>

PHPMailer Library

Library Overview

PHPMailer is a powerful and flexible PHP library for sending emails. It supports sending plain-text emails, HTML emails, and emails with attachments. PHPMailer offers more configuration options and error handling mechanisms, making it one of the most widely used email libraries in PHP development.

Usage Example

Here is an example code that demonstrates how to send a plain-text email using the PHPMailer library:

<span class="fun">require 'PHPMailer/PHPMailer.php'; // Include the PHPMailer library</span>
<span class="fun">$mail = new PHPMailerPHPMailerPHPMailer();</span>
<span class="fun">$mail->isSMTP();</span>
<span class="fun">$mail->Host = "smtp.example.com";</span>
<span class="fun">$mail->SMTPAuth = true;</span>
<span class="fun">$mail->Username = "username";</span>
<span class="fun">$mail->Password = "password";</span>
<span class="fun">$mail->SMTPSecure = "ssl";</span>
<span class="fun">$mail->Port = 465;</span>
<span class="fun">$mail->CharSet = "UTF-8";</span>
<span class="fun">$mail->setFrom("sender@example.com", "Sender");</span>
<span class="fun">$mail->addAddress("example@example.com");</span>
<span class="fun">$mail->Subject = "Plain-text Email Test";</span>
<span class="fun">$mail->Body = "This is a test plain-text email.";</span>
<span class="fun">if($mail->send()) {</span>
<span class="fun">    echo "Email sent successfully!";</span>
<span class="fun">} else {</span>
<span class="fun">    echo "Email sending failed!" . $mail->ErrorInfo;</span>

Conclusion

This article has provided a detailed explanation of the commonly used PHP email sending methods, including the mail function, SMTP protocol, and PHPMailer library, along with actual code examples. These methods allow developers to choose the appropriate way to send simple or complex emails, based on their requirements. We hope this article helps developers better understand and utilize PHP email sending techniques, improving their development efficiency.