H. Ferrence
H. Ferrence

Reputation: 8106

How to get php mail to recognize From address

I have a mail script with these code snippets:

$headers['From']        = 'Server <[email protected]>';
$headers['To']          = $to;
$headers['Subject']     = 'Mail Test from Server';
$headers['Reply-To']    = '[email protected]';
$headers['Bcc']         = '';
$headers['Return-Path'] = '[email protected]';
...
$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'] = '[email protected]';
$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 <[email protected]>)   id 1WTE9v-0002NR-C8; Thu, 27
 Mar 2014 13:32:07 -0400
To: <[email protected]>
Subject: Mail Test from Server
From: Server <[email protected]>
Reply-To: <[email protected]>
Message-ID: <[email protected]>
Sender: Nobody <[email protected]>
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: [email protected]

My Question / My Need

How do I change my mail script to show the proper from address? s/b Server <[email protected]> and not Nobody <[email protected]>

Upvotes: 0

Views: 487

Answers (3)

Taj Morton
Taj Morton

Reputation: 1638

If you're on Linux (using sendmail backend to mail()), pass [email protected] to the Mail object during construction like this:

// and any other params you'd like...
$params = array();
$params["sendmail_args"] = "[email protected]";

$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'] = '[email protected]';
$params['password'] = 'abcdefgh';
// and any other params you'd like...

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

Upvotes: 2

Nicoli
Nicoli

Reputation: 914

I resolved this using php mail

$headers = 'From: Server <[email protected]>\r\n';
$headers .= 'Return-Path: Server <[email protected]>\r\n';
$headers .= "Reply-To: [email protected]\r\n";
$headers .= "X-Mailer: PHP/" . phpversion();
mail($to, $subject, $headers, $body, "[email protected]");

Please note the 5th parameter on the mail function.

Hope this helps.

Upvotes: 0

Harish Kanakarajan
Harish Kanakarajan

Reputation: 685

Hi use the PHP mail function,

$headers = 'Server <[email protected]>';
$to = '[email protected]';
$message = 'My message';
$subject  = 'Mail Test from Server';

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

Hope this helps you.

Upvotes: -1

Related Questions