donglian4879 2017-01-05 07:47
浏览 127
已采纳

通过本地smtp服务器发送电子邮件

Code:

<?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';
}
?>

When I run this code, it says message has been sent. However, when I look at my email, there's a notification saying that the email sent through my code was bounced back.

What could be the possible problem? was it the configuration in my code or was it the server?

screencap of the bounce back email

  • 写回答

1条回答 默认 最新

  • douchuanchai2793 2017-01-11 07:39
    关注

    For those who encounter the same problem, what resolved to this problem is smozgur's answer.

    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..

    I contacted supervisor and told him the status of the email function, which is emails are being sent, but the mail server returns the emails to the sender. Also, the possible cause of the problem, which is the local server is not authorized to send email and considered as SPAM. Lastly, the possible solution, which is add the server IP as the SPF record in DNS record.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 想用@vueuse 把项目动态改成深色主题,localStorge里面的vueuse-color-scheme一开始就给我改成了dark,不知道什么原因(相关搜索:背景颜色)
  • ¥15 flask实现搜索框访问数据库
  • ¥15 mrk3399刷完安卓11后投屏调试只能显示一个设备
  • ¥20 白日门传奇少一个启动区服和启动服务器的快捷键,东西都是全的 , 他们说套一个出来就行了 但我就是弄不好,谁看看,
  • ¥100 如何用js写一个游戏云存档
  • ¥15 ansys fluent计算闪退
  • ¥15 有关wireshark抓包的问题
  • ¥15 需要写计算过程,不要写代码,求解答,数据都在图上
  • ¥15 向数据表用newid方式插入GUID问题
  • ¥15 multisim电路设计