PDA

View Full Version : php question - mail function


brianellisrules
Dec 12, 2003, 08:31 AM
OK, here's the deal. I setup a mailing list for my site.

http://www.brianellisrules.com/mailinglist/

I found the script online and it's simple and it works. Although if anyone has any suggestions for improvements, I'm all ears.

I tried adding in a feature where it would send me a notification email whenever someone signed up or left the list. Here's a snippet:

$myemail="(my email address)";
$subject="new mailing list subscription!";
$message="$fname $lname ($email) has joined the mailing list!";
$from=$email;
mail($myemail, $subject, $message, $from);

$fname, $lname and $email are input via the form on the page and that works.

It sends the email and all the variables are working. However, the $from portion is listed as part of the message, rather than going in the "From: " header... so when I get the email, it says "From: nobody@(my server)". (it's supposed to be "From: the person's email address", but instead their email address is listed in the body.)

What am I doing wrong and how do I get the $from variable to show up in the "From:" header of the email?

Thanks,
Brian

sonofslim
Dec 12, 2003, 09:17 AM
try this instead:
$from = 'FROM: '.$email;

i've had similar trouble with that in the past, but it seems that explicitly stating the FROM: header within the variable makes a difference. on my server, at least.

edit: found the technical reasons for this. the syntax for mail() is: mail ( string to, string subject, string message [, string additional_headers])

so when you're defining an additional header, you need to tell the function which header you're adding, specifically, because there's no default value on that parameter.

brianellisrules
Dec 12, 2003, 09:41 PM
Hmmm, makes sense... but it didn't work. Thanks for trying though.

Rower_CPU
Dec 12, 2003, 11:12 PM
Here's a sample of one of mine that works right:
mail("$email", "$subject", "Dear $fullname,\n\nThis is a confirmation email generated by ...", "From: $admin_email");

Just like sonofslim's example. Might it be a server configuration issue?

brianellisrules
Dec 12, 2003, 11:23 PM
Actually, sonofslim's suggestion DID work... I just uploaded the wrong file when I re-tested it.



I seem to embarass myself a lot when I come here.

Rower_CPU
Dec 13, 2003, 12:23 AM
Hey, no worries. Glad you got it worked out.

brianellisrules
Dec 17, 2003, 09:19 AM
Here's a follow-up question... now that I have this working, whenever I send out an email it's in plain text. No big deal. What's kind of annoying is when I put a single quote in there, it put's a slash before it when it sends the email. So if I type "that's" it comes out "that/'s" (or something similar, it might be a backward slash instead of foward... I don't have an example sitting here in front of me.)

Is this a common occurance? Is there a way around this?

Thanks again,
Brian

Rower_CPU
Dec 17, 2003, 12:33 PM
Yeah, I've had issues with that in the past when passing form inputs from page to page. It comments out any sort of quotes with a backslash (\) to prevent the php from confusing the quotes in the string as the end of a statement.

How is the text getting added to the message? If it's from the database or a variable string, you might want to run it through a stripslashes function (http://us2.php.net/manual/en/function.stripslashes.php) before you send it and see if that clears it up.

brianellisrules
Dec 19, 2003, 08:07 AM
Originally posted by Rower_CPU
Yeah, I've had issues with that in the past when passing form inputs from page to page. It comments out any sort of quotes with a backslash (\) to prevent the php from confusing the quotes in the string as the end of a statement.

How is the text getting added to the message? If it's from the database or a variable string, you might want to run it through a stripslashes function (http://us2.php.net/manual/en/function.stripslashes.php) before you send it and see if that clears it up.
Cool, I'll have to look into it. It was another one of those "find a script on the internet and butcher it to fit my needs" things. I'm pretty sure it's added through a variable string, but I haven't looked at it since I set it up.

Brian

mrjamin
Dec 21, 2003, 08:56 AM
You really should fill out the headers section a little more - see the example below:


$subject = "Message Subject";
// ALWAYS PUT THE MIME TYPE IN!
$headers = "MIME-Version: 1.0\n";
// tell the email client what kind of content it is
$headers .= "Content-type: text/plain; charset=iso-8859-1\n";
// put a name in, then an email address
// based where the domain is the same
// as the domain you're actually using -
// you could use the user's name in the
// first part of the "From: " bit
$headers .= "From: \"A name\" <address@domain.tld>\n";
// Use the reply to as the user's details
$headers .= "Reply-To: \"User\" <address@domain.tld>\n";
// tell the email client what you were
// using to send the email
$headers .= "X-Mailer: PHP's mail() Function\n";
$message = "The message";
mail($recipient, $subject, $message, $headers);


Using a decent amount of header information will stop email clients and mailservers flagging the email as junk, and also ensures that the email will display properly in any email client.

MrJ - PHP Guru :D