tags:

views:

6323

answers:

8

I am using PHP with Apache on Linux, with sendmail. I use the PHP mail() function. The email is sent, but the envelope has the Apache_user@localhostname in MAIL FROM (example [email protected]) and some remote mailservers reject this because the domain doesn't exist (obviously). Using PHP mail(), can I force change the envelope MAIL FROM?

EDIT: If I add a Header in the fourth field of the mail() function, that changes the From field in the headers of the body of the message, and DOES NOT change the envelope MAIL FROM.

I can force it by spawning sendmail with "sendmail -t -odb -oi -frealname@realhost" and piping the email contents to it. Is this a better approach?

Is there a better, simpler, more PHP appropriate way of doing this?

EDIT: The bottom line is I should have RTFM. Thanks for the answers folks, the fifth parameter works and all is well.

A: 

Yes: see the php mail manual page for how to change the header.

Code below works without errors

<?php 

$Name = "Da Duder"; //senders name 
$email = "[email protected]"; //senders e-mail adress 
$recipient = "[email protected]"; //recipient 
$mail_body = "The text for the mail..."; //mail body 
$subject = "Subject for reviever"; //subject 
$header = "From: ". $Name . " <" . $email . ">\r\n"; //optional headerfields 

mail($recipient, $subject, $mail_body, $header); //mail command :) 
?>
warren
That changes the header in the body of the email and does not change the envelope MAIL FROM.
codebunny
http://us3.php.net/manual/en/function.mail.php
warren
that was why I pointed to the manual page... so you could see the example there
warren
A: 

Use the 4th argument to the mail() function - additional headers.

You can set something like "From: \"My Self\" <[email protected]>\r\nReply-to: [email protected]"

Note, some mail servers expect plain "\n"

There's also a 5th argument:

  The additional_parameters parameter can be used to pass an additional parameter to the program configured to use when sending mail using the sendmail_path configuration setting. For example, this can be used to set the envelope sender address when using sendmail with the -f sendmail option.

    The user that the webserver runs as should be added as a trusted user to the sendmail configuration to prevent a 'X-Warning' header from being added to the message when the envelope sender (-f) is set using this method. For sendmail users, this file is /etc/mail/trusted-users.
Greg
That changes the From field in the body of the message, and does not change the envelope MAIL FROM.
codebunny
+8  A: 

mail() has a 4th and 5th parameter (optional). The 5th argument is what should be passed as options directly to sendmail. I use the following:

mail('[email protected]','subject!','body!','From: [email protected]','-f [email protected]');
Lucas Oman
Note: You may need to add the sender to /etc/mail/trusted-users as well.
Devon
Devon: Thanks for pointing that out!
Lucas Oman
A: 

You can try this (im not sure tho):

ini_set("sendmail_from", [email protected]);
mail(...);
ini_restore("sendmail_from");
Joe Scylla
+1  A: 

I would also recommend checking into PHPMailer. It's great for creating and sending email, making the process a lot easier, along with support for SMTP.

Darryl Hein
A: 

Is there a way of sending only name with email for Receipts to see? Julius http://www.hellouganda.com

A: 

sendmail_from is for win32 only please check the sendmail configuration whether it is marked as privileged in apache

lingamurthy
A: 

Thanks Warren! Your input helped me.

Deepak