Jump to content

Help with sending emails


wikedawsum

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/90739-help-with-sending-emails/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.