Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5

Thread: mail() problem

  1. #1
    Regular Coder
    Join Date
    Mar 2006
    Posts
    200
    Thanks
    27
    Thanked 2 Times in 2 Posts

    mail() problem

    Hi,

    I have written a send to a friend script in php. The script appears to work fine...I can receive it, but hotmail, aol and bt connect email accounts don't receive anything.

    PHP Code:
    $friend_from_email "myemail@myaddress.co.uk";
    $friend_subject "Take a look at this";

    $filename "send_to_friend.txt"
    $handle fopen ($filename"r"); 
    $contents fread ($handlefilesize ($filename));
    $contents str_replace("Xyour_nameX"$your_name$contents);
    $contents str_replace("Xyour_emailX"$your_email$contents);
    $contents str_replace("Xfriend_nameX"$friend_name$contents);
    $contents str_replace("Xfriend_emailX"$friend_email$contents);
    $contents str_replace("XmessageX"$message$contents);
    $contents str_replace("XpageURLX"$pageURL$contents);
    $contents str_replace("Xfriend_from_emailX"$friend_from_email$contents);
    mail("$friend_email","$friend_subject","$contents","From: $your_name <$friend_from_email>");
    fclose ($handle); 
    I've tried adding header info like this:

    PHP Code:
    mail("$friend_email""$friend_subject""$contents",
         
    "From: $your_name <$email>\n" .
         
    "MIME-Version: 1.0\n " .
         
    "Content-type: text/html; charset=iso-8859-1" .     
         
    "X-Mailer: PHP/" phpversion()); 
    but it doesn't seem to make any difference.

    Any help is really appreciated!

    Thanks,

    Nicky

  • #2
    Regular Coder
    Join Date
    Jul 2007
    Location
    UK
    Posts
    223
    Thanks
    0
    Thanked 14 Times in 14 Posts
    Does $your_name need to be in quotes?
    i.e.
    Code:
    mail("$friend_email","$friend_subject","$contents","From: \"$your_name\" <$friend_from_email>");
    Also have you checked their spam folders?

  • #3
    Regular Coder
    Join Date
    Mar 2006
    Posts
    200
    Thanks
    27
    Thanked 2 Times in 2 Posts
    It's not going into their spam folders.

    It works fine...$your_name appears in the From field of the e-mail, so I'm not sure the quotes are needed but I'll have a go.

    Like I said..the code I have works fine..I just think I need something more, possibly more header information for it to be accepted by some e-mail accounts.

    Thanks for your help.

    Nicky

  • #4
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    If it helps these are the headers I use for my site
    PHP Code:
    $headers  "MIME-Version: 1.0\r\n";
    $headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
    $headers .= "From: PR Designz<me@domain.com>\r\n" .
                
    "Reply-To: ".$Email.""
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #5
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,020
    Thanks
    2
    Thanked 313 Times in 305 Posts
    There are several possibilities. Either your mail server is not sending the mail out or the receiving mail servers are not accepting it.

    When you send it to yourself, the To: address $friend_email is to a mail box on your mail server and your mail server just thinks it is receiving an email and it places it into your mail box. When you send it to an external mail address, your mail serve then thinks that you are relaying an email.

    Normally, to send an email to an external address, if the mail box in the From: address (ignoring the alias name portion of it, which does not require additional quotes BTW) is a mail box on your mail server, your mail server should accept the mail and re-send it. However, if your mail server is setup to require SMTP authentication to relay mail/send to a third party address, it won't accept and re-send this email, in which case there should be a failure message. Either check your web server log for errors or turn on full PHP error reporting at the start of your script. If your mail server is setup to require authentication, the PHP mail(...) function cannot be used and you would need to use a script like the phpmailer class - http://phpmailer.sourceforge.net/

    The header in your second example only has \n between the lines and it is possible that either your mail server or the receiving mail servers can not process the values. These need to be \r\n. Once you follow the code in Aerospace_Eng's post, the \n problem will be corrected. His code also supplies a proper From: address for a mail box on your mail server (see the next paragraph for why) and allows a separate reply to address that contains a mail box not on your mail server.

    For external mail servers to accept mail from your mail server, your mail server needs to have a reverse DNS record that resolves back to the domain name in the From: address and and you need to have an SPF record (an incorrect or non-matching SPF record will get your mail rejected, a missing SPF record will get your mail voted as junk/spam) that says that your mail server is allowed to send mail for the domain in the From: address. Check for these type of problems by entering your domain at www.dnsreport.com

    In the case where the From: address is the entered address from the form (the second header example in your first post), it would normally be an address that does not match your sending mail server and if the domain in that From: address has an SPF record, mail from your mail server would get rejected because the SPF record at that domain's DNS server would indicate that your mail server is not allowed to send mail for that From: domain.
    Last edited by CFMaBiSmAd; 07-14-2007 at 03:28 PM. Reason: fixed wording
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •