1

i have a php blog system where user can publicly post on our blog after registering, now i was trying to send emails to all my users after a new post is published that a new post name "title example" has been published.

 $send = mysqli_query($con, "SELECT * FROM users");
 while($em = mysqli_fetch_array($send)) {

 phpmailer stuff here

 to = ("$em['email']"); here I'm fetching every email of user
  } while loop is closed here

i want to know is this correct way of sending email to all user? will this cause any load on server?

cssyphus
  • 37,875
  • 18
  • 96
  • 111

1 Answers1

0

Something like this will get you started.

$headers = "";
$headers .= "Content-type: text/html; charset=iso-8859-1\n";
$headers .= "From: your_from_email_address@yourdomain.com\n";
$headers .= "Organization: yourdomain.com\n";
$headers .= "X-Priority: 3\n";
$headers .= "X-Mailer: PHP". phpversion() ."\n" . PHP_EOL;

$msg_start = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head></head><body>';
$subj = 'Subject of email';
$msg = $msg_start . '<div style="color:red">This is teh email message right here</div>';

$all_users = mysqli_query($con, "SELECT * FROM users");
while($user = mysqli_fetch_array($all_users)) {
    $e = $user['email'];
    $result = mail($e, $subj, $msg, $headers);

} 

Note that with HTML in email messages, all styling must be inline. Unfortunately, you cannot include HTML tags like <style>div{background:wheat;}</style>.

cssyphus
  • 37,875
  • 18
  • 96
  • 111