如何让php邮件识别From地址

时间:2014-03-27 17:54:13

标签: php sendmail

我有一个包含这些代码段的邮件脚本:

$headers['From']        = 'Server <valid_account@mydomain.com>';
$headers['To']          = $to;
$headers['Subject']     = 'Mail Test from Server';
$headers['Reply-To']    = 'no-reply@mydomain.com';
$headers['Bcc']         = '';
$headers['Return-Path'] = 'valid_account@mydomain.com';
...
$mail_object =& Mail::factory('mail');
$mail_object->send($recipient, $headers, $body);
...
// Define SMTP Parameters
$params['host']     = 'mail.mydomain.com';
$params['port']     = '25';
$params['auth']     = 'PLAIN';
$params['username'] = 'valid_account@mydomain.com';
$params['password'] = 'abcdefgh';

以下是传入消息的邮件标题:

Received: from nobody by vps.mydomain.com with local (Exim 4.82)
    (envelope-from <nobody@vps.mydomain.com>)   id 1WTE9v-0002NR-C8; Thu, 27
 Mar 2014 13:32:07 -0400
To: <myaccount@yahoo.com>
Subject: Mail Test from Server
From: Server <valid_account@mydomain.com>
Reply-To: <no-reply@mydomain.com>
Message-ID: <E1WTE9v-0002NR-C8@vps.mydomain.com>
Sender: Nobody <nobody@vps.mydomain.com>
Date: Thu, 27 Mar 2014 13:32:07 -0400
X-AntiAbuse: This header was added to track abuse, please include it with any abuse report
X-AntiAbuse: Primary Hostname - vps.mydomain.com
X-AntiAbuse: Originator/Caller UID/GID - [99 99] / [47 12]
X-AntiAbuse: Sender Address Domain - vps.mydomain.com
X-Get-Message-Sender-Via: vps.mydomain.com: uid via acl_c_vhost_owner from authenticated_id: nobody from /only user confirmed/virtual account not confirmed
X-PM-Spam-Prob: : 9%
MIME-Version: 1.0
Content-Type: text/plain
Return-Path: nobody@vps.mydomain.com

我的问题/我的需要

如何更改邮件脚本以显示正确的地址? s / b Server <valid_account@mydomain.com>而非Nobody <nobody@vps.mydomain.com>

3 个答案:

答案 0 :(得分:2)

如果您使用的是Linux(使用sendmail后端mail()),请在构建期间将-fvalid_account@mydomain.com传递给Mail对象,如下所示:

// and any other params you'd like...
$params = array();
$params["sendmail_args"] = "-fvalid_account@mydomain.com";

$mail_object =& Mail::factory('sendmail', $params);
$mail_object->send($recipient, $headers, $body);

请注意,后端已更改为sendmail。我建议设置后端,而不是使用mail()。请在此处查看完整列表:http://pear.php.net/manual/en/package.mail.mail.factory.php

根据您的编辑,以下是SMTP后端所需的内容:

$params = array();
$params['host']     = 'mail.mydomain.com';
$params['port']     = '25';
$params['auth']     = 'PLAIN';
$params['username'] = 'valid_account@mydomain.com';
$params['password'] = 'abcdefgh';
// and any other params you'd like...

$mail_object =& Mail::factory('smtp', $params);
$mail_object->send($recipient, $headers, $body);

答案 1 :(得分:0)

我使用php邮件解决了这个问题

$headers = 'From: Server <valid_account@mydomain.com>\r\n';
$headers .= 'Return-Path: Server <valid_account@mydomain.com>\r\n';
$headers .= "Reply-To: valid_account@mydomain.com\r\n";
$headers .= "X-Mailer: PHP/" . phpversion();
mail($to, $subject, $headers, $body, "-fvalid_account@mydomain.com");

请注意邮件功能的第5个参数。

希望这有帮助。

答案 2 :(得分:-1)

您好使用PHP邮件功能

$headers = 'Server <valid_account@mydomain.com>';
$to = 'test@test.com';
$message = 'My message';
$subject  = 'Mail Test from Server';

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

希望这会对你有所帮助。