perezf Posted August 13, 2007 Share Posted August 13, 2007 How can i make it where my script below can send html emails? Right now it works, it sends emails, but how can i make it so it can send html emails <html> <head><title>Send Emails</title></head> <body> <?php if(empty($_POST['emailcontents'])) { echo "Please place email contents below"; } else { $host = "localhost"; $db = "massmailer"; $user = "maileradmin"; $pass = "mailerpass"; mysql_connect($host, $user, $pass) or die('Could not Connect to Database massmailer'); mysql_select_db($db) or die('Could not Select the Database'); $result = mysql_query("SELECT * FROM emails") or die("Could not select table"); $nrows = mysql_num_rows($result); $count = 0; for($i=0; $i<$nrows; $i++) { $row = mysql_fetch_assoc($result); $count++; mail($row['emailaddr'], "Test from Frank", $_POST['emailcontents'], "From: contact@test.com") or die("Could not send email"); echo "<b>Email Successfully Sent to:</b> ".$row['emailaddr']."<br />"; } } ?> <form action="" method="post"> <label>Contents: </label><br /> <textarea name="emailcontents"></textarea><br /> <input type="submit" value="Send Email to All" name="btnsendemail" /> </form> <a href="addemail.php">Add an Email to the List</a> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/64689-why-cant-i-send-html-emails/ Share on other sites More sharing options...
thefortrees Posted August 13, 2007 Share Posted August 13, 2007 http://www.zend.com/zend/trick/html-email.php Quote Link to comment https://forums.phpfreaks.com/topic/64689-why-cant-i-send-html-emails/#findComment-322551 Share on other sites More sharing options...
perezf Posted August 14, 2007 Author Share Posted August 14, 2007 i could not figure it out, is there anyway to make the script i have now work to send html emails,??? Quote Link to comment https://forums.phpfreaks.com/topic/64689-why-cant-i-send-html-emails/#findComment-323103 Share on other sites More sharing options...
hitman6003 Posted August 14, 2007 Share Posted August 14, 2007 You need to specify html headers: // To send HTML mail, the Content-type header must be set $headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; http://www.php.net/function.mail#id3102134 You could also use phpMailer, or a similar class. http://phpmailer.sourceforge.net/ Quote Link to comment https://forums.phpfreaks.com/topic/64689-why-cant-i-send-html-emails/#findComment-323105 Share on other sites More sharing options...
perezf Posted August 14, 2007 Author Share Posted August 14, 2007 i tried that and all the html code displays in the email, and now the email is sent to junk? How can i fix this Here is my code: <html> <head><title>Send Emails</title></head> <body> <?php if(empty($_POST['emailcontents'])) { echo "Please place email contents below"; } else { $host = "localhost"; $db = "massmailer"; $user = "maileradmin"; $pass = "mailerpass"; mysql_connect($host, $user, $pass) or die('Could not Connect to Database massmailer'); mysql_select_db($db) or die('Could not Select the Database'); $result = mysql_query("SELECT * FROM emails") or die("Could not select table"); $nrows = mysql_num_rows($result); $count = 0; for($i=0; $i<$nrows; $i++) { $row = mysql_fetch_assoc($result); $count++; $headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; $headers .= 'From: contact@test.com'; mail($row['emailaddr'], "Test from Frank", $_POST['emailcontents'], $headers) or die("Could not send email"); echo "<b>Email Successfully Sent to:</b> ".$row['emailaddr']."<br />"; } } ?> <form action="" method="post"> <label>Contents: </label><br /> <textarea name="emailcontents"></textarea><br /> <input type="submit" value="Send Email to All" name="btnsendemail" /> </form> <a href="addemail.php">Add an Email to the List</a> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/64689-why-cant-i-send-html-emails/#findComment-323109 Share on other sites More sharing options...
hitman6003 Posted August 14, 2007 Share Posted August 14, 2007 The headers must end with "\r\n". $headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; $headers .= 'From: contact@test.com' . "\r\n"; //<----------- Quote Link to comment https://forums.phpfreaks.com/topic/64689-why-cant-i-send-html-emails/#findComment-323111 Share on other sites More sharing options...
perezf Posted August 14, 2007 Author Share Posted August 14, 2007 i made the changes, but in my emails it still shows the html code for instance Content-type: text/html; charset=iso-8859-1 From: contact@test.com <html><body><h1>frank</h1></body></html> here is the code below, how would i fix that <html> <head><title>Send Emails</title></head> <body> <?php if(empty($_POST['emailcontents'])) { echo "Please place email contents below"; } else { $host = "localhost"; $db = "massmailer"; $user = "maileradmin"; $pass = "mailerpass"; mysql_connect($host, $user, $pass) or die('Could not Connect to Database massmailer'); mysql_select_db($db) or die('Could not Select the Database'); $result = mysql_query("SELECT * FROM emails") or die("Could not select table"); $nrows = mysql_num_rows($result); $count = 0; for($i=0; $i<$nrows; $i++) { $row = mysql_fetch_assoc($result); $count++; $headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; $headers .= 'From: contact@test.com' . "\r\n"; mail($row['emailaddr'], "Test from Frank", $_POST['emailcontents'], $headers) or die("Could not send email"); echo "<b>Email Successfully Sent to:</b> ".$row['emailaddr']."<br />"; } } ?> <form action="" method="post"> <label>Contents: </label><br /> <textarea name="emailcontents"></textarea><br /> <input type="submit" value="Send Email to All" name="btnsendemail" /> </form> <a href="addemail.php">Add an Email to the List</a> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/64689-why-cant-i-send-html-emails/#findComment-323334 Share on other sites More sharing options...
perezf Posted August 14, 2007 Author Share Posted August 14, 2007 Is there a reason why my script wont work, it doesnt send html, just text, I try putting html into it for instance <html><body><h1>frank</h1></body></html> and that is exactly what appears in the email??? its probably something simple :-\ Quote Link to comment https://forums.phpfreaks.com/topic/64689-why-cant-i-send-html-emails/#findComment-323462 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.