Forum Moderators: coopster

Message Too Old, No Replies

Help with email script

         

adammc

12:57 am on May 14, 2006 (gmt 0)

10+ Year Member



Hi Folks,

I am trying to write a script that emails a newsletter to the members in my database.

I have it sending the email however it is not printing the name field in the email when sent.

I even tried setting the name variable like so $name = $r1["name"];

Here is the code I am using:

[PHP]<?

include("../../config.php");
include("../db-connect.php");

if(isset($submit))
{
if(empty($validated))
{
echo "<center><br><br><br> Error.<br> Go <a class=TN href=mail.php> back</a> and make your choice. </center>";
exit;
}
if(empty($message))
{
echo "<center><br><br><br> You are trying to send blank email. <br> Go <a class=TN href=mail.php> back</a> and write some text. </center>";
exit;
}

if($_POST[validated] == 'yes')
{
$q1 = "select email, name from newsletter WHERE validated='yes' ";
$r1 = mysql_query($q1) or die(mysql_error());
$name = $r1["name"];
}


elseif($_POST[validated] == 'no')
{
$q1 = "select email, name from newsletter WHERE validated='no' ";
$r1 = mysql_query($q1) or die(mysql_error());
$name = $r1["name"];

}

while($a1 = mysql_fetch_array($r1))
{

$message = " Dear $name \n\n\n $message";
$from = "From: info@kimberleys.net.au";
mail($a1[0], $subject, $message, $from);

}
echo "<center><br><br><br>The mail was sent successfully. </center>";
unset($validated);
exit;
}
?>[/PHP]

Any help would be GREATLY appreciated.

eelixduppy

3:03 am on May 14, 2006 (gmt 0)



hmmm...try echoing $name to the screen before emailing it to see if it actually has a value. If it doesn't, i would try adding $name = ""; at the top of the page.

Good luck!

eelix

Habtom

5:29 am on May 14, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You haven't fetched the data of your query.

$r1 = mysql_query($q1) or die(mysql_error());
$name = $r1["name"];

SHOULD GO SOMETHING LIKE

$r1 = mysql_query($q1) or die(mysql_error());
$r2 = mysql_fetch_array($r1);
$name = $r2["name"];

OR

$r1 = mysql_query($q1) or die(mysql_error());
$r2 = mysql_fetch_array($r1);
$name = $r2['name'];

Habtom

dreamcatcher

7:05 am on May 14, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi adammc,

Just move the $name variable to inside the while loop:


while($a1 = mysql_fetch_array($r1))
{
$name = $a1["name"];
$message = " Dear $name \n\n\n $message";
$from = "From: info@kimberleys.net.au";
mail($a1[0], $subject, $message, $from);
}

dc

adammc

7:24 am on May 14, 2006 (gmt 0)

10+ Year Member



Thanks for the help guys, much apreciated.

I got it sorted using Dreamcatchers advice, a big thanks to you :)

One more question..
This script is going to be stored in a secure folder, should I add any other security snippets to the code to make it more safe?

Also, will this script handle 100's of emails or is it going to cause problems with timeouts etc?

jatar_k

3:06 pm on May 15, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



>> should I add any other security snippets to the code to make it more safe

that depends on how it will be run

as far as timeouts and things go, that all depends on your setup and your ISP. My isp used to block me after about 500 in a row so I had to break it up a bit. As far as script timeouts, I could send about 10K before I had to change the max execution but we were on our own servers so we were tweaked anyway.