douwen5924
2014-03-27 17:54 阅读 63
已采纳

如何让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 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);
    
    点赞 评论 复制链接分享
  • donglao9606 donglao9606 2014-03-27 17:58

    Hi use the PHP mail function,

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

    Hope this helps you.

    点赞 评论 复制链接分享
  • doufei2662 doufei2662 2014-03-27 18:10

    I resolved this using php mail

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

    Please note the 5th parameter on the mail function.

    Hope this helps.

    点赞 评论 复制链接分享