问题
So, I've set up a contact form for my website, and it's sending me e-mails when people sign-up. Here's the code:
<?php
include("include/settings.php");
if(isset($_POST['name']) && isset($_POST['contactEmail']) && isset($_POST['message'])){
$name = $_POST['name'];
$from = $_POST['contactEmail'];
$message = $_POST['message'];
$subject = "Message from " . $name;
if (mail ($to, $subject, $message, $from)) {
$response = array('sent' => 1);
echo json_encode($response);
} else {
$response = array('sent' => 0);
echo json_encode($response);
}
}
?>
Right now when I get an e-mail, it's from my godaddy servername. I don't mind if it continues to send from this email, but I need to have the from name changed to something different. Is this possible?
回答1:
Your present headers are being interpreted asyou@example.com, Message from John, the messsage, email@example.com
when it should be interpreted asyou@example.com, Message from John, the messsage, From: email@example.com
So the postmaster says: "There's no From: here, so I'll just assume you meant "my server".
- That is the reason why the "from" appears to be coming from the GoDaddy server.
Use proper headers: http://php.net/manual/en/function.mail.php
$headers = "From: ". $name . " <" . $from . ">\r\n";
then rewrite your present mail()
to read as:
mail ($to, $subject, $message, $headers)
If you want it to be something different, then use whatever different Email address.
Nota: Mail expects a "From" to be an email address and not a person's name.
So you could do:
$from_other = "another_email@example.com";
then do:
$headers = "From: ". $name . " <" . $from_other . ">\r\n";
then rewrite your present mail()
to:
mail ($to, $subject, $message, $headers)
- If that's what the ultimate goal is.
There are additional header options that are also available for you to use:
$from = $from . ' <' . $from . '>';
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=utf-8" . "\r\n";
$headers .= 'From: ' . $from . "\r\n";
$headers .= 'Reply-To: ' . $from . "\r\n";
$headers .= 'Return-Path: ' . $from . "\r\n";
$headers .= 'X-Mailer: PHP/' . phpversion() . "\r\n";
$headers .= 'X-Originating-IP: ' . $_SERVER['SERVER_ADDR'] . "\r\n";
- Using
Reply-To:
if you wish to use a different "reply to". Those variables can be changed to whatever you wish them to be.
回答2:
edit your $from
header to:
$from = 'From: '.$_POST['contactEmail'];
and then send it as you are already sending:
mail ($to, $subject, $message, $from)
Convention is to use $headers
variable. You can define various header parameters as:
$headers = 'From: webmaster@example.com' . "\r\n" .
'Reply-To: webmaster@example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
and send them as:
mail($to, $subject, $message, $headers);
Refer to this example for more: http://php.net/manual/en/function.mail.php#example-3482
回答3:
Use mail() and send proper headers:
$headers = 'From: ' . $_POST['name'] . ' <' . $_POST['contactEmail'] . '>';
mail($to, $subject, $message, $headers)
来源:https://stackoverflow.com/questions/27318724/using-php-to-send-e-mail-change-the-from-so-its-not-my-server-name