...

View Full Version : PHP mail not working



TheMachineWare
05-25-2011, 07:59 AM
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.

bullant
05-25-2011, 01:08 PM
This tute (http://www.webcheatsheet.com/php/send_email_text_html_attachment.php) shows how to send html emails using php mail()

TheMachineWare
05-25-2011, 09:24 PM
Here is my full 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: subscribe@test.com\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??

Inigoesdr
05-25-2011, 10:28 PM
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
tags (http://www.codingforums.com/showthread.php?t=68462) when posting code.

TheMachineWare
05-26-2011, 02:19 AM
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:



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: subscribe@test.com\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:


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: subscribe@test.com\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??

TheMachineWare
05-26-2011, 04:45 AM
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.

bullant
05-26-2011, 05:11 AM
Have you checked if you have SMTP enabled on your hosting account. SMTP is a "must have" for php mail() to work.

TheMachineWare
05-26-2011, 03:18 PM
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.

TheMachineWare
05-26-2011, 03:52 PM
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.

TheMachineWare
05-26-2011, 09:13 PM
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?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum