【问题标题】:Is it possible to set the "message To:" header when sending email in PHP?是否可以在 PHP 中发送电子邮件时设置“message To:”标头?
【发布时间】:2012-02-18 05:10:45
【问题描述】:

这是一个相对简单的问题,可能只是没有解决方案。

是否可以设置 message To: 标头并用 PHP 发送电子邮件?请注意,message To:envelope To: 标头不同。后者实际上决定了电子邮件的路由位置,而前者只是决定了它在收件人的电子邮件程序中显示的内容。

背景:我正在为自己设置一个小型一次性电子邮件服务,因此我正在接收带有 PHP 脚本的电子邮件,修改一些标题,然后将其重新发送到我的真实电子邮件地址。当我在我的真实邮箱中收到邮件时,我希望原始收件人电子邮件地址(一次性电子邮件地址)仍然显示在邮件中(用于客户端过滤规则等)。

这可能吗?我已经能够修改所有其他类型的标题,但是这个让我卡住了。

【问题讨论】:

  • 我看了看,在任何地方都找不到任何“发给”的标题。你有某种文档的链接吗?
  • @Mike - 我想this would help。标题为“信封标头”的部分了解我在说什么,即信封标头与邮件标头。
  • 我认为您正在寻找 To 标头,而不是 message To。请注意您链接到的文章说 "To: The "message To:" 如上所述。注意 To: 标头不需要包含收件人的地址!"
  • 收件人:“显示名称”
  • 嗯...所以看来我误解了标题的措辞。它显然被称为“To:”标题。然而,问题在于 PHP 的 mail() 函数将“To:”标头解释为预期的收件人,并将电子邮件发送到该地址。我想要做的是将它发送到一个地址,让它看起来好像它是给另一个地址的(因为它最初是,我基本上只是转发它)。

标签: php email


【解决方案1】:

我在这里使用这样的东西:

$message = 'Your Message';
$subject = 'Your Subject';
$headers = 'Your headers';
$to = 'Address to send to';
$headers = 'Reply-To: Address'; //Perhaps you are looking for this?

mail($to,$subject,$message,$headers);

希望这对你有用!

编辑:抱歉误解了您的问题。

【讨论】:

  • 对,但是该函数中的 $to 设置了“信封到”标头,该标头确定了消息的路由,即发送到哪个地址。我已经准备好了。我现在要做的是修改“message to”标头,即显示在收件人邮件程序的“To:”字段中的地址。诚然,大多数人不知道这两个标题之间存在差异。
【解决方案2】:

当使用 PHP 发送电子邮件时,我一直使用一个使用 STMP 的自定义函数,并且在该函数中,它使用以下标头来设置我相信您尝试设置的名称:

"To: $nameto <$emailto>"

其中 $emailto 是您要发送到的实际电子邮件,而 $nameto 是您希望它显示给谁。

这是我使用的函数,如果您需要更改任何其他标题,您可以修改它们,它们在函数的末尾:

/*
    Desc: Used to send emails from stmp server. Make sure to have the config variables set (at the top of function). Stores logs in array, you'll need to modify the function to actually use the array though.
    Params:
        String $from - Email address that the email is from. Ex. "john.smith@example.com"
        String $namefrom - Name to go along with the email address. Ex. "John Smith"
        String $to - Email address of the recipient of the email. Ex, "jane.doe@example.com"
        String $nameto - Name to go along with the email address. Ex. "Jane Doe"
        String $subject - Email subject.
        String $message - Main contents of email.
    Return: true on success false on failure.
*/
function amail($from, $namefrom, $to, $nameto, $subject, $message)
{
    $smtpServer = "";
    $port = 0;
    $username = "";
    $password = "";
    $timeout = 30;
    $localhost = "127.0.0.1";
    $newLine = "\r\n";
    $smtpConnect = fsockopen($smtpServer, $port, $errno, $errstr, $timeout) or die('Could not connect.');
    $smtpResponse = fgets($smtpConnect, 515);
    if(empty($smtpConnect)){
        return false;
    } else {
        $logArray['connection'] = "Connected: $smtpResponse";
    }
    fputs($smtpConnect, "HELO $localhost" . $newLine);
    $smtpResponse = fgets($smtpConnect, 515);
    $logArray['heloresponse'] = "$smtpResponse";
    fputs($smtpConnect,"AUTH LOGIN" . $newLine);
    $smtpResponse = fgets($smtpConnect, 515);
    $logArray['authrequest'] = "$smtpResponse";
    fputs($smtpConnect, base64_encode($username) . $newLine);
    $smtpResponse = fgets($smtpConnect, 515);
    $logArray['authusername'] = "$smtpResponse";
    fputs($smtpConnect, base64_encode($password) . $newLine);
    $smtpResponse = fgets($smtpConnect, 515);
    $logArray['authpassword'] = "$smtpResponse";
    fputs($smtpConnect, "MAIL FROM: $from" . $newLine);
    $smtpResponse = fgets($smtpConnect, 515);
    $logArray['mailfromresponse'] = "$smtpResponse";
    fputs($smtpConnect, "RCPT TO: $to" . $newLine);
    $smtpResponse = fgets($smtpConnect, 515);
    $logArray['mailtoresponse'] = "$smtpResponse";
    fputs($smtpConnect, "DATA" . $newLine);
    $smtpResponse = fgets($smtpConnect, 515);
    $logArray['data1response'] = "$smtpResponse";
    $headers = "MIME-Version: 1.0" . $newLine;
    $headers .= "Content-type: text/plain; charset=iso-8859-1" . $newLine;
    $headers .= "To: $nameto <$to>" . $newLine;
    $headers .= "From: $namefrom <$from>" . $newLine;
    $headers .= "Subject: $subject" . $newLine;
    fputs($smtpConnect, "$headers\n\n$message\n".$newLine.".".$newLine);
    $smtpResponse = fgets($smtpConnect, 1024);
    $logArray['data2response'] = "$smtpResponse";
    fputs($smtpConnect,"QUIT" . $newLine);
    $smtpResponse = fgets($smtpConnect, 515);
    $logArray['quitresponse'] = "$smtpResponse";
    return true;
}

【讨论】:

    【解决方案3】:

    感谢大家的帮助!我找到了一个很好的解决方案,所以我想分享一下。

    我最终只是扩展了 CodeIgniter 的电子邮件类以设置一个 To 标头(在此之前我将开始称它为虚标标头,因为它只是看起来没有实质内容)并通过 SMTP 发送。如果您想查看我所做的一个简单示例,请使用您自己的 MY_Email.php 文件扩展 Email 类(请参阅用户指南中的 Extending Native Libraries 部分)并从 Email 类中复制 _build_headers() 函数。

        /**
     * Build final headers
     *
     * @access  protected
     * @param   string
     * @return  string
     */
    protected function _build_headers()
    {
        $this->_set_header('X-Sender', $this->clean_email($this->_headers['From']));
        $this->_set_header('X-Mailer', $this->useragent);
        $this->_set_header('X-Priority', $this->_priorities[$this->priority - 1]);
        $this->_set_header('Message-ID', $this->_get_message_id());
        $this->_set_header('Mime-Version', '1.0');
    }
    

    然后在 Mime-Version 行的正下方添加您自己的标题,如下所示:

        $this->_set_header('To', 'person@example.com');
    

    然后只需向您自己的一个电子邮件地址发送一封常规电子邮件。您会注意到,即使电子邮件到达您的地址,它似乎也是发送到 person@example.com。

    轰隆隆。

    如果您想实际使用我创建的完整 MY_Email.php 文件,为您自己的 CodeIgniter 项目添加“Vanity To”选项,see this gist

    【讨论】:

      猜你喜欢
      • 2015-05-13
      • 1970-01-01
      • 2018-08-29
      • 2011-07-30
      • 1970-01-01
      • 2019-01-31
      • 2016-04-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多