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条)

报告相同问题?

悬赏问题

  • ¥15 编写vba代码实现数据录入工作
  • ¥15 做过TCL海信电视小米电视相关影视会员软件私我
  • ¥15 Mapreduce是正常的,在运行其他jar包时并没有任何问题,只是在做LogCount.jar 时出的问题。如图所示
  • ¥15 ImportError: DLL load failed while importing _iterative: 找不到指定的模块。
  • ¥15 如何通过交互分析得出某高危患者对放疗获益更多
  • ¥15 相关性分析中,p<0.05, r=0.29,怎么评价相关性呢
  • ¥15 docker部署Mongodb后输入命令报错?
  • ¥15 将下列流程图转变成python程序代码
  • ¥15 我需要全国每个城市的最新小区名字等数据。
  • ¥15 开发一个小区生态的小程序