Ray Ray - 6 months ago 9
SQL Question

PHP - How to send emails to address on MYSQL?

How can I send emails to all emails on my database? E.g. here is my format of MYSQL.

MYSQL -- Table = users --- column = email. I need to send emails to everyone of the email on column "email".

Answer

Simple ready to use PHP script for sending mail from mysql data

<?php

mysql_connect("localhost", "mysql_user", "mysql_password") or
    die("Could not connect: " . mysql_error());
mysql_select_db("mydb");

$result = mysql_query("SELECT email FROM mytable");

while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
  sendMail($row[0]);
}
mysql_free_result($result);

function sendMail($to){
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster@example.com' . "\r\n" .
    'Reply-To: webmaster@example.com' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $message, $headers);
}

?>