I have a simple mail function:
$to = 'someone#somedomain.com';
$cc = 'someother#somedomain.com';
$bcc = 'someotherother#somedomain.com';
$sub = 'Some Subject';
$body = $html;
$headers[] = "MIME-Version: 1.0";
$headers[] = "Content-type: text/html; charset=iso-8859-1";
$headers[] = "From: Fancy Name <from#email.com>";
$headers[] = "Reply-To: Fancy Name <from#email.com>";
$headers[] = 'CC: '. $cc;
$headers[] = 'BCC: '. $bcc;
mail($to, $sub, $body, implode("\n", $headers));
this works on windows but not on CentOS - the logs return "Envelope From address apache#host is't allowed" (or something similar).
Looked into that and as a 5th param you can pass -f from#email.com and this does work. But it doesn't show as Fancy Name, it shows the email address - how can I either get headers to work properly or pass a name to the -f param?
Thanks
Found a solution:
do the headers as your would and in the 5th param do -f email#domain -F "Sender Name" e.g.
$to = 'someone#somedomain.com';
$cc = 'someother#somedomain.com';
$bcc = 'someotherother#somedomain.com';
$sub = 'Some Subject';
$body = $html;
$headers[] = "MIME-Version: 1.0";
$headers[] = "Content-type: text/html; charset=iso-8859-1";
$headers[] = "From: Fancy Name <from#email.com>";
$headers[] = "Reply-To: Fancy Name <from#email.com>";
$headers[] = 'CC: '. $cc;
$headers[] = 'BCC: '. $bcc;
mail($to, $sub, $body, implode("\n", $headers), '-f from#email.com -F "Fancy Name"');
try to run this snipt of code and let me know
mail ('user#to-email.com', "Testing Mail Server", "This is a test email from CentOS web server", null, "-f user#from-email.com");
Related
I want to send the email to the email subscriber , here is my approach
$msg= file_get_contents("activate-mailer.html");
$from = $_POST['sunscribeemail']; // this is the sender's Email address
$subject = "Email Subscription for the news letter ";
$message = from . " " . Subscribe for the news letter:" . "\n\n" ;
$headers = array();
$headers[] = "MIME-Version: 1.0";
$headers[] = "Content-type: text/plain; charset=iso-8859-1";
$headers[] = "From: " . $from;
$headers[] = "Reply-To: " . $from;
$headers[] = "X-Mailer: PHP/".phpversion();
mail($to,$subject,$msg,implode("\r\n", $headers));
Email sent to subscriber successfully but it is sending html as text like <html>..</html> but i want to render this html to the email body.
Simply change the Content-Type from text to html
$headers[] = "Content-type:text/html;charset=UTF-8";
While I am sending any mail, this will return only name: from: message: subject : , but all these fields remain blank..
<?php
$name = #trim(stripslashes($_POST['name']));
$from = #trim(stripslashes($_POST['email']));
$subject = #trim(stripslashes($_POST['subject']));
$message = #trim(stripslashes($_POST['message']));
$to = 'me#example.com';
$headers = array();
$headers[] = "MIME-Version: 1.0";
$headers[] = "Content-type: text/plain; charset=iso-8859-1";
$headers[] = "From: {$name} <{$from}>";
$headers[] = "Reply-To: <{$from}>";
$headers[] = "Subject: {$subject}";
$headers[] = "X-Mailer: PHP/".phpversion();
mail($to, $subject, $message, $headers);
die;
Without knowing your environment, mail function accepts the headers argument as a string, not an array.
mail($to, $subject, $message, implode("\r\n", $headers));
http://php.net/manual/en/function.mail.php
This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 7 years ago.
This is my sendemail.php. It is not working. Are there any mistakes on it ?
<?php
$name = #trim(stripslashes($_POST['name']));
$from = #trim(stripslashes($_POST['email']));
$subject = #trim(stripslashes($_POST['subject']));
$message = #trim(stripslashes($_POST['message']));
$to = 'richardhenokh#gmail.com';//replace with your email
$headers = array();
$headers[] = "MIME-Version: 1.0";
$headers[] = "Content-type: text/plain; charset=iso-8859-1";
$headers[] = "From: {$name} <{$from}>";
$headers[] = "Reply-To: <{$from}>";
$headers[] = "Subject: {$subject}";
$headers[] = "X-Mailer: PHP/".phpversion();
mail($to, $subject, $message, $headers);
die;
try this. It is working fine
<?php
$name = #trim(stripslashes($_POST['name']));
$from = #trim(stripslashes($_POST['email']));
$subject = #trim(stripslashes($_POST['subject']));
$message = #trim(stripslashes($_POST['message']));
$to = 'imaphpdeveloper#gmail.com';//replace with your email
$headers = "MIME-Version: 1.0";
$headers .= "Content-type: text/plain; charset=iso-8859-1";
$headers .= "From: {$name} <{$from}>";
$headers .= "Reply-To: <{$from}>";
$headers .= "Subject: {$subject}";
$headers .= "X-Mailer: PHP/".phpversion();
mail($to, $subject, $message, $headers);
die;
?>
Also you can try PHPmailer. headers should be a string not an array. Also {$from} etc.. in headers should be replaced properly. Now using above code mail is sending. But have to replace {$from} properly.
I'm trying to use the below code to send mail, but got the below error
Doesn't mail() take in arrays for headers?
Warning: mail() expects parameter 4 to be string, array given in ../email.php on line 16
code :
<?php
$name = #trim(stripslashes($_POST['name']));
$from = #trim(stripslashes($_POST['email']));
$subject = #trim(stripslashes($_POST['subject']));
$message = #trim(stripslashes($_POST['message']));
$to = 'example#gmail.com';
$headers = array();
$headers[] = "MIME-Version: 1.0";
$headers[] = "Content-type: text/plain; charset=iso-8859-1";
$headers[] = "From: {$name} <{$from}>";
$headers[] = "Reply-To: <{$from}>";
$headers[] = "Subject: {$subject}";
$headers[] = "X-Mailer: PHP/".phpversion();
mail($to, $subject, $message, $headers);
PHP mail() need string $to, string $subject, string $message, string $headers
if you want to use an array for the headers
mail($to, $subject, $message, implode("\r\n", $headers));
else change your code in
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/plain; charset=iso-8859-1\r\n";
$headers .= "From: {$name} <{$from}>\r\n";
$headers .= "Reply-To: <{$from}>\r\n";
$headers .= "Subject: {$subject}\r\n";
$headers .= "X-Mailer: PHP/".phpversion()."\r\n";
why \r\n (from mail() documentation)?
Multiple extra headers should be separated with a CRLF (\r\n) [...]
If messages are not received, try using a LF (\n) only. Some Unix mail
transfer agents (most notably » qmail) replace LF by CRLF
automatically (which leads to doubling CR if CRLF is used). This
should be a last resort, as it does not comply with » RFC 2822.
side note use "\r\n" and not '\r\n'
As the message says the 4th parameter ($additional_headers) should be a string. So you need to join the array elements:
$headers = implode("\r\n", $headers);
mail($to, $subject, $message, $headers);
From the documentation:
String to be inserted at the end of the email header.
This is typically used to add extra headers (From, Cc, and Bcc). Multiple extra headers should be separated with a CRLF (\r\n). If outside data are used to compose this header, the data should be sanitized so that no unwanted headers could be injected.
You make $headers to be a array, but it has to be a string! So just change these lines:
$headers = array();
$headers[] = "MIME-Version: 1.0";
$headers[] = "Content-type: text/plain; charset=iso-8859-1";
$headers[] = "From: {$name} <{$from}>";
$headers[] = "Reply-To: <{$from}>";
$headers[] = "Subject: {$subject}";
$headers[] = "X-Mailer: PHP/".phpversion();
to this and concatenate the strings together:
$headers = "";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/plain; charset=iso-8859-1\r\n";
$headers .= "From: {$name} <{$from}>\r\n";
$headers .= "Reply-To: <{$from}>\r\n";
$headers .= "Subject: {$subject}\r\n";
$headers .= "X-Mailer: PHP/" . phpversion() . "\r\n";
I am unable to set header in php for from address I have tried all possible values :
The code below is correct but code is not attached to header .
It shows this as default from but does not change.
From : webhost:cpanel#hosting
Is this the problem with cpanel ?
Please help me out really appreciate the help.
Thanks in Advance.
<?php
$name = $_REQUEST['name'];
$email = $_REQUEST['email'];
$phone = $_REQUEST['contact'];
$subject = "feedback";
$question = $_REQUEST['question'];
$body = "<html>
<head>
</html>";
$mime_boundary = "<<<--==+X[".md5(time())."]\r\n\r\n";
$headers = "MIME-Version: 1.0"."\r\n" ."Content-Type:text/html;"."\r\n";
$headers .= 'From:'.$email. "\r\n";
$to ='example#example.com';
mail($to,$subject,$body,$headers);
echo "<script>alert(' message sent.');</script>";
?>
also tried :
<?php
$headers = array();
$headers[] = "MIME-Version: 1.0";
$headers[] = "Content-type: text/plain; charset=iso-8859-1";
$headers[] = "From: Sender Name <sender#domain.com>";
$headers[] = "Reply-To: Recipient Name <receiver#domain3.com>";
$headers[] = "Subject: {$subject}";
$headers[] = "X-Mailer: PHP/".phpversion();
mail($to, $subject, $email, implode("\r\n", $headers));
?>
Only this works:
$to ='example#example.com';
$subject="subject";
$body="body";
$mime_boundary = "<<<--==+X[".md5(time())."]\r\n\r\n";
$headers = "MIME-Version: 1.0"."\r\n" ."Content-Type:text/html;"."\r\n";
mail($to,$subject,$body,$headers);
try this :
$to = 'me#gmail.com';
$subject = "Email from me by: John";
$body = "my body";
$headers = "From: "."hello#world.com"."\r\n" .
"Reply-To: "."hello#world.com"."\r\n".
"X-Mailer: php";
$sent=mail($to, $subject, $body, $headers);
if ($sent) {
echo "good!";
} else {
echo("<p>Message delivery failed...</p>");
}