...

View Full Version : php mail() form - Changing The FROM email



macmonkey
09-24-2005, 06:11 PM
I've checked the php manual and I've tried a few things but can't figure it out..

Right now when someone submits a mail form through my site - the FROM: address is this annoying username@123linhost.bb.blahblahblah.com path from my godaddy hosting.

I'd like to change this to something simple and nice like...

mail@MyDomain.com

I've tried using headers (but evidently these don't do what I want)


$headers = "MIME-Version: 1.0\r\n";
$headers .= "From: mail@mydomain.com>\n";
$headers .= "Reply-To: info@mydomain.com\n";
$headers .= "X-Priority: 2\n";
$headers .= "X-MSMail-Priority: Normal\n";
$headers .= "X-Mailer: PHP4\n";
$headers .= "Content-type: text/html; charset=iso-8859-1" . "\n";

In the manual it talks about changing the sendmail_from = in my php.ini file but I can't get to this except through SSH which I know nothing about.

Any other options?

Thanks

misterx
09-24-2005, 06:25 PM
Your From: line headers look a little funky to me. Shouldn't it just be something like this?
$headers = "From: Display Name <username@domain.tld>\n";

macmonkey
09-24-2005, 07:11 PM
yea I'd pulled that from a post a few months back.. changing it didn't do any good :(

tsudeki
09-25-2005, 03:04 AM
I'm willing to bet it's the way you're doing the newlines.

I've had these strange problems on unix servers where (if there was more than 1 line in the header) the email address would be root@subdomain.domain.tld.

Try changing them all to carriage returns (\r\n) and try putting the From above the MIME-Version...dunno if it'll work but it's worth a shot. :)

bcarl314
09-25-2005, 03:15 AM
A couple of things.

First, change all your new lines (\n) to carriage return newlines (\r\n)

Second, your header format should be

"From: User Name<email@domain.com>\r\n"

And third, for spam filters, you may need to use the -f option in the mail funciton

example:

mail(TO_ADDY, SUBJECT, MESSAGE, $headers, -f youremail@yourdomain.com);

700lbGorilla
09-25-2005, 05:37 AM
Yeah, having the same problem


$to = $e;
$subject = "One more step";
$message = "Hello, <br><br>";
$message .= "Just one more step and you will be on your way to joining <a href=\"http://www.webiste.com\" target=\"_blank\">Website</a><br><br>";
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$headers .= "From: Registration<register@website.com>\r\n";
$mailsent = mail($to, $subject, $message, $headers);

I get the from as something@box27.host.com But in the first line of the email message it says "From: Registration" But nowhere does it say register@domain.com. I also get errors is i try to do the -f register@domain.com thing.

mindlessLemming
09-25-2005, 09:39 AM
ini_set(sendmail_from, $from); //where $from is the person sending the mail
mail($to, $subject, $msg, $header);
ini_restore( sendmail_from );

petronel
09-25-2005, 02:03 PM
Yeah, having the same problem

I get the from as something@box27.host.com But in the first line of the email message it says "From: Registration" But nowhere does it say register@domain.com. I also get errors is i try to do the -f register@domain.com thing.


try to move From: before MIME... like this


$to = $e;
$subject = "One more step";
$message = "Hello, <br><br>";
$message .= "Just one more step and you will be on your way to joining <a href=\"http://www.webiste.com\" target=\"_blank\">Website</a><br><br>";
$headers = "From: Registration<register@website.com>\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "Content-Transfer-Encoding: 7bit\r\n\r\n";

$mailsent = mail($to, $subject, $message, $headers);

700lbGorilla
09-25-2005, 06:17 PM
ini_set(sendmail_from, $from); //where $from is the person sending the mail
mail($to, $subject, $msg, $header);
ini_restore( sendmail_from );


Can you further explain that, not sure how that fits into my code. I'm still having no luck with the other solutions.

mindlessLemming
09-26-2005, 04:15 AM
Can you further explain that, not sure how that fits into my code. I'm still having no luck with the other solutions.



$to = $e;
$subject = "One more step";
$message = "Hello, <br><br>";
$message .= "Just one more step and you will be on your way to joining <a href=\"http://www.webiste.com\" target=\"_blank\">Website</a><br><br>";
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\n";
$headers .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$headers .= "From: 'Registration' <register@website.com>\n";
$headers .= "Return-Path:register@website.com\n";
$headers .= "X-Sender: register@website.com\n";
ini_set(sendmail_from, 'register@website.com');
$mailsent = mail($to, $subject, $message, $headers);
ini_restore( sendmail_from );
if($mailsent) echo 'w007!';

That should give the desired result for all email clients, including Gmail and Hotmail. [No more '(unknown sender)' garbage for Gmail users!]


You should be testing this on a live server. Trying to use mail() from a localhost server on a Windows box is hell.

Here's a complete contact form w/ php source that may be helpful: http://leftjustified.net/lab/simple-safe-php-email/
^ also includes checks for attempted email injection attacks ;)

Feel free to use as you wish :)

700lbGorilla
09-26-2005, 04:42 AM
Yeah, that still didn't work, using that inserts "From: 'Registration' Return-Path:register@domain.com X-Sender:register@domain.com" into the beginning of the actual message. Thinking I need to contact my webhost and see if they are blocking the ability to alter who it is from.

mindlessLemming
09-26-2005, 11:27 AM
I've never used \n\r in email headers -- that may have been your problem.
When I actually tested your script I got the same result as you :o

A quick hack and ftp later, I can assure you this one works under PHP 4.3.10 and the mail is perfect when received by a Gmail account :)



<?php
$to = $e;
$subject = "One more step";
$message = "Hello, <br><br>";
$message .= "Just one more step and you will be on your way to joining <a href=\"http://www.webiste.com\" target=\"_blank\">Website</a><br><br>";
$headers = "From: 'Registration' <register@website.com>\n";
$headers .= "Return-Path:register@website.com\n";
$headers .= "X-Sender: register@website.com\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\n";
$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-Transfer-Encoding: 7bit\n";
ini_set(sendmail_from, 'register@website.com');
$mailsent = mail($to, $subject, $message, $headers);
ini_restore( sendmail_from );
if($mailsent) echo 'w007!';
?>

700lbGorilla
09-26-2005, 12:15 PM
I uses PHP 4.3.11 and it still didn't work, is it possible for a host to stop this? I'm going to contact them and see why this keeps happening.

mindlessLemming
09-26-2005, 12:29 PM
yep, sounds like it's your host.
Just wanted to make sure you were using a correct script so the host had no option of saying "OMFG!!! RTFM j00 n00b!" or the customer service equivalent.
:)

700lbGorilla
09-27-2005, 04:26 AM
I figured it out before I contacted them, I was just testing to see if the script would work. But I guess in order to have a from address, that account has to actually exist. Not sure if this is a normal thing, I just thought whatever the from was, it would work. As soon as I changed it to a from: address that actually existed, it worked like a charm. So is this a host precaution to controlling spamming? Or is this a mail() rule that I wasn't aware of?

mindlessLemming
09-27-2005, 07:37 AM
Probably the former :|
That said, I ran the script on my server exactly as posted and it worked fine, regardless of bogus email address.

With that in mind, I'd say it's some custom config work on Godaddy's part.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum