Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.