php,require 'PHPMailer/PHPMailerAutoload.php';,,$mail = new PHPMailer;,,$mail->isSMTP();,$mail->Host = 'smtp.example.com';,$mail->SMTPAuth = true;,$mail->Username = 'user@example.com';,$mail->Password = 'secret';,$mail->SMTPSecure = 'tls';,$mail->Port = 587;,,$mail->setFrom('from@example.com', 'Mailer');,$mail->addAddress('joe@example.net', 'Joe User');,,$mail->Subject = 'First PHPMailer Message';,$mail->Body    = 'Hi! This is my first e-mail sent through PHPMailer.';,$mail->AltBody = 'This is a body in plain text for non-HTML5 capable clients';,,if(!$mail->send()) {,  echo 'Message could not be sent.';,  echo 'Mailer Error: ' . $mail->ErrorInfo;,} else {,  echo 'Message has been sent';,},“,,在这个例子中,我们首先加载了 PHPMailer 类库,然后创建了一个新的 PHPMailer 对象。然后我们设置了 SMTP 服务器的各种参数,包括主机名、端口、用户名和密码。我们还设置了发件人和收件人,以及邮件的主题和正文。我们尝试发送邮件,并根据结果打印出相应的消息。在PHP中,我们可以使用SMTP(简单邮件传输协议)来发送电子邮件,以下是使用PHP的SMTP发送电子邮件的详细步骤:

创新互联建站-专业网站定制、快速模板网站建设、高性价比民权网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式民权网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖民权地区。费用合理售后完善,10多年实体公司更值得信赖。
1、安装SMTP库
我们需要安装一个支持SMTP的PHP库,这里我们使用的是PHPMailer库,你可以通过以下命令安装:
composer require phpmailer/phpmailer
2、引入SMTP库
在你的PHP文件中,引入刚刚安装的PHPMailer库:
use PHPMailerPHPMailerPHPMailer; use PHPMailerPHPMailerException;
3、创建SMTP实例
接下来,我们需要创建一个PHPMailer实例,并配置SMTP服务器信息:
$mail = new PHPMailer(true);
try {
    //Server settings
    $mail>SMTPDebug = 2;                                 // Enable verbose debug output
    $mail>isSMTP();                                      // Set mailer to use SMTP
    $mail>Host = 'smtp.example.com';                     // Specify main and backup SMTP servers
    $mail>SMTPAuth = true;                               // Enable SMTP authentication
    $mail>Username = 'your_email@example.com';           // SMTP username
    $mail>Password = 'your_email_password';              // SMTP password
    $mail>SMTPSecure = 'tls';                            // Enable TLS encryption, ssl also accepted
    $mail>Port = 587;                                    // TCP port to connect to
} catch (Exception $e) {
    echo "Message could not be sent. Mailer Error: {$mail>ErrorInfo}";
}
4、设置邮件内容
现在,我们需要设置邮件的收件人、发件人、主题和正文:
//Recipients
$mail>setFrom('your_email@example.com', 'Your Name');
$mail>addAddress('recipient@example.com', 'Recipient Name');     // Add a recipient
//Content
$mail>isHTML(true);                                  // Set email format to HTML
$mail>Subject = 'Here is the subject';
$mail>Body    = 'This is the HTML message body in bold!';
$mail>AltBody = 'This is the body in plain text for nonHTML mail clients';
5、发送邮件
调用send()方法发送邮件:
if(!$mail>send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail>ErrorInfo;
} else {
    echo 'Message has been sent';
}
至此,你已经学会了如何在PHP中使用SMTP发送电子邮件,如果你还有其他问题,请随时提问。