使用PHP脚本,MySQL数据库发送大量电子邮件

时间:2022-10-23 17:16:59

I want to send mass emails using PHP mail function. Email addresses are stored in MySQL database. There are nearly 30k email addresses. I am sending one email at a time.

我想使用PHP邮件功能发送大量电子邮件。电子邮件地址存储在MySQL数据库中。有近30k的电子邮件地址。我一次发送一封电子邮件。

60 emails are sent properly & the next all emails are skipped. I am using godaddy host to send emails. Following is code I am using

正确发送60封电子邮件,然后跳过所有电子邮件。我正在使用godaddy主机发送电子邮件。以下是我正在使用的代码

<?php 

$con1=mysqli_connect("subscibe","subscibe","pw","subscibe");

$subject = $_POST['subject'];        
$message = $_POST['message'];

$getusers = mysql_query("SELECT * FROM subscibe.emaillist");
$email_from = "email@gmail.com";
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();

while($result = mysql_fetch_array($getusers)) {

$emailaddress = $result['Emailaddresses'];

mail($emailaddress,$subject,$message,$headers);

//Add email address to temp table
$sqlq="INSERT INTO subscibe.temp VALUES ('$emailaddress')";

if (!mysqli_query($con1,$sqlq)) {
die('Error: ' . mysqli_error($con1));
}
}

echo "Emails are sent"

?>

2 个解决方案

#1


you may want to use a foreach loop which would send the email to each record in your DB. Something like:

您可能希望使用foreach循环将电子邮件发送到数据库中的每条记录。就像是:

$getusers = mysql_query("SELECT * FROM subscibe.emaillist");
foreach ($getusers as $maillist) {
$mail->to($mailist['email_address']);
$mail->send()

#2


<?php 
$con = mysql_connect("localhost","dbuser","dbpass"); // replace dbuser, dbpass with your db user and password
mysql_select_db("dbname", $con); // replace dbname with your database name
/*
To use this script database table must have three fields named sno, email and sub_status
*/
$query = "select sno, email from dbtable where sub_status = 'SUBSCRIBED'";
$result = mysql_query($query, $con);
$emails = array();
$sno = array();
while($row=mysql_fetch_assoc($result))
{
    $sno[] = $row['sno']; // this will be used to unsubscribe the user
    $emails[]=$row['email']; // email id of user
}
/* you can also get email id data from CSV using below code */
//$file =  file_get_contents("travel_database.csv"); 
//$emails = explode(",",$file);
/* count.txt is used to store current email sent number/count */
$count =  file_get_contents("count.txt");
for($i=$count;$i<count($emails);$i++)
{
    $to  = $emails[$i];
    // subject
    $subject = 'Set Your Title Here';
    // message
    $message = file_get_contents("sample.html"); // this will get the HTML sample template sample.html
    $message .= '<p><a href="http://yourdomain.com/path-to-folder/unsubscribe.php?id='.$sno[$i].'&username='.$emails[$i].'">Please click here to unsubscribe.</a></p>
    </body>
    </html>';
    // 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";
    // Additional headers
    //$headers .= "To: $to" . "\r\n";
    $headers .= 'From: Name <info@yourdomain.com>' . "\r\n";
    //$headers .= 'Cc: sendcc@yourdomain.com' . "\r\n";
    //$headers .= 'Bcc: sendbcc@yourdomain.com' . "\r\n";
    // Mail it
    if(mail($to, $subject, $message, $headers)) {
        $file = fopen("mailsentlist.txt","a+"); // add email id to mailsentlist.txt to track the email sent
        fwrite($file, $to.",\r\n");
        fclose($file);
    }
    else
    {
        $file = fopen("notmailsentlist.txt","a+"); // add email to notmailsentlist.txt here which have sending email error
        fwrite($file, $to.",\r\n");
        fclose($file);
    }
    if(($i-$count)>=200) // this will send 200 mails from database per execution
    {   
        $filec = fopen("count.txt",'w'); // store current count to count.txt
        fwrite($filec, $i);
        fclose($filec);
        break;
    }
}//for end
$filec = fopen("count.txt",'w'); // store fine count to count.txt this will be used as a start point of next execution
fwrite($filec, $i);
fclose($filec);
?>

#1


you may want to use a foreach loop which would send the email to each record in your DB. Something like:

您可能希望使用foreach循环将电子邮件发送到数据库中的每条记录。就像是:

$getusers = mysql_query("SELECT * FROM subscibe.emaillist");
foreach ($getusers as $maillist) {
$mail->to($mailist['email_address']);
$mail->send()

#2


<?php 
$con = mysql_connect("localhost","dbuser","dbpass"); // replace dbuser, dbpass with your db user and password
mysql_select_db("dbname", $con); // replace dbname with your database name
/*
To use this script database table must have three fields named sno, email and sub_status
*/
$query = "select sno, email from dbtable where sub_status = 'SUBSCRIBED'";
$result = mysql_query($query, $con);
$emails = array();
$sno = array();
while($row=mysql_fetch_assoc($result))
{
    $sno[] = $row['sno']; // this will be used to unsubscribe the user
    $emails[]=$row['email']; // email id of user
}
/* you can also get email id data from CSV using below code */
//$file =  file_get_contents("travel_database.csv"); 
//$emails = explode(",",$file);
/* count.txt is used to store current email sent number/count */
$count =  file_get_contents("count.txt");
for($i=$count;$i<count($emails);$i++)
{
    $to  = $emails[$i];
    // subject
    $subject = 'Set Your Title Here';
    // message
    $message = file_get_contents("sample.html"); // this will get the HTML sample template sample.html
    $message .= '<p><a href="http://yourdomain.com/path-to-folder/unsubscribe.php?id='.$sno[$i].'&username='.$emails[$i].'">Please click here to unsubscribe.</a></p>
    </body>
    </html>';
    // 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";
    // Additional headers
    //$headers .= "To: $to" . "\r\n";
    $headers .= 'From: Name <info@yourdomain.com>' . "\r\n";
    //$headers .= 'Cc: sendcc@yourdomain.com' . "\r\n";
    //$headers .= 'Bcc: sendbcc@yourdomain.com' . "\r\n";
    // Mail it
    if(mail($to, $subject, $message, $headers)) {
        $file = fopen("mailsentlist.txt","a+"); // add email id to mailsentlist.txt to track the email sent
        fwrite($file, $to.",\r\n");
        fclose($file);
    }
    else
    {
        $file = fopen("notmailsentlist.txt","a+"); // add email to notmailsentlist.txt here which have sending email error
        fwrite($file, $to.",\r\n");
        fclose($file);
    }
    if(($i-$count)>=200) // this will send 200 mails from database per execution
    {   
        $filec = fopen("count.txt",'w'); // store current count to count.txt
        fwrite($filec, $i);
        fclose($filec);
        break;
    }
}//for end
$filec = fopen("count.txt",'w'); // store fine count to count.txt this will be used as a start point of next execution
fwrite($filec, $i);
fclose($filec);
?>