阅读量:3
在Debian上配置PHP的SMTP邮件发送可以通过多种方式实现,以下是使用sendmail和PHPMailer两种常见方法的步骤:
方法一:使用Sendmail
-
安装Sendmail
sudo apt update sudo apt install sendmail -
配置Sendmail 编辑Sendmail配置文件
/etc/mail/sendmail.cf或创建一个.cf文件并链接到/etc/mail/sendmail.cf。sudo nano /etc/mail/sendmail.cf在文件中添加或修改以下行:
define(`SMART_HOST', `smtp.yourprovider.com')dnl define(`RELAY_MAILER_ARGS', `TCP $h 587')dnl define(`ESMTP_MAILER_ARGS', `TCP $h 587')dnl define(`confAUTH_OPTIONS', `A p')dnl TRUST_AUTH_MECH(`EXTERNAL DIGEST-MD5 CRAM-MD5 LOGIN PLAIN')dnl define(`confAUTH_MECHANISMS', `EXTERNAL GSSAPI DIGEST-MD5 CRAM-MD5 LOGIN PLAIN')dnl -
重启Sendmail
sudo systemctl restart sendmail -
配置PHPMailer 在你的PHP脚本中使用PHPMailer发送邮件:
use PHPMailer\PHPMailer\PHPMailer; use PHPMailer\PHPMailer\SMTP; use PHPMailer\PHPMailer\Exception; require 'path/to/PHPMailer/src/Exception.php'; require 'path/to/PHPMailer/src/PHPMailer.php'; require 'path/to/PHPMailer/src/SMTP.php'; $mail = new PHPMailer(true); try { //Server settings $mail->SMTPDebug = SMTP::DEBUG_SERVER; // Enable verbose debug output $mail->isSMTP(); // Send using SMTP $mail->Host = 'smtp.yourprovider.com'; // Set the SMTP server to send through $mail->SMTPAuth = true; // Enable SMTP authentication $mail->AuthType = SMTP::AUTH_LOGIN; // Authentication type $mail->Port = 587; // TCP port to connect to; use 465 for `SMTPS` $mail->SMTPSecure = SMTP::ENCRYPTION_STARTTLS; // Enable implicit TLS encryption $mail->Username = 'your-email@example.com'; // SMTP username $mail->Password = 'your-password'; // SMTP password //Recipients $mail->setFrom('from@example.com', 'Mailer'); $mail->addAddress('recipient@example.com', 'Joe User'); // 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 non-HTML mail clients'; $mail->send(); echo 'Message has been sent'; } catch (Exception $e) { echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}"; }
方法二:使用PHPMailer直接发送邮件
-
安装PHPMailer
composer require phpmailer/phpmailer -
配置PHPMailer 在你的PHP脚本中使用PHPMailer发送邮件:
use PHPMailer\PHPMailer\PHPMailer; use PHPMailer\PHPMailer\SMTP; use PHPMailer\PHPMailer\Exception; require 'vendor/autoload.php'; $mail = new PHPMailer(true); try { //Server settings $mail->SMTPDebug = SMTP::DEBUG_SERVER; // Enable verbose debug output $mail->isSMTP(); // Send using SMTP $mail->Host = 'smtp.yourprovider.com'; // Set the SMTP server to send through $mail->SMTPAuth = true; // Enable SMTP authentication $mail->AuthType = SMTP::AUTH_LOGIN; // Authentication type $mail->Port = 587; // TCP port to connect to; use 465 for `SMTPS` $mail->SMTPSecure = SMTP::ENCRYPTION_STARTTLS; // Enable implicit TLS encryption $mail->Username = 'your-email@example.com'; // SMTP username $mail->Password = 'your-password'; // SMTP password //Recipients $mail->setFrom('from@example.com', 'Mailer'); $mail->addAddress('recipient@example.com', 'Joe User'); // 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 non-HTML mail clients'; $mail->send(); echo 'Message has been sent'; } catch (Exception $e) { echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}"; }
通过以上两种方法,你可以在Debian上配置PHP的SMTP邮件发送。选择适合你的方法进行配置即可。
以上就是关于“如何在Debian上配置PHP的SMTP邮件发送”的相关介绍,筋斗云是国内较早的云主机应用的服务商,拥有10余年行业经验,提供丰富的云服务器、租用服务器等相关产品服务。云服务器资源弹性伸缩,主机vCPU、内存性能强悍、超高I/O速度、故障秒级恢复;电子化备案,提交快速,专业团队7×24小时服务支持!
简单好用、高性价比云服务器租用链接:https://www.jindouyun.cn/product/cvm