tags:

views:

48

answers:

3

Hey,

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

Thanks if anyone can help me out.

+3  A: 

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: [email protected]' . "\r\n" .
    'Reply-To: [email protected]' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

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

?>
JapanPro
After mytable theres "email".
Raymond
mytable is your table name, email is your column of email , you can replace by your name.
JapanPro
:O You scripted for free~! :)
Aditya Menon
its will be paid if voted up or accepted as answer, ha ha:)
JapanPro
There we go :) hope that helps
Raymond
A: 

Do you know how to pull data from MySQL? if so, you should just execute the mail() function for each row.

mkroman
No, I do not know how to.
Raymond
A: 

The code that JapanPro suggested is great! However, I have read over the internet about doing this. If there is loads of emails to be sent it will take a long time and possible slow down your server and clog it up. I would recommend doing it in intervals. So like in sets of 10 or 20.

This is only based on the information I have read. And I can se where they are coming from.

hart1994