1

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.

1
  • are you running the code in local or in web server? Commented Sep 6, 2012 at 15:23

5 Answers 5

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

when i take out the "if" statements, the code works perfect, with the "if" statements nothing works
Ahh.. yes per one of the other answers, you are only retrieving the email filed from the DB with your query, you are not selecting the 'period" field, so there would be no value in $row['period']
1

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

2 Comments

i just made the mistake when i posted it :(
no problemo :) - after select * it is still not working? If not basically what is happening is that period is being stored in a different format in your database than what is derived using date(N) What value does $row['period'] have?
0

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

Comments

0

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.

Comments

0

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);
     }

}   
 ?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.