PHP God Needed: HTML formatted email from PHP mailer

Hey guys. I thought this was hard because I’m an uber-beginner to PHP/MySQL (3rd day!!). Now, after a good 6 hours of searching the web, asking all my script buddies, and good ol’ trial and error, I have come to the conclusions that this is, in fact, a difficult thing to do.

I am trying to send out a nice HTML formatted email from a PHP script that sends to all emails in a database. The HTML mail also grabs data from a db as well. I have tried with and without the added php code, many different ways of settign up the file, and even without the html. No matter what i do i get an error. if anyone can help me build a working file that sends html emails i will be eternally grateful. I stripped the script down to the bare minimum, which is what youll see here. i need to have that loop there to get all the emails in the db but it seems to be clashing with the headers. (the script works fine without the headers added).


<?
	mysql_connect ("localhost", "user", "pass");
	mysql_select_db ("database");

	$sql = "SELECT * FROM redmail";
	$result = mysql_query($sql);
	$subject = $_POST['subject'];
	$message = $_POST['message'];
$headers  = "MIME-Version: 1.0
";
$headers .= "Content-type: text/html; charset=iso-8859-1
";
	While ($row = mysql_fetch_array($result)) 
{
	$email = $row['email'];
	$success = mail("$email","$subject","$message","$headers","From: [email][email protected][/email]");
	if ($success) 
		echo "The newsletter was successfully sent<br />"; 
	else 
		echo "An error occurred when sending the newsletter<br />";

}

mysql_close();
?>

thanks in advance!

You’d have to post the errors you get, also, how many emails are you trying to send, could be that the scripts max_execution time is over before it’s done…?

sorry, i just edited the above code, i posted the wrong one…

i added in the

    if ($success) 
        echo "The newsletter was successfully sent<br />"; 
    else 
        echo "An error occurred when sending the newsletter<br />";

part before.
The error I get is simply my homemade error message, “An error occurred when sending the newsletter”. And at this point I’m only testing with two emails.

is there a better way to test whats going wrong?

Just one thing. These are variables inside the mail() function. No need to have " " around them…

mail($email, ,$subject,$message,$headers,"From: [email][email protected][/email]");

That might fix it…

thanks man! i fixed it. i wound up changing the whole thing around and this one works now.

Bit late if you sussed it but this may be of some use toyou

http://phpmailer.sourceforge.net/

hey thats really neat. thanks! ill keep it in mind if i ever need it. :slight_smile: