douwen5924 2014-03-27 17:54
浏览 73
已采纳

如何让php邮件识别From地址

I have a mail script with these code snippets:

$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';

Here are the mail headers from the incoming message:

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

My Question / My Need

How do I change my mail script to show the proper from address? s/b Server <valid_account@mydomain.com> and not Nobody <nobody@vps.mydomain.com>

  • 写回答

3条回答 默认 最新

  • duafagm1066 2014-03-27 18:15
    关注

    If you're on Linux (using sendmail backend to mail()), pass -fvalid_account@mydomain.com to the Mail object during construction like this:

    // 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);
    

    Note that the backend was changed to sendmail. I'd recommend setting the backend, instead of using mail(). See the full list here: http://pear.php.net/manual/en/package.mail.mail.factory.php

    Based on your edit, here's what you need for the SMTP backend:

    $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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?