Web Hosting Talk







View Full Version : Sending Emails w/PHP - Question about From


ltsf
08-23-2008, 03:08 PM
Hello,

Below is code that I've found. I'm trying to figure out why the From doesn't work. So here s the code:



<?php
$senderemail = "email@email.com";
$sendersubject = "Test";
$sendername = "Bill Gates";
$sendermessage = "a=z";

// multiple recipients
$to = 'billgates@gates.com; // note the comma

// subject
$subject = 'Contact from Site';

$random_hash = md5(date('r', time()));

// multipart message

// Plain text
$message = '--PHP-alt-'.$random_hash."\n";
$message .= 'Content-Type: text/plain; charset="iso-8859-1"'."\n";
$message .= 'Content-Transfer-Encoding: 7bit'."\n"."\n";

$message .= "Contact from Site\r\nFrom: $sendername\r\nEmail: $senderemail\r\nMessage:\r\n$sendermessage\r\n";

// HTML text
$message .= '--PHP-alt-'.$random_hash."\n";
$message .= 'Content-Type: text/html; charset="utf-8"'."\n";
$message .= 'Content-Transfer-Encoding: 7bit'."\n";

$message .= "<html>";
$message .= "
<head>
<meta http-equiv=\"Content-Type\" content=\"text/html;charset=utf-8\">
<title>Contact from Site</title>
</head>
<img src=\"https://98.129.32.170:8443/skins/winxp.new.compact/images/def_plesk_logo.gif\">
<body>
<p>A new message has been written in your site!</p>
<p>From: $sendername</p>
<p>Email: $senderemail</p>
<p>Subject: $sendersubject</p>
<p>The original message is:</p>
<pre>$sendermessage</pre>
</body>";
$message .= "</html>\n";

$message .= '--PHP-alt-'.$random_hash.'--'."\n";


// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";

// Additional headers
//$headers = "To: >\r\n";
$headers .= "Sandra Sender <sender@example.com>";
//$headers .= 'Cc: birthdayarchive@example.com' . "\r\n";
//$headers .= 'Bcc: birthdaycheck@example.com' . "\r\n";

$headers = "Content-Type: multipart/alternative; boundary=\"PHP-alt-".$random_hash."\"\r\n";
//Content-Type: text/html; charset="utf-8"' . "\r\n";

// Mail it
mail($to, $subject, $message, $headers);

print "Congratulations your email has been sent";

The only problem is that the email that is sent doesn't show who it's from. It shows Root, instead of Sandra Sender.

Can anyone help me out?

Burhan
08-23-2008, 05:45 PM
# $headers .= "Sandra Sender <sender@example.com>";
$headers .= "From: Sandra Sender <sender@example.com>\r\n";

ltsf
08-23-2008, 06:21 PM
Still doesn't work

Burhan
08-25-2008, 09:46 AM
It is because you are overwriting your headers in the line:


$headers = "Content-Type: multipart/alternative; boundary=\"PHP-alt-".$random_hash."\"\r\n";