Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

brianellisrules

macrumors regular
Original poster
Oct 17, 2003
229
0
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:

PHP:
$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

macrumors 6502a
Jun 6, 2003
742
0
Re: php question - mail function

try this instead:
PHP:
$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:
PHP:
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.
 

Rower_CPU

Moderator emeritus
Oct 5, 2001
11,219
2
San Diego, CA
Here's a sample of one of mine that works right:
PHP:
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

macrumors regular
Original poster
Oct 17, 2003
229
0
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.
 

brianellisrules

macrumors regular
Original poster
Oct 17, 2003
229
0
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

Moderator emeritus
Oct 5, 2001
11,219
2
San Diego, CA
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 before you send it and see if that clears it up.
 

brianellisrules

macrumors regular
Original poster
Oct 17, 2003
229
0
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 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

macrumors 65816
Feb 6, 2003
1,161
1
Strongbadia
You really should fill out the headers section a little more - see the example below:

PHP:
$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
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.