problem with php mail 'From' header
Edit: I just noted that you are trying to use a gmail address as the from value. This is not going to work, and the ISP is right in overwriting it. If you want to redirect the replies to your outgoing messages, use reply-to
.
A workaround for valid addresses that works with many ISPs:
try adding a fifth parameter to your mail()
command:
mail($to,$subject,$message,$headers,"-f your@email.here");
php mail From: & Reply-to: headers issue
I use an array for my headers, then implode them on "\r\n":
$headers = array();
$headers[] = 'Content-type: text/html; charset="UTF-8";';
$headers[] = 'Date: ' . date('r', $_SERVER['REQUEST_TIME']);
$headers[] = 'Message-ID: <' . $_SERVER['REQUEST_TIME'] . md5($_SERVER['REQUEST_TIME']) . (empty($_SERVER['SERVER_NAME']) ? '' : '@' . $_SERVER['SERVER_NAME']) . '>';
// here is the from and reply-to part:
$headers[] = 'From: "' . $fromName . '" <' . $fromAddress . '>';
$headers[] = 'Reply-To: "' . $replyToName . '" <' . $replyToAddress . '>';
$headers[] = 'X-Mailer: PHP v' . phpversion();
if (!empty($_SERVER['SERVER_ADDR'])) {
$headers[] = 'X-Originating-IP: ' . $_SERVER['SERVER_ADDR'];
}
// Use the implode function to collapse the array into a single string
mail($to, $subject, $message, implode("\r\n", $headers));
PHP mail function not setting from header correctly
I believe this line
$result = @mail($to, $subject, $message, $from);
Should be fixed to this
$result = sendmail($to, $subject, $message, $from);
And change your sendmail function to this
function sendmail($to, $subject, $message, $from) {
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers .= 'From: ' . $from . "\r\n";
$headers .= 'Reply-To: ' .$from . "\r\n";
$headers .= 'X-Mailer: PHP/' . phpversion();
@mail($to, $subject, $message, $headers);
if ($result) return 1;
else return 0;
}
Here you are using $email, but maybe you should use $from.
Your function takes $to, $subject, $message, $from.
Also you should maybe change your code a bit
function sendmail($to, $subject, $message, $from) {
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers .= 'From: ' . $from . "\r\n";
$headers .= 'Reply-To: ' .$from . "\r\n";
$headers .= 'X-Mailer: PHP/' . phpversion();
if(mail($to, $subject, $message, $headers)){
return 1;
}
return 0;
}
sendmail is a unix application binary used to send emails. Writing your own sendmail function in PHP doesn't do anything. The PHP mail function uses sendmail on linux by default I believe, and on windows, you need to configure SMTP. But as your emails are being sent, this is not the problem. Maybe you should rename the function, so you don't mix these things by accident.
Also here is whole code rewritten, maybe you can learn a thing or two :)
$post = false;
// flag to indicate which method it uses. If POST set it to 1
if (isset($_POST['Name'])) {
$post = true;
}
$name = (isset($_GET['Name'])) ? $_GET['Name'] : $_POST['Name'];
$email = (isset($_GET['Email'])) ? $_GET['Email'] : $_POST['Email'];
$phone = (isset($_GET['Phone'])) ? $_GET['Phone'] : $_POST['Phone'];
$comment = (isset($_GET['Comment'])) ? $_GET['Comment'] : $_POST['Comment'];
// Simple server side validation for POST data, of course, you should validate the email
if (!$name) {
$errors[] = 'Please enter your name.';
}
if (!$email) {
$errors[] = 'Please enter your email.';
}
if (!$comment) {
$errors[] = 'Please enter your comment.';
}
// if the errors array is empty, send the mail
if (count($errors) == 0) {
// recipient
$to = 'example@example.com';
// sender
$from = $name . ' <' . $email . '>';
// subject and the html message
$subject = 'Comment from ' . $name;
$message = '<!DOCTYPE html>
<html>
<head></head>
<body>
<table>
<tr><td>Name</td><td>' . $name . '</td></tr>
<tr><td>Email</td><td>' . $email . '</td></tr>
<tr><td>Phone</td><td>' . $phone . '</td></tr>
<tr><td>Comment</td><td>' . nl2br($comment) . '</td></tr>
</table>
</body>
</html>';
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers .= 'From: ' . $from . "\r\n";
$headers .= 'Reply-To: ' . $from . "\r\n";
$headers .= 'X-Mailer: PHP/' . phpversion();
$mailSuccess = mail($to, $subject, $message, $headers);
// if POST was used, display the message straight away
if ($post) {
if ($mailSuccess) {
echo 'Thank you! We have received your message.';
} else {
echo 'Sorry, unexpected error. Please try again later';
}
// else if GET was used, return the boolean value so that
// ajax script can react accordingly
// 1 means success, 0 means failed
} else {
echo (int)$mailSuccess;
}
// if the errors array has values
} else {
// display the errors message
foreach ($errors as $error) {
echo $error . '<br/>';
}
echo '<a href="contact.php">Back</a>';
}
php mail() headers prevent email from sending
"MIME-Version: 1.0 \r\n" . PHP_EOL .
is too many newlines. Don’t use PHP_EOL
at all; use \r\n
, and only once.
You also have an extra single quote after charset
.
$headers =
"From: website <donotreply@website.com>\r\n" .
"BCC: customer1@hotmail.com\r\n" .
"MIME-Version: 1.0\r\n" .
"Content-Type: text/html; charset=UTF-8";
Issue with headers in sending emails in PHP
You want to use a properly formatted "From" statement as follows:
$header = 'From: <noreply@xxxxx.com>' . "\r\n" .
or
$header = 'From: "no reply" <noreply@xxxxx.com>' . "\r\n" .
and another possible approach could be this: problem with php mail 'From' header
php mail() function, headers issue, can not send an email
Please stop using the php mail() function! My advice will be to use PHPMailer and sending the emails via SMTP.
Here is a small code snippet using PHPmailer:
<?php
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtpserver.com'; // Specify main SMTP server. If you dont have one us GMAL or mandrill
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'user@youremail.com'; // SMTP username
$mail->Password = 'pass'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
$mail->setFrom('from@youremail.com', 'Mailer');
$mail->addAddress('joe@youremail.net', 'Joe User'); // Add a recipient
$mail->addAddress('ellen@youremail.com'); // Name is optional
$mail->addReplyTo('info@youremail.com', 'Information');
$mail->addCC('cc@youremail.com');
$mail->addBCC('bcc@youremail.com');
$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
Related Topics
What Are the Disadvantages of Using Persistent Connection in Pdo
How to Make Pdf File Downloadable in HTML Link
Are Arrays in PHP Copied as Value or as Reference to New Variables, and When Passed to Functions
How to Enable Curl in PHP/Xampp
Get PHP to Stop Replacing '.' Characters in $_Get or $_Post Arrays
How to Pass Data to View in Laravel
Set Up the Base Url in Codeigniter
PHP Function to Make Slug (Url String)
PHP/MySQL Insert Row Then Get 'Id'
Get Url Query String Parameters
PHP, How to Catch a Division by Zero
How to Check Whether an Array Is Empty Using PHP
Global or Singleton For Database Connection
How to Get the Base Url With PHP
What's the Best Way to Get the Last Element of an Array Without Deleting It