How to send email using PHP and PHPMAILER?
With the development of the Internet era, email has become an indispensable part of our daily lives. Today, PHP has become a widely used server-side programming language, and with the PHPMAILER library, you can easily send emails. This article will show you how to send emails using PHP and PHPMAILER.
require 'PHPMailer/PHPMailer.php'; require 'PHPMailer/SMTP.php'; require 'PHPMailer/Exception.php'; use PHPMailerPHPMailerPHPMailer; use PHPMailerPHPMailerSMTP; use PHPMailerPHPMailerException;
// 创建一个新的PHPMailer对象 $mail = new PHPMailer(true); try { // 配置SMTP服务器 $mail->IsSMTP(); $mail->Host = 'smtp.example.com'; $mail->SMTPAuth = true; $mail->Username = 'your_email@example.com'; $mail->Password = 'your_password'; $mail->SMTPSecure = 'ssl'; $mail->Port = 465; // 设置发件人和收件人 $mail->setFrom('your_email@example.com', 'Your Name'); $mail->addAddress('recipient@example.com', 'Recipient Name'); // 设置邮件主题和内容 $mail->Subject = 'Test Email'; $mail->Body = 'This is a test email sent from PHPMAILER.'; // 添加附件(可选) //$mail->addAttachment('path_to_file'); // 发送邮件 $mail->send(); echo 'Email sent successfully!'; } catch (Exception $e) { echo 'Email could not be sent. Error: ', $mail->ErrorInfo; }
In the above code, you need to replace the SMTP server configuration information with the actual values. In addition, you can customize the subject, content, attachments, etc. of the email.
Summary:
By using PHP and the PHPMAILER library, you can easily send emails. It only takes a few steps to configure the SMTP server, set the sender, recipients and email content. The PHPMAILER library provides many other functions and options, such as adding attachments, HTML emails, etc. You can further study and practice as needed. Wish you success in sending emails using PHPMAILER!
The above is the detailed content of How to send email using PHP and PHPMAILER?. For more information, please follow other related articles on the PHP Chinese website!