user1652443
user1652443

Reputation: 11

php email send list from mysql database

I am trying to create an email send list from a mysql database that will only send to the recieptant if it is the correct day, i want an individual email send per person, i can do it no problem if they are all in the "To: " field. Here is what I have no

 <?
 $freq=date("N");

//get email address's

$result = mysql_query("SELECT * FROM email_list ");

while($row = mysql_fetch_array($result))
{

 if($row['period']=="daily"){     
 $to="To: ".$row['name']." <".$row['email'].">\r\n";
 $subject="Your exchange rate update";
 $headers  = 'MIME-Version: 1.0' . "\r\n";
 $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
 $headers .="To: ".$row['name']." <".$row['email'].">\r\n";
 $headers .= "From: ME <[email protected]>" . "\r\n";
 mail($to, $subject, $email, $headers);
 }
 if($row['period']==$freq){
   $to="To: ".$row['name']." <".$row['email'].">\r\n";
 $subject="Your exchange rate update";
 $headers  = 'MIME-Version: 1.0' . "\r\n";
 $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
 $headers .="To: ".$row['name']." <".$row['email'].">\r\n";
 $headers .= "From: name <[email protected]>" . "\r\n";
 mail($to, $subject, $email, $headers);
     }

}   
 ?>

No emails are sent and get no error messages.

Upvotes: 1

Views: 2107

Answers (5)

raidenace
raidenace

Reputation: 12826

You are only selecting email field from table, but while generating the mail headers To address, you are trying to access both $row['name'] and $row['email'].

$row['name'] might be giving an undefined index warning, messing up headers and subsequent email sending.

UPDATE: I see that you have updated the query to select * instead of select email. As a tip, instead of silently updating the question like so, please update it with comments and also let the folks know if any of the change you did helped/changed the behavior based on the original code posted.

It becomes hard when someone looks at the code posted in the question and finds that the answers posted are totally not related to it..

Upvotes: 1

Abid Hussain
Abid Hussain

Reputation: 7762

try this

<?
 $freq=date("N");

//get email address's

$result = mysql_query("SELECT * FROM email_list ");

while($row = mysql_fetch_array($result))
{

 if($row['period']=="daily"){     
 $to=$row['name']." <".$row['email'].">\r\n";
 $subject="Your exchange rate update";
 $headers  = 'MIME-Version: 1.0' . "\r\n";
 $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
 $headers .="To: ".$row['name']." <".$row['email'].">\r\n";
 $headers .= "From: ME <[email protected]>" . "\r\n";
 mail($to, $subject, $email, $headers);
 }
 if($row['period']==$freq){
   $to=$row['name']." <".$row['email'].">\r\n";
 $subject="Your exchange rate update";
 $headers  = 'MIME-Version: 1.0' . "\r\n";
 $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
 $headers .="To: ".$row['name']." <".$row['email'].">\r\n";
 $headers .= "From: name <[email protected]>" . "\r\n";
 mail($to, $subject, $email, $headers);
     }

}   
 ?>

Upvotes: 0

Salketer
Salketer

Reputation: 15711

You select "email" only. $row['period'] is empty since it is not selected in your query. So both if are False, thus no e-mail is sent.

Upvotes: 0

Mike Brant
Mike Brant

Reputation: 71384

You don't put "To:" into the string that is passed as first parameter to mail().

Upvotes: 1

Doug Molineux
Doug Molineux

Reputation: 12431

When you SELECT email the $row array will only contain $row["email"]

Also try to echo the response of the mail function, and turn error reporting on:

error_reporting(-1);

You are also going to need a functioning mail server on the system this code is running on:

http://php.net/manual/en/function.mail.php

Upvotes: 0

Related Questions