📜  邮件 php 发送 - PHP (1)

📅  最后修改于: 2023-12-03 15:12:27.533000             🧑  作者: Mango

邮件 PHP 发送 - PHP

Sending emails from a PHP application can be a crucial part of any web project. In this article, we will explore how to send email using the PHP mail() function and the popular PHPMailer library.

Sending email with PHP's mail() function

PHP provides a built-in function mail() which can be used to send emails from a PHP application. The mail() function takes three mandatory parameters and two optional parameters:

bool mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )
  • $to : The email address(es) to which the email will be sent. Multiple email addresses can be separated by commas.
  • $subject: The subject of the email.
  • $message: The body of the email.
  • $additional_headers (optional): Additional headers that can be added to the email headers.
  • $additional_parameters (optional): Additional parameters that can be passed to the mailer.

Here is an example code snippet to send an email using the mail() function in PHP:

$to = 'recipient@example.com';
$subject = 'Hello from PHP';
$message = 'This is a test email from PHP';

$headers = "From: sender@example.com\r\n";
$headers .= "Reply-To: sender@example.com\r\n";
$headers .= "Content-Type: text/plain\r\n";

mail($to, $subject, $message, $headers);

Note that the $headers parameter is optional but it is a good practice to include the From and Reply-To headers. Also, you should specify the content type of the message using the Content-Type header, otherwise, the message may not display correctly in the recipient's email client.

Sending email with PHPMailer library

PHPMailer is a powerful and popular PHP library for sending email. It supports various features such as sending attachments, HTML messages, SMTP authentication, etc.

To use PHPMailer in your PHP application, you need to first download the library and include it in your project. You can download the library from its official GitHub repository:

PHPMailer on GitHub

Here is an example code snippet to send an email using PHPMailer:

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'path/to/PHPMailer/src/PHPMailer.php';
require 'path/to/PHPMailer/src/SMTP.php';
require 'path/to/PHPMailer/src/Exception.php';

$mail = new PHPMailer(true);

try {
    //Server settings
    $mail->SMTPDebug = 0;                                        // Enable verbose debug output
    $mail->isSMTP();                                             // Send using SMTP
    $mail->Host       = 'smtp.example.com';                      // Set the SMTP server to send through
    $mail->SMTPAuth   = true;                                     // Enable SMTP authentication
    $mail->Username   = 'user@example.com';                       // SMTP username
    $mail->Password   = 'secret';                                 // SMTP password
    $mail->SMTPSecure = 'tls';                                    // Enable TLS encryption
    $mail->Port       = 587;                                      // TCP port to connect to

    //Recipients
    $mail->setFrom('sender@example.com', 'Sender Name');
    $mail->addAddress('recipient@example.com', 'Recipient Name');     // Add a recipient

    // Attachments
    $mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
    $mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name

    // Content
    $mail->isHTML(true);                                  // Set email format to HTML
    $mail->Subject = 'Hello from PHPMailer';
    $mail->Body    = 'This is a test email from PHPMailer';

    $mail->send();
    echo 'Message has been sent';
} catch (Exception $e) {
    echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}

As you can see in the above code, we have included the required PHPMailer classes and set up the SMTP server settings. We have also added a recipient and attachments to the email. Finally, we used the send() method of the PHPMailer object to send the email.

Conclusion

Sending emails from a PHP application can be done using the built-in mail() function or the PHPMailer library. The PHPMailer library provides more features and flexibility, and is generally recommended for complex email sending scenarios.