【问题标题】:PHP PEAR send_mail Failed to set sender with name and emailPHP PEAR send_mail 无法使用名称和电子邮件设置发件人
【发布时间】:2012-03-09 04:46:09
【问题描述】:

我正在尝试使用此脚本发送电子邮件

$to = 'example@example.com';
$headers['To'] = $to;
$headers['From'] = '"My Name" <examlpe@example.com>';
$headers['Return-Path'] = 'examlpe@example.com';
$headers['Subject'] = 'Subject';

$auth = array('host' => MAIL_HOST, 'auth' => true, 'username' => MAIL_USER, 'password' => MAIL_PASS);
$smtp = Mail::factory('smtp', $auth);
$mail = $smtp->send($to, $headers, $message);

if (PEAR::isError($mail))
    echo('<p>PEAR mail: '.$mail->getMessage().'</p>');
else
    echo('<p>PEAR mail: Message successfully sent!</p>');

但我收到以下消息:

Failed to set sender: "My Name" <example@example.com> [SMTP: Invalid response code received from server (code: 501, response: <"My Name" <example@example.com>>: "@" or "." expected after ""My Name"")]

当我像这样创建 from 字段时:

$headers['From'] = 'examlpe@example.com';

它工作正常,我收到了电子邮件。

如何发送带有发件人姓名的电子邮件?

【问题讨论】:

  • 你试过无引号吗? My Name &lt;example@example.com&gt; 也是一个有效的邮箱。

标签: php email pear sendmail


【解决方案1】:

Return-Path 和 Reply-To 不一样。

Return-Path:如果您想覆盖电子邮件的信封发件人,请设置 Return-Path 标头,然后将使用该值而不是 From: 标头(来自 PEAR 网站)的值。您不能为 Return-Path 设置名称,只能设置电子邮件地址。

回复:让收件人回复此地址。您可以设置姓名和电子邮件地址。

因此,将 Return-Path 替换为 Reply-To 即可。

【讨论】:

    【解决方案2】:

    为什么需要在 From 中使用电子邮件地址。我建议您应该添加“回复”标题并在此处提供电子邮件地址,并在 From 中指定名称。

    【讨论】:

    • 我需要发件人中的电子邮件地址,因为没有它,它不会发送。但我的错误是我虽然 Return-Path 是 Reply-To 并且它会导致错误。当我用回复替换返回路径时,它可以工作。
    猜你喜欢
    • 2013-03-03
    • 2014-01-05
    • 2017-07-13
    • 2021-04-13
    • 1970-01-01
    • 2017-01-31
    • 2011-10-07
    • 2011-05-11
    相关资源
    最近更新 更多