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 !

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

The "$users" variable needs to be a comma separate list of email addresses. Such as "a@a.com, b@b.com, c@c.com".

 

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

Link to comment
Share on other sites

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 ! :)

Link to comment
Share on other sites

The "$users" variable needs to be a comma separate list of email addresses. Such as "a@a.com, b@b.com, c@c.com".

 

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 !

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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