require_once('phpmailer/PHPMailerAutoload.php');
In addition, SMTP server information is also required. You can choose to use the SMTP service provided by the host service provider or build it yourself. It is necessary to obtain the following information:
SMTP server address
Port number (such as 587)
Login username
Login password
// IntroducedPHPMailerLibrary
require_once('phpmailer/PHPMailerAutoload.php');
// createPHPMailerExample
$mail = new PHPMailer(true);
try {
// set upSMTPServer information
$mail->isSMTP();
$mail->Host = 'your_smtp_server_address';
$mail->SMTPAuth = true;
$mail->Username = 'your_username';
$mail->Password = 'your_password';
$mail->Port = 587;
// Optional debug mode(Enabled during development and debugging)
// $mail->SMTPDebug = 2;
// set up邮件内容
$mail->CharSet = 'UTF-8';
$mail->setFrom('your_email_address');
$mail->addAddress('recipient_email_address');
$mail->Subject = 'Order delivery notice';
$mail->msgHTML('Dear customers,Your order has been delivered。Please pay attention to your package。');
// Send an email
$mail->send();
echo 'The email was sent successfully';
} catch (Exception $e) {
echo 'Email sending failed: ' . $mail->ErrorInfo;
}
The email settings section includes character encoding (UTF-8 is recommended), sender, recipient, email subject and body content. The $mail->send()
method is used to perform sending operations and can obtain error information through exception handling, which is convenient for debugging.
You can call the above send function in the order update logic and dynamically pass in the buyer's email, order information and other content to achieve automated notifications.
This method is flexible in configuration and easy to integrate, and is ideal for most PHP e-commerce systems.