30479

Using PHP to send e-mail, change the FROM so it's not my server name

Question:

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:

<pre class="lang-php prettyprint-override"><?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?

Answer1:

Your present headers are being interpreted as<br />you@example.com, Message from John, the messsage, email@example.com<br /> when it should be interpreted as<br />you@example.com, Message from John, the messsage, From: email@example.com

So the postmaster says: <em>"There's no From: here, so I'll just assume you meant "my server"</em>.

<ul><li>That is the reason why the "from" appears to be coming from the GoDaddy server.</li> </ul>

Use proper headers: <a href="http://php.net/manual/en/function.mail.php" rel="nofollow">http://php.net/manual/en/function.mail.php</a>

$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 <em>different</em> Email address.

<strong>Nota:</strong> 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)

<ul><li>If that's what the <em>ultimate goal</em> is.</li> </ul><hr />

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"; <ul><li>Using Reply-To: if you wish to use a different "reply to". Those variables can be changed to whatever you wish them to be.</li> </ul>

Answer2:

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: <a href="http://php.net/manual/en/function.mail.php#example-3482" rel="nofollow">http://php.net/manual/en/function.mail.php#example-3482</a>

Answer3:

Use mail() and send proper headers:

$headers = 'From: ' . $_POST['name'] . ' <' . $_POST['contactEmail'] . '>'; mail($to, $subject, $message, $headers)

Recommend

  • Signing a java applet with an spc file from GoDaddy
  • net::ERR_EMPTY_RESPONSE when post with ajax
  • Stored data can't access in Yii
  • Is there a MySQL query that can encode to JSON in exactly this way?
  • How to Grab SSL Certificate in OpenSSL
  • FCM on app closed
  • Installing SSL cert on Amazon EC2 ELB
  • Why are `colMeans()` and `rowMeans()` functions faster than using the mean function with `lapply()`?
  • 'include' of functions in groovy scripts
  • Passing information to server-side function in a Google Docs Add On
  • Can XOR be expressed using SKI combinators?
  • What's the syntax to inherit documentation from another indexer?
  • Javascript CORS - No 'Access-Control-Allow-Origin' header is present
  • Stop Bash Script if Hive Fails
  • How to add git credentials to the build so it would be able to be used within a shell code?
  • How do I access an unhandled exception in an MVC Error view?
  • How do I open a C file with a relative path?
  • Content-Length header not returned from Pylons response
  • Email verification using google app script and google forms
  • why do I get the error when installing the gem 'pg'? [duplicate]
  • FileReader+canvas image loading problem
  • Why value captured by reference in lambda is broken? [duplicate]
  • Email format validation in mvc3 view
  • Spring Data JPA custom method causing PropertyReferenceException
  • Is there any way to access browser form field suggestions from JavaScript?
  • C# - Is there a limit to the size of an httpWebRequest stream?
  • Why ng-show works with ng-repeat but ng-if doesn't? [duplicate]
  • Excel - Autoshape get it's name from cell (value)
  • Updating server-side rendering client-side
  • How to apply VCL Styles to DLL-based forms in Inno Setup?
  • sending/ receiving email in Java
  • Display Images one by one with next and previous functionality
  • Upload files with Ajax and Jquery
  • jQuery tmpl and DataLink beta
  • Warning: Can't call setState (or forceUpdate) on an unmounted component
  • How to pass list parameters for each object using Spring MVC?
  • json Serialization in asp
  • Change div Background jquery
  • Can't mass-assign protected attributes when import data from csv file
  • sending mail using smtp is too slow