dongzhi4498 2014-09-24 08:33
浏览 26
已采纳

PHPMailer没有使用正确的'from'地址发送

I'm using PHPMailer linked to my gmail account.

I've required the autoloader, created a function to handle sending mail, then fire the function to test. It all looks like this:

require "PHPMailer/PHPMailerAutoload.php";

function sendMail($to, $subject, $body, $from){

  //init PHPMailer
  $mail = new PHPMailer;
  $mail->isSMTP();
  $mail->SMTPAuth = true;
  $mail->SMTPSecure = "tls";
  $mail->Port = 587;
  $mail->isHTML(true);

  //connection settings
  $mail->Host = "smtp.gmail.com";
  $mail->Username = "xxx@xxx.com";
  $mail->Password = "xxxxxxxx";

  //addresses
  $mail->addAddress($to);
  $mail->setFrom($from);

  //create email
  $mail->Subject = $subject;
  $mail->Body = $body;

  //send email
  $mail->send();
}

//sendMail(to, subject, body, from)
sendMail("xxxx@xxxxx.com", "Test Subject", "Test body", "xxxxx@xxxxx.com");

This sends the email with the correct subject and body, to the right place, but it ignores the 'from' address and the email always says it comes from the gmail account through which the email is passed. Is there anyway to configure this to work?

Note I have obviously omitted the correct account details and real to or from addresses from the code above.

  • 写回答

2条回答 默认 最新

  • dsa88886666 2014-09-24 09:41
    关注

    It's very simple: By default, Gmail doesn't allow you to set the from address to a non-gmail address unless they are handling your domain; they will rewrite the from address to be your gmail address, exactly as you are seeing. This is nothing to do with PHPMailer nor the PHP mail function.

    There is provision to set up specific from addresses (rather than whole domains), but you have to set them up beforehand; you can't just send from random addresses. See this answer.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作