通过本地 smtp 服务器发送电子邮件
Sending email through local smtp server
代码:
<?php
require 'PHPMailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->CharSet="utf-8";
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = '172.17.224.12'; // Specify main and backup SMTP servers
$mail->SMTPAuth = false; // Enable SMTP authentication
//$mail->Username = '***'; // SMTP username
//$mail->Password = '***'; // SMTP password
$mail->SMTPSecure = 'TLS'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 25; // TCP port to connect to
$mail->setFrom('itslt@example.com', 'Company.com');
$mail->addReplyTo('itslt@example.com');
$mail->addAddress('dichitslt@example.com'); // Add a recipient
$mail->isHTML(true); // Set email format to HTML
$bodyContent = '<h1>How to Send Email using PHP in Localhost by CodexWorld</h1>';
$bodyContent .= '<p>This is the HTML email sent from localhost using PHP script by <b>CodexWorld</b></p>';
$mail->SMTPDebug = 2;
$mail->Subject = 'Email from Localhost by CodexWorld';
$mail->Body = $bodyContent;
if(!$mail->send()) {
echo 'Message could not be sent.<br>';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
?>
当我运行此代码时,它表示消息已发送。然而,当我查看我的电子邮件时,有一条通知说通过我的代码发送的电子邮件被退回了。
可能是什么问题?是我代码中的配置还是服务器?
screencap of the bounce back email
对于遇到同样问题的人,解决这个问题的是smozgur的回答。
I think the server you are trying to send email through php - your
local server - is not authorized to send email and considered as SPAM
(like someone else is trying to send email by using your credentials).
When I need to make my scripts sends email from the server, then I add
my server IP as the SPF record in the DNS records. That way, my email
server knows that IP is sending email in my knowledge and allows. In
your case, I think you can likely add your IP to temporarily solve the
problem. But you need to do that for the production server IP later as
well. If this is the case of course..
我联系了主管并告诉他电子邮件功能的状态,即正在发送电子邮件,但是邮件服务器returns 将电子邮件发送给发件人。此外,问题的可能原因是本地服务器未被授权发送电子邮件并被视为垃圾邮件。最后,可能的解决方案是将服务器 IP 添加为 DNS 记录中的 SPF 记录。
代码:
<?php
require 'PHPMailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->CharSet="utf-8";
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = '172.17.224.12'; // Specify main and backup SMTP servers
$mail->SMTPAuth = false; // Enable SMTP authentication
//$mail->Username = '***'; // SMTP username
//$mail->Password = '***'; // SMTP password
$mail->SMTPSecure = 'TLS'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 25; // TCP port to connect to
$mail->setFrom('itslt@example.com', 'Company.com');
$mail->addReplyTo('itslt@example.com');
$mail->addAddress('dichitslt@example.com'); // Add a recipient
$mail->isHTML(true); // Set email format to HTML
$bodyContent = '<h1>How to Send Email using PHP in Localhost by CodexWorld</h1>';
$bodyContent .= '<p>This is the HTML email sent from localhost using PHP script by <b>CodexWorld</b></p>';
$mail->SMTPDebug = 2;
$mail->Subject = 'Email from Localhost by CodexWorld';
$mail->Body = $bodyContent;
if(!$mail->send()) {
echo 'Message could not be sent.<br>';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
?>
当我运行此代码时,它表示消息已发送。然而,当我查看我的电子邮件时,有一条通知说通过我的代码发送的电子邮件被退回了。
可能是什么问题?是我代码中的配置还是服务器?
screencap of the bounce back email
对于遇到同样问题的人,解决这个问题的是smozgur的回答。
I think the server you are trying to send email through php - your local server - is not authorized to send email and considered as SPAM (like someone else is trying to send email by using your credentials). When I need to make my scripts sends email from the server, then I add my server IP as the SPF record in the DNS records. That way, my email server knows that IP is sending email in my knowledge and allows. In your case, I think you can likely add your IP to temporarily solve the problem. But you need to do that for the production server IP later as well. If this is the case of course..
我联系了主管并告诉他电子邮件功能的状态,即正在发送电子邮件,但是邮件服务器returns 将电子邮件发送给发件人。此外,问题的可能原因是本地服务器未被授权发送电子邮件并被视为垃圾邮件。最后,可能的解决方案是将服务器 IP 添加为 DNS 记录中的 SPF 记录。