php question - mail function

Discussion in 'Web Design and Development (archive)' started by brianellisrules, Dec 12, 2003.

  1. macrumors regular

    Joined:
    Oct 17, 2003
    #1
    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
     
  2. macrumors 6502a

    sonofslim

    Joined:
    Jun 6, 2003
    #2
    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 tostring subjectstring 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.
     
  3. thread starter macrumors regular

    Joined:
    Oct 17, 2003
    #3
    Hmmm, makes sense... but it didn't work. Thanks for trying though.
     
  4. Moderator emeritus

    Rower_CPU

    Joined:
    Oct 5, 2001
    Location:
    San Diego, CA
    #4
    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?
     
  5. thread starter macrumors regular

    Joined:
    Oct 17, 2003
    #5
    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.
     
  6. Moderator emeritus

    Rower_CPU

    Joined:
    Oct 5, 2001
    Location:
    San Diego, CA
  7. thread starter macrumors regular

    Joined:
    Oct 17, 2003
    #7
    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
     
  8. Moderator emeritus

    Rower_CPU

    Joined:
    Oct 5, 2001
    Location:
    San Diego, CA
    #8
    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.
     
  9. thread starter macrumors regular

    Joined:
    Oct 17, 2003
    #9
    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
     
  10. macrumors 65816

    mrjamin

    Joined:
    Feb 6, 2003
    Location:
    Strongbadia
    #10
    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
     

Share This Page