1

I'm trying to send emails to multiple address pulled from SQL. I came up with following script but no luck. I know that using mail() or sendmail is not best choice, if anybody can point out where I went wrong or have a better solution using phpmailer that would be brilliant.

PS: I do not want to use SMTP as we are using an address that we will not be monitoring. The Do-NOT-REPLY one, actually we don't have email service at all. But server does support mail/sendmail/phpmailer, all tested.

<HTML> 
   <TITLE>Email Notification</TITLE> 
<?php

include "subscribe/mySQL.class.php"; //Connect to SQL 

if ($subject) { 
    $mailaddress = "DO-NOT-REPLY@domain.my";
    $query = "select email from subscribe"; 
    $res = mysql_query($query); 
    $row = mysql_fetch_array($res); 

    while ($row) { 

        mail($row['email'],$subject,$text."n ","From:".$mailaddress); 
        $row = mysql_fetch_array($res);

    } 

    echo "<script type='text/javascript'>"; 
    echo "parent.location.href='welcome.php'"; 
    echo "</script>";} 
?> 
<BODY> 
<P ALIGN=CENTER><FONT FACE="Arial" SIZE="7" COLOR="#FF0000">Send Notifications<BR><BR></FONT> 
<P ALIGN=LEFT><FORM NAME="email" ACTION="test.php" METHOD="POST"> 
<FONT FACE="Arial" SIZE="6" COLOR="#0000FF">Subject:<INPUT TYPE=TEXT NAME="subject" SIZE="50" MAXLENGTH="18" value=<?php echo $subject ?>><BR><BR> 
Content: <TEXTAREA NAME="text" COLS="90" ROWS="3" value="<?php echo $text?>"> </TEXTAREA><BR><BR> 
</FONT> 
<INPUT TYPE=SUBMIT VALUE=Send Email></FORM> 
</BODY> 
</HTML>
ajtrichards
  • 29,723
  • 13
  • 94
  • 101
Godinall
  • 2,280
  • 1
  • 13
  • 18
  • If only we knew what output you are getting and what you expect to get. `I came up with following script but no luck.` and `if anybody can point out where I went wrong`. This means we have to start from scratch debugging your code. A little more detail would be useful and you might get a quick reply. – Touch Oct 04 '13 at 13:53
  • Thanks for the comment, to clarify, above code gave me nothing, however, if I replace the SQL part with given multiple address say $to = 'a@example.com' . ', '; $to .= 'b@example.com'; this will work and I can get test emails. But in this way, I have to paste all emails from database, what's more, all recipients will see each other in the address line. Hope this makes sense. – Godinall Oct 04 '13 at 14:03
  • Ok. I made an edit. Just check if solves your problem. – Touch Oct 04 '13 at 14:15

1 Answers1

0

You see nothing because you don't print anything to help you see that the email was sent.

Try using: mysql_fetch_assoc() instead of mysql_fetch_array() and see how it works for you.

//Sorry, but I am a fan of mysql_fetch_assoc 
//$row = mysql_fetch_array($res); 

while ($row = mysql_fetch_assoc($res)) { //Here is my main change

    if (mail($row['email'],$subject,$text."n ","From:".$mailaddress)) {
       echo '<hr>Message sent to: '.$row['email'].'<br>'; //Just to see that it was sent.
    }

    //$row = mysql_fetch_array($res); //Since I put this in the while test

} 

The mail() function returns a boolean value, so test that value and print something so that you can know if the mails was succesfully sent.

Let me know if this is not problem, that way I can either delete this answer or edit it accordingly.

Touch
  • 1,481
  • 10
  • 19
  • Thank you very much for your prompt reply, I tried your change but no out put – Godinall Oct 04 '13 at 14:22
  • Try `print_r($row)` inside the while loop just to see if it even has the email address. – Touch Oct 04 '13 at 14:25
  • To confirm no email address was printed. – Godinall Oct 04 '13 at 14:30
  • That could be the reason. It was not provided an email. Just make sure that you have emails in your database and if so, I think the problem is in retrieve the email address from the database. Just make sure that you are able to retrieve data from your MySQL(or whatever database you use). – Touch Oct 04 '13 at 14:43
  • No idea why connection to database failed, used exactly the same lib for other functions, now changed to PDO and problem solved, thank you very much! For your time and professional answers. If you can improve my initial code when convenient to include data connection that would be better as I'm new but keen to find out where I went wrong. Thank you again. Ken – Godinall Oct 04 '13 at 15:12