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 10 of 10
  1. #1
    New Coder
    Join Date
    Mar 2011
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

    PHP mail not working

    I currently have a mail script that works with the following headers:

    $headers .= "MIME-Version: 1.0\n";
    $headers .= "Content-Transfer-Encoding: 8bit\n";
    $headers .= "Content-Type: text/plain; charset=iso-8859-1\n";

    When I try to change it to Content-Type: text/html; charset=iso-8859-1. The script runs, but the email never sends. I don't get any error messages that I know of. I don't know if this in a configuration problem with my host or my script problem. My server is running php 5.3.5.

    Thanks.

  • #2
    Banned
    Join Date
    Feb 2011
    Posts
    2,699
    Thanks
    13
    Thanked 395 Times in 395 Posts
    This tute shows how to send html emails using php mail()

  • #3
    New Coder
    Join Date
    Mar 2011
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Here is my full code:

    PHP Code:
    <?php
    if ($_POST['fname'] != "" && $_POST['lname'] != "" && $_POST['email'] != "" && $_POST['company'] != "") {
            
    $company stripslashes($_POST['company']);
            
    $fname stripslashes($_POST['fname']);
            
    $lname stripslashes($_POST['lname']);
            
    $email stripslashes($_POST['email']);
            
    $address stripslashes($_POST['address']);
            
    $address2 stripslashes($_POST['address2']);
            
    $city stripslashes($_POST['city']);
            
    $state stripslashes($_POST['state']);
            
    $zip stripslashes($_POST['zip']);
            
    $phone stripslashes($_POST['phone']);
            
    $fax stripslashes($_POST['fax']);
            
    $website stripslashes($_POST['website']);
            
            
    $message "Company: ".$company."\nFirst Name: ".$fname."\nLast Name: ".$lname."\nEmail: ".$email."\nAddress: ".$address."\nAddress 2: ".$address2."\nCity: ".$city;
            
    $message .= "\nState: ".$state."\nZip: ".$zip."\nPhone: ".$phone."\nFax: ".$fax."\nWebsite: ".$website;
            
            
    $tdate date('m/d/Y');
            
    $subject "I want to Subscribe ".$tdate;
            
            
    $to "test@test.com";
            
    $boundary '-----=' md5(uniqid(rand()));
            
    $headers "From: [email]subscribe@test.com[/email]\n";
            
    $headers .= "MIME-Version: 1.0\n";
            
    $headers .= "Content-Transfer-Encoding: 8bit\n";
            
    $headers .= "Content-Type: text/plain; charset=iso-8859-1\n";
            
            
    mail($to$subject$message$headers);
            
            echo 
    "<span class=\"cmslarge\">Thank you for subscribing!.</span><br />\n<br />\n";
    }
    ?>
    This code works without any problems. I have read guides and the only difference I can really see to get it to make it as HTML is change the Content-Type: text/plain and change that to Content-Type: text/html.

    When I change that, the system no longer sends any emails. Am I missing something simple on this or is it a problem with my host??
    Last edited by Inigoesdr; 05-25-2011 at 10:23 PM.

  • #4
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    Terminate your lines with \r\n instead of just \n, lose the content-transfer-encoding unless you explicitly need it, and make sure your from email is correct(you don't need the [email] tags). You aren't actually sending any HTML there. Please remember to read the stickies for this forum. In particular the one about using [php][/php] tags when posting code.

  • #5
    New Coder
    Join Date
    Mar 2011
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I originally used \r\n and it had no effect. I added the content encoding to see. When I run the script with no content-encoding and just \r\n. The email is still not sent by the server.

    When I have:

    PHP Code:
    if ($_POST['fname'] != "" && $_POST['lname'] != "" && $_POST['email'] != "" && $_POST['company'] != "") {
            
    $company stripslashes($_POST['company']);
            
    $fname stripslashes($_POST['fname']);
            
    $lname stripslashes($_POST['lname']);
            
    $email stripslashes($_POST['email']);
            
    $address stripslashes($_POST['address']);
            
    $address2 stripslashes($_POST['address2']);
            
    $city stripslashes($_POST['city']);
            
    $state stripslashes($_POST['state']);
            
    $zip stripslashes($_POST['zip']);
            
    $phone stripslashes($_POST['phone']);
            
    $fax stripslashes($_POST['fax']);
            
    $website stripslashes($_POST['website']);
            
            
    $message "Company: ".$company."\nFirst Name: ".$fname."\nLast Name: ".$lname."\nEmail: ".$email."\nAddress: ".$address."\nAddress 2: ".$address2."\nCity: ".$city;
            
    $message .= "\nState: ".$state."\nZip: ".$zip."\nPhone: ".$phone."\nFax: ".$fax."\nWebsite: ".$website;
            
            
    $tdate date('m/d/Y');
            
    $subject "I want to Subscribe ".$tdate;
            
            
    $to "test@test.com";
            
    $boundary '-----=' md5(uniqid(rand()));
            
    $headers "From: [email]subscribe@test.com[/email]\n";
            
    $headers .= "MIME-Version: 1.0\n";
            
    $headers .= "Content-Transfer-Encoding: 8bit\n";
            
    $headers .= "Content-Type: text/plain; charset=iso-8859-1\n";
            
            
    mail($to$subject$message$headers);
            
            echo 
    "<span class=\"cmslarge\">Thank you for subscribing!.</span><br />\n<br />\n";

    This code works and my webserver sends an email to the account I have entered with the correct from address.

    When I use this code:
    PHP Code:
    if ($_POST['fname'] != "" && $_POST['lname'] != "" && $_POST['email'] != "" && $_POST['company'] != "") {
            
    $company stripslashes($_POST['company']);
            
    $fname stripslashes($_POST['fname']);
            
    $lname stripslashes($_POST['lname']);
            
    $email stripslashes($_POST['email']);
            
    $address stripslashes($_POST['address']);
            
    $address2 stripslashes($_POST['address2']);
            
    $city stripslashes($_POST['city']);
            
    $state stripslashes($_POST['state']);
            
    $zip stripslashes($_POST['zip']);
            
    $phone stripslashes($_POST['phone']);
            
    $fax stripslashes($_POST['fax']);
            
    $website stripslashes($_POST['website']);
            
            
    $message "Company: ".$company."\nFirst Name: ".$fname."\nLast Name: ".$lname."\nEmail: ".$email."\nAddress: ".$address."\nAddress 2: ".$address2."\nCity: ".$city;
            
    $message .= "\nState: ".$state."\nZip: ".$zip."\nPhone: ".$phone."\nFax: ".$fax."\nWebsite: ".$website;
            
            
    $tdate date('m/d/Y');
            
    $subject "I want to Subscribe ".$tdate;
            
            
    $to "test@test.com";
            
    $boundary '-----=' md5(uniqid(rand()));
            
    $headers "From: [email]subscribe@test.com[/email]\n";
            
    $headers .= "MIME-Version: 1.0\n";
            
    $headers .= "Content-Transfer-Encoding: 8bit\n";
            
    $headers .= "Content-Type: text/html; charset=iso-8859-1\n";
            
            
    mail($to$subject$message$headers);
            
            echo 
    "<span class=\"cmslarge\">Thank you for subscribing!.</span><br />\n<br />\n";

    The script processes. I don't get any php errors. I do not receive an email at all. The only difference is the content-type. The rest of the code is the exact same. I realize I am just sending plain text. I am getting this to work prior to formatting everything into HTML.

    I do not understand why the webserver is not sending an email with the mail command when I only change the content-type??

  • #6
    New Coder
    Join Date
    Mar 2011
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Update from my host. The mail is not being sent out from the mail server. So now I have to troubleshoot why the mail command is failing.

  • #7
    Banned
    Join Date
    Feb 2011
    Posts
    2,699
    Thanks
    13
    Thanked 395 Times in 395 Posts
    Have you checked if you have SMTP enabled on your hosting account. SMTP is a "must have" for php mail() to work.

  • #8
    New Coder
    Join Date
    Mar 2011
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts
    bullant,

    yes. smtp is enabled. Again, the problem I am getting is that this script works fine with content-type: text/plain. I can run this as many times as I want and it works everytime and I always get a email from it. The moment I switch it over to content-type:text/html. AGAIN, this is the only thing I change. The script no longer emails me a message. The script appears to work fine, but I DON"T get a email.

    My host is saying they think it is failing with content-type: text/html because the server is assuming that it's a static file.

    I am also trying to get a success/fail from the mail command. But the server isn't returning a true/false from the mail command.

  • #9
    New Coder
    Join Date
    Mar 2011
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts
    If I put content-type: multipart/mixed. The script works fine and the email is sent. The system will not work if content-type: text/html is used.


    I also talked with my host and when the content-type: is set to text/html. the sever has NO log of a email being sent in the mail log file. I know the email is not being flagged as spam!

    If the content-type is set as either text/plain or multipart/mixed. The email is sent and the server is recording this in the mail log.

    I am also unable to get a true/false (success/fail) from the mail() command. What setting am I missing to get this back?

    My webhost is using linux with sendmail.
    Last edited by TheMachineWare; 05-26-2011 at 04:17 PM.

  • #10
    New Coder
    Join Date
    Mar 2011
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I have now finally been able to test my script on another server with the same host. A friend of mine uses them also. We uploaded my file to his server and ran the script and it worked without issues. So it looks like my script is not at fault here, but a server configuration with my host. This email would work fine with 2/3 content-types.

    Anyone have any ideas on the server configuration that could allow this when running linux with sendmail?


  •  

    Posting Permissions

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