Php mail() code in this situation

Started on the old forum in October 2013 · view the original in the archive · old links redirect here

4 replies · 653 views · started 13 years ago · 1 min read

4 participants
Ajayme Newbie · 8 posts #1

Hello, i wud like to send different data(1st store in my db) nd then send, to different users in my database with mail() function, can anyone tell me how can i achieve this? Example- there are 60 registered members in my site, so for each member i wud like to send a unique message, but all at once, so can anyone help me ? Thank u..

arnage Lead · 811 posts #2

Hi. 🙂

Like this, just change table names and maybe write a form to add message.

$emails = array(); 
$select = mysql_query('SELECT `email` FROM `users`'); 
while ($sel = mysql_fetch_array($select)) { 
    $emails[] = $sel[0]; 
} 
foreach ($emails as $email) { 
    $subject = 'Subject...'; 
    $message = 'Message...'; 
    $header = 'MIME-Version: 1.0'.PHP_EOL; 
    $header .= 'Content-type: text/html; charset=utf-8'.PHP_EOL; 
    $header .= 'From: mailing@mail.com'; 
     
    @mail($email, $subject, $message, $header); 

echo 'Sent!'; 
} 
<!DOCTYPE html PUBLIC "-//***WAPFORUM**.**RS***
Ghost Senior · 366 posts #3

i would recommend making sure the server your on. allows email sending and doesnt limit you to how many emails you send in a certain period of time. as with a function such as this it could be classed as mass mailing. (email bombing) even though going to many different email addresses. its a very quick way to getting your site/server account suspended.

be careful and make sure your allowed to do these things on server. goodluck and happy coding 🙂

<?php include ('Ghost'); if ($Post == true) { echo ' ![](http://destrukt.dk/ico/phpdeveloper.jpg) alt='coding-talk.com!!' />'; echo 'Sharing Is Caring!'; } else { echo ' ![](http://img98.imageshack.us/img98/6476/av3ro4.jpg) alt='the username GHOST has been comprimised!' />'; echo 'OMG SOMEBODY HELP ME!!'; } ?>
Ajayme Newbie · 8 posts #4

Ghost wrote: i would recommend making sure the server your on. allows email sending and doesnt limit you to how many emails you send in a certain period of time. as with a function such as this it could be classed as mass mailing. (email bombing) even though going to many different email addresses. its a very quick way to getting your site/server account suspended.

be careful and make sure your allowed to do these things on server. goodluck and happy coding :)

Thanks alot bro.. 🙂

Added after 2 minutes:

arnage, thanks bro, but i want to store the message(s) in database first and send to all.. Example, i wil store user1 msg as 'hello' and user2 msg as 'hi' user3 msg as 'welco' etc.., and i want to send email to all three at same time wid the messages(as body) assosiated wid each user in database, now can u help me?

just_m3. Developer · 199 posts #5

Ajayme wrote: Thanks alot bro.. 🙂

Added after 2 minutes:

arnage, thanks bro, but i want to store the message(s) in database first and send to all.. Example, i wil store user1 msg as 'hello' and user2 msg as 'hi' user3 msg as 'welco' etc.., and i want to send email to all three at same time wid the messages(as body) assosiated wid each user in database, now can u help me?

Build a submit form with 3 fields for user 1 2 3 (or a drop down to select the user and set message) then use mysql insert to store messages with email@ as id , after that create a link or php action to execute the code from bottom .

Mysql table "messagelist" with 2 fields

as id [email] => "demo@mailer.com" message=> "Hellow there how are you"
<?php 

$emails = array();  
$select = mysql_query('SELECT `email` FROM `users`');  
while ($sel = mysql_fetch_array($select)) {  
    $emails[] = $sel[0];  
}  

foreach ($emails as $email) {  

$select = mysql_fetch_array(mysql_query('SELECT message FROM messagelist WHERE email="'.$email.'" '));  

if(empty($select[0])){
// if is no message stored for email@
$selectmessage="Hellow there";
}

else{
// message from database for email@
$selectmessage=$select[0];
}

    $subject = 'Subject...';  
    $message = $selectmessage ; 
    $header = 'MIME-Version: 1.0'.PHP_EOL;  
    $header .= 'Content-type: text/html; charset=utf-8'.PHP_EOL;  
    $header .= 'From: mailing@mail.com';  
      
    @mail($email, $subject, $message, $header);  

echo 'Sent!';  
}  

?>

This is ten percent luck, twenty percent skill Fifteen percent concentrated power of will Five percent pleasure, fifty percent pain 😂And a hundred percent reason to remember the name!

Sign in to reply.

Similar topics

Keyboard shortcuts

g f
Go to forums
g h
Go to your feed
g m
Go to messages
r
Reply (in a topic)
/
Search
j
Next post
k
Previous post
?
This list
Ctrl/⌘ + Enter
Send what you are writing
Ctrl/⌘ + B / I
Bold / italic while writing
Esc
Close