Jump to content

[SOLVED] [help] teach me how to send mail through email()


samoi

Recommended Posts

hello guys,

 

I have a database, and it contains users with emails and other stuff.

 

I need to send them emails by the updates in my site, can I?

 

I don't want to send them one by one, I want to send them all in once!

 

should I use something like,

 


<?php
$users = "SELECT email FROM tableusers";
$SQL_users = mysql_query($users);

$send = mail($users, "subject", "email message");

if ($send) {
    while ($me = mysql_fetch_assoc($send)) {
        echo $me['username'];
        echo "-----";
        echo $me['email'];
        echo "---- Has been contacted";
    }
} else {
    echo "Failed";
}

?>

 

 

I just made up this code right now, to be able to delevier my question !

 

Thank you in advance !

Why not loop through a select statement?

 

$users = "SELECT email FROM tableusers";
$SQL_users = mysql_query($users);
while($row = mysql_fetch_array($SQL_users)) {
mail({$row['email']}, "subject" , "email message");
}

 

The "$users" variable needs to be a comma separate list of email addresses. Such as "[email protected], [email protected], [email protected]".

 

Here is the function I use to retrieve email addresses from my database and turn them into a coma separated list:

 

function calendar_mail()
{
//Database connection
$conn = mysql_connect(DB_HOST, DB_USER, DB_PASS) or die('Error connecting to mysql');
mysql_select_db(DB_NAME);

$sql = 'SELECT user_email FROM phpbb_users WHERE user_notify_calendar="1" AND user_email!=""';
$result = mysql_query($sql);

while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
$recepients = $recepients.$row['user_email'] . ", ";
}
$recepients = rtrim($recepients, " ,");
return $recepients;
}

 

The examples on the php website are also very helpful: http://us.php.net/function.mail

Why not loop through a select statement?

 

$users = "SELECT email FROM tableusers";
$SQL_users = mysql_query($users);
while($row = mysql_fetch_array($SQL_users)) {
mail({$row['email']}, "subject" , "email message");
}

 

 

thank you for your help, I found a tutorial ! :)

The "$users" variable needs to be a comma separate list of email addresses. Such as "[email protected], [email protected], [email protected]".

 

Here is the function I use to retrieve email addresses from my database and turn them into a coma separated list:

 

function calendar_mail()
{
//Database connection
$conn = mysql_connect(DB_HOST, DB_USER, DB_PASS) or die('Error connecting to mysql');
mysql_select_db(DB_NAME);

$sql = 'SELECT user_email FROM phpbb_users WHERE user_notify_calendar="1" AND user_email!=""';
$result = mysql_query($sql);

while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
$recepients = $recepients.$row['user_email'] . ", ";
}
$recepients = rtrim($recepients, " ,");
return $recepients;
}

 

The examples on the php website are also very helpful: http://us.php.net/function.mail

 

 

Thank you too !

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.