Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change envelope from address using PHP mail?

Tags:

php

email

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 mail servers reject this because the domain doesn't exist (obviously). Using mail, can I force it to 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 RTM. Thanks for the answers folks, the fifth parameter works and all is well.

like image 228
codebunny Avatar asked Oct 07 '08 15:10

codebunny


People also ask

How can I change my address in PHPMailer?

The relevant line in Theolodis answer is: $mail->SetFrom('[email protected]', 'First Last'); There is no need to use AddReplyTo() this is something completely different. You only need to set your from address (and name optionally) by using SetFrom() .

What is PHP mail () function?

PHP mail() function is used to send email in PHP. You can send text message, html message and attachment with message using PHP mail() function.

How does PHP mail send email?

PHP makes use of mail() function to send an email. This function requires three mandatory arguments that specify the recipient's email address, the subject of the the message and the actual message additionally there are other two optional parameters. mail( to, subject, message, headers, parameters );

What mail server does PHP mail use?

Php uses by default, the local mail-server.


1 Answers

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]'); 
like image 152
Lucas Oman Avatar answered Oct 05 '22 20:10

Lucas Oman