简体   繁体   中英

how to send multiple email using php

I have a button that when pressed, it will email updates on the emails that are in the database.

This is my code. I also got this from a forum but i don't know what's wrong that it don't get the emails in the database:

$form_action = $_POST['form_action'];
if ($form_action == 'REGISTER') {

//send to your self
$to ="admin@admin.com";

$subject = 'System Email';

// message
$message = "<html>".
"<head>".
" <title>System Email</title>".
"</head>".
"<body>".
"message here!".
"</body>".
"</html>";

//get email list

//open database connection
$username = "root";
$password = ""; //input your password here.
$database = "dbase";
//connect to database
$link=mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die("<b>Unable to specified database</b>");

$query="SELECT email_address FROM db_other_details";
$result=mysql_query($query) or die('Error, query failed');
mysql_close($link);

$row=1;
$numrows=mysql_num_rows($result);
$bccfield="Bcc: ". mysql_result($result,0,"email_address");
while($row<$numrows)
{
$email=mysql_result($result,$row,"email_address");
$bccfield .= "," . $email; //seperate by comma 
$row++;
}
$bccfield .= "\r\n";

// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= $bccfield;

$headers .= 'From: System <admin@admin.com>' . "\r\n";

// Mail it
mail($to, $subject, $message,$headers);

 if (mail($to,$subject,$message,$headers)) {
   header("Location: ".WEBSITE_URL."email-all.html?result=success");
} else {
    header("Location: ".WEBSITE_URL."email-all.html?result=failed");
}
}

switch (trim($_GET['result'])) {
    case "failed":
        $sendmail_result    = "failed";
        $form_message       = "Cannot send email to " . $numrows . "registrants at this time. Please try again later.";
        break;
    case "success":
        $sendmail_result    = "success";
        $form_message       = "Email sent to" . $numrows ." registrants.";
}

Any suggestions on how to do this? I checked some other topics here with same question but of no help.

Thanks!

1) Use PDO.

2) Make sure you have error messages on for php.

3) var_dump / print_r your variables & GET requests to see if the data is available.

4) Check mysql error log for potential query errors.

5) UTF-8 encode your data before sending them with php::mail() function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM