Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP mail() how to set sender mail

Tags:

php

email

This is my code:

$to = '[email protected]';

$subject = 'test';

$body = 'test';

$header  = 'MIME-Version: 1.0' . "\r\n";
$header .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$header .= "To: <$to>" . "\r\n";
$header .= 'From: [email protected] \r\n';

mail($to, $subject, $body, $header);

The code works, it sends the email. But the sender is not the one that I defined. The sender seems to be the webmail host. What am I doing wrong?

like image 924
user1885099 Avatar asked Jul 26 '13 20:07

user1885099


People also ask

How do I set the name of an email sender via PHP?

Just add the -f parameter to provide the sender's email and -F parameter to provide the sender's name to the mail(), all as a string. Assuming that you are using sendmail on a Linux machine: You can find more detailed information by typing "man sendmail" in your shell.

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 is PHP mail configuration?

The php. ini file is where you configure your PHP installation. This is the file you need to edit in order to configure PHP to send mail. You need to ensure that the php. ini file contains details of the mail server that should be used whenever your application sends mail.


1 Answers

Try setting the envelope sender, as well setting the sender in the headers of the message, like so:

$to = "[email protected]";
$from = "[email protected]";
$subject = "subject";
$message = "this is the message body";

$headers = "From: $from"; 
$ok = @mail($to, $subject, $message, $headers, "-f " . $from);   
like image 195
mti2935 Avatar answered Oct 03 '22 22:10

mti2935