wikedawsum Posted February 12, 2008 Share Posted February 12, 2008 Hello, all. I have created a script to try and send HTML emails with PHP. The script halfway works. It sends out my emails in HTML format, BUT.. it posts the subject 3 times, and also prints the message 3 times in the email body. What I am trying to do is pull emails from a database and send the message to those emails. Here's my complete script. <?php include_once('include_fns.php'); session_start(); if (isset($_SESSION['auth_user'])){ $conn = mysql_connect("******", "******", "******") or die($msg_no_connect); mysql_select_db("******") or die(mysql_error()); $query = "SELECT * FROM users WHERE ".$_POST['newsletter_type']." = '1'"; $res=mysql_query($query) or die('query failed'); } else { echo 'You are not authorized to view this page.'; } ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>My Site</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> </head> <body> <h1>Welcome <? echo $_SESSION['auth_user']; ?></h1> <h2>Writer Administration</h2> <p><?php if (mysql_num_rows($res) > 0){ echo "<p>The newsletter has been sent.</p>"; while ($row = mysql_fetch_assoc($res)){ $to = "{$row[email]}"; $body =' <html> <body> <style> <!-- body { font-family: "arial"; font-size: 10pt; color: "000000"; line-height: 9pt; } h1 { font-family: "arial"; font-size: 12pt; color: "FD1CD5"; } --> </style> </body> '; $headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; $headers .= 'From: ****@******.com' . "\r\n"; $bodys .="<h1>Hello, {$row[username]}</h1><br /><br />"; $bodys .="<p>".$_POST['message']."</p>"; $subject .= "".$_POST['subject'].""; $body = $body . $bodys; mail($to, $subject, $body, $headers); } } else { echo "There was a problem sending your email. Please contact the <a href='mailto:***@******.com'>administrator</a>."; } ?> </p> </body> </html> As you can see, I'm trying to get the email and username from my database. It does bring them back, but in my email message, it prints the username and the message each time it finds a username. For example, if 3 people have signed up to receive this newsletter, then my email has the subject printed 3 times, contains 3 usernames and 3 messages.. but it sends to each email address. I hope I explained that well enough. Does anyone know what might be causing this, or if there is a better way to do this? I have used this script before, but only sending to one email address and not pulling it from a database. Any help or a point in the right direction is GREATLY appreciated. Thanks! wikedawsum Quote Link to comment https://forums.phpfreaks.com/topic/90739-help-with-sending-emails/ Share on other sites More sharing options...
marcus Posted February 12, 2008 Share Posted February 12, 2008 $subjbect = $_POST['subject']; $body = "<h1>Hello, {$row[username]}</h1><br /><br />"; $body .="<p>".$_POST['message']."</p>"; $to = $row['email']; mail($to, $subject, $body, $headers) or die("Failed to send e-mail"); Quote Link to comment https://forums.phpfreaks.com/topic/90739-help-with-sending-emails/#findComment-465101 Share on other sites More sharing options...
wikedawsum Posted February 12, 2008 Author Share Posted February 12, 2008 Thanks, mgallforever. That solved the problem. It's always one little piece that gets me! Appreciate your help! Quote Link to comment https://forums.phpfreaks.com/topic/90739-help-with-sending-emails/#findComment-465150 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.