【问题标题】:How to change the "from" header while sending an email from cpanel?从 cpanel 发送电子邮件时如何更改“发件人”标题?
【发布时间】:2017-06-19 15:12:22
【问题描述】:

我正在尝试使用 mail() 函数从我的网站向我的 gmail 帐户发送电子邮件。这工作正常,但问题是,当我在我的 gmail 帐户收件箱(不是垃圾邮件)中收到邮件时,有一个“?”标记而不是常规的空白个人资料图片。当我将鼠标悬停在“?”上时标记,弹出一条消息说,“Gmail 无法验证 mywebsite.com 确实发送了此消息(而不是垃圾邮件发送者)”。我已经添加了这个问题的两张图片。我联系了我的托管服务提供商,他们告诉我这是一个与脚本相关的问题。他们分析了我通过在我的 gmail 收件箱中单击“显示原始文件”获得的文件。他们说,问题的发生是因为 gmail 获得的“发件人”电子邮件地址是“cpanel-username@server-hostname.com”而不是“myemail@website.com”。我必须用“myemail@website.com”替换“cpanel-username@server-hostname.com”。我尝试了很多,但无法更改“来自”标题。我的代码是这样的

$Email = "myemail@gmail.com";
$message = "Hello!!";
$headers = "From: Mywebsite <noreply@mywebsite.com>";

$sentmail = mail("$Email",'Something Important',"$message",$headers);

请注意,“noreply@mywebsite.com”是我在 cpanel 中创建的真实电子邮件地址。我也试过 phpmailer 来解决这个问题,但我没有成功。我的phpmailer代码是这样的

require 'mailer/PHPMailerAutoload.php';

$mail = new PHPMailer;

$mail->isMail();    // I also tried $mail->isSMTP(), but it does not work. Just keeps loading the page but never completes.
$mail->Host = 'localhost';
$mail->SMTPAuth = true;
$mail->Username = 'noreply@mywebsite.com';
$mail->Password = '****************';
$mail->SMTPSecure = 'tls';
$mail->Port = 365;

$mail->setFrom('noreply@mywebsite.com', 'Mywebsite');
$mail->addAddress('myemail@gmail.com');

$mail->isHTML(true);

$mail->Subject = 'Something Important';
$mail->Body    = 'Hello!!';

if(!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent';
}

但这也不能解决我的问题。当在 gmail 中收到邮件时,发件人标头仍然是“cpanel-username@server-hostname.com”。请有人帮我解决这个问题。

【问题讨论】:

  • 您找到解决方案了吗?我也面临同样的问题。
  • 后来我用PHPmailer解决了我的问题

标签: php email gmail


【解决方案1】:

您可以将 -f user@domain.tld 添加到 php.ini 中的 mail.force_extra_parameters 行。 请参阅此处提供的解决方案。它帮助了我。 https://forums.cpanel.net/threads/email-sent-as-username-hostname.601655/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-21
    • 2010-12-20
    • 2020-11-03
    • 2016-01-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多