Php mailer bulk email 500 error

Hello, I try to send an email to all my users once a condition is met, it was working fine at start, but when the database got bigger, crashes started happening each time this is action is triggered.

$mailem2="SELECT DISTINCT * from $rows ";
$postem2=mysqli_query($db, $mailem2);

while ($whosen2=mysqli_fetch_assoc($postem2)) {
$wherto2= $whosen2['email'];
$mail2 = new PHPMailer();
$mail2->isSMTP();


$mail2->SMTPAuth = true;
$mail2->SMTPSecure = "ssl"; //ssl
  $mail2->Port = 465; //587
  $mail2->Host = "smtp.gmail.com";
$mail2->Username = "contact@****";
$mail2->Password = "***********";
$inc3=' ClĂ´ture: ';
$eninc3=utf8_decode($inc3);
$mail2->setFrom('contact@***.');
$mail2->addAddress($wherto2);
$mail2->Subject = "$eninc3 $afna ";
$mail2->isHTML(true);
$mail2->Body = "

<!DOCTYPE html>
<html lang='fr'>
<head>

<title></title>
<html  lang='en' xml:lang='en'>
<meta name='viewport' content='width=device-width, initial-scale=1'>

<style type='text/css'>
    /* FONTS */
    @media screen {
        @font-face {
          font-family: 'Lato';
          font-style: normal;
          font-weight: 400;
          src: local('Lato Regular'), local('Lato-Regular'), url( format('woff');
        }

        @font-face {
          font-family: 'Lato';
          font-style: normal;
          font-weight: 700;
          src: local('Lato Bold'), local('Lato-Bold'), url() format('woff');
        }

        @font-face {
          font-family: 'Lato';
          font-style: italic;
          font-weight: 400;
          src: local('Lato Italic'), local('Lato-Italic'), url() format('woff');
        }

        @font-face {
          font-family: 'Lato';
          font-style: italic;
          font-weight: 700;
          src: local('Lato Bold Italic'), local('Lato-BoldItalic'), url() format('woff');
        }
    }

    /* CLIENT-SPECIFIC STYLES */
    body, table, td, a { -webkit-text-size-adjust: 100%; -ms-text-size-adjust: 100%; }
    table, td { mso-table-lspace: 0pt; mso-table-rspace: 0pt; }
    img { -ms-interpolation-mode: bicubic; }

    /* RESET STYLES */
    img { border: 0; height: auto; line-height: 100%; outline: none; text-decoration: none; }
    table { border-collapse: collapse !important; }
    body { height: 100% !important; margin: 0 !important; padding: 0 !important; width: 100% !important; }

    /* iOS BLUE LINKS */
    a[x-apple-data-detectors] {
        color: inherit !important;
        text-decoration: none !important;
        font-size: inherit !important;
        font-family: inherit !important;
        font-weight: inherit !important;
        line-height: inherit !important;
    }

    /* MOBILE STYLES */
    @media screen and (max-width:600px){
        h1 {
            font-size: 32px !important;
            line-height: 32px !important;
        }
    }

    /* ANDROID CENTER FIX */
    div[style*='margin: 16px 0;'] { margin: 0 !important; }
</style>
</head>
<body style='background-color: #f4f4f4; margin: 0 !important; padding: 0 !important;'>

<!-- HIDDEN PREHEADER TEXT -->
<div style='display: none; font-size: 1px; color: #fefefe; line-height: 1px;  Helvetica, Arial, sans-serif; max-height: 0px; max-width: 0px; opacity: 0; overflow: hidden;'>
    ClĂ´ture imminente, faites vite pour prendre votre place dans la tombola!
</div>

<table border='0' cellpadding='0' cellspacing='0' width='100%'>
    <!-- LOGO -->
    <tr>
        <td bgcolor='#7CBEB3' align='center'>
            <!--[if (gte mso 9)|(IE)]>
            <table align='center' border='0' cellspacing='0' cellpadding='0' width='600'>
            <tr>
            <td align='center' valign='top' width='600'>
            <![endif]-->
            <table border='0' cellpadding='0' cellspacing='0' width='100%' style='max-width: 600px;' >
                <tr>
                    <td align='center' valign='top' style='padding: 40px 10px 40px 10px;'>
                        <a href='' target='_blank'>
                            <img alt='Logo' src='' width='270' height='140' style='display: block; width: 40px; max-width: 40px; min-width: 40px;  Helvetica, Arial, sans-serif; color: #ffffff; font-size: 18px;' border='0'>
                        </a>
                    </td>
                </tr>
            </table>
            <!--[if (gte mso 9)|(IE)]>
            </td>
            </tr>
            </table>
            <![endif]-->
        </td>
    </tr>
    <!-- HERO -->
    <tr>
        <td bgcolor='#7CBEB3' align='center' style='padding: 0px 10px 0px 10px;'>
            <!--[if (gte mso 9)|(IE)]>
            <table align='center' border='0' cellspacing='0' cellpadding='0' width='600'>
            <tr>
            <td align='center' valign='top' width='600'>
            <![endif]-->
            <table border='0' cellpadding='0' cellspacing='0' width='100%' style='max-width: 600px;' >
                <tr>
                    <td bgcolor='#ffffff' align='center' valign='top' style='padding: 40px 20px 20px 20px; border-radius: 4px 4px 0px 0px; color: #111111;  Helvetica, Arial, sans-serif; font-size: 48px; font-weight: 400; letter-spacing: 4px; line-height: 48px;'>
                      <h1 style='font-size: 48px; font-weight: 400; margin: 0;'>Plus que queleques places!</h1>
                    </td>
                </tr>
            </table>
            <!--[if (gte mso 9)|(IE)]>
            </td>
            </tr>
            </table>
            <![endif]-->
        </td>
    </tr>
    <!-- COPY BLOCK -->
    <tr>
        <td bgcolor='#f4f4f4' align='center' style='padding: 0px 10px 0px 10px;'>
            <!--[if (gte mso 9)|(IE)]>
            <table align='center' border='0' cellspacing='0' cellpadding='0' width='600'>
            <tr>
            <td align='center' valign='top' width='600'>
            <![endif]-->
            <table border='0' cellpadding='0' cellspacing='0' width='100%' style='max-width: 600px;' >
              <!-- COPY -->
              <tr>
                <td bgcolor='#ffffff' align='left' style='padding: 20px 30px 40px 30px; color: #666666;  Helvetica, Arial, sans-serif; font-size: 18px; font-weight: 400; line-height: 25px;' >
                  <p style='margin: 20px;'>la tombola $afna est bientôt terminée. Cliquez sur le boutton ci-dessous pour y participer . Faites vite, plus que quelques places disponible</p>
                </td>
              </tr>
              <!-- BULLETPROOF BUTTON -->
              <tr>
                <td bgcolor='#ffffff' align='left'>
                  <table width='100%' border='0' cellspacing='0' cellpadding='0'>
                    <tr>
                      <td bgcolor='#ffffff' align='center' style='padding: 20px 30px 60px 30px;'>
                        <table border='0' cellspacing='0' cellpadding='0'>
                          <tr>
                              <td align='center' style='border-radius: 3px;' bgcolor='#7CBEB3'><a href='' target='_blank' style='font-size: 20px; font-family: Helvetica, Arial, sans-serif; color: #ffffff; text-decoration: none; color: #ffffff; text-decoration: none; padding: 15px 25px; border-radius: 2px; border: 1px solid #7CBEB3; display: inline-block;'>Voir plus</a></td>
                          </tr>
                        </table>
                      </td>
                    </tr>
                  </table>
                </td>
              </tr>

              <!-- COPY -->

              <!-- COPY -->
              <tr>
                <td bgcolor='#ffffff' align='left' style='padding: 0px 30px 20px 30px; color: #666666;  Helvetica, Arial, sans-serif; font-size: 18px; font-weight: 400; line-height: 25px;' >
                  <p style='margin: 20px;'>Vous avez une question? Envoyez votre demande en réponse à cet email.</p>
                </td>
              </tr>
              <!-- COPY -->
              <tr>
                <td bgcolor='#ffffff' align='left' style='padding: 0px 30px 40px 30px; border-radius: 0px 0px 4px 4px; color: #666666;  Helvetica, Arial, sans-serif; font-size: 18px; font-weight: 400; line-height: 25px;' >
                  <p style='margin:20px;'>Cheers,<br>Service clients </p>
                </td>
              </tr>
            </table>
            <!--[if (gte mso 9)|(IE)]>
            </td>
            </tr>
            </table>
            <![endif]-->
        </td>
    </tr>
    <!-- SUPPORT CALLOUT -->
    <tr>
        <td bgcolor='#f4f4f4' align='center' style='padding: 30px 10px 0px 10px;'>
            <!--[if (gte mso 9)|(IE)]>
            <table align='center' border='0' cellspacing='0' cellpadding='0' width='600'>
            <tr>
            <td align='center' valign='top' width='600'>
            <![endif]-->
            <table border='0' cellpadding='0' cellspacing='0' width='100%' style='max-width: 600px;' >
                <!-- HEADLINE -->

            </table>
            <!--[if (gte mso 9)|(IE)]>
            </td>
            </tr>
            </table>
            <![endif]-->
        </td>
    </tr>
    <!-- FOOTER -->
    <tr>
        <td bgcolor='#f4f4f4' align='center' style='padding: 0px 10px 0px 10px;'>
            <!--[if (gte mso 9)|(IE)]>
            <table align='center' border='0' cellspacing='0' cellpadding='0' width='600'>
            <tr>
            <td align='center' valign='top' width='600'>
            <![endif]-->
            <table border='0' cellpadding='0' cellspacing='0' width='100%' style='max-width: 600px;' >
              <!-- NAVIGATION -->
              <tr>
                <td bgcolor='#f4f4f4' align='left' style='padding: 30px 30px 30px 30px; color: #666666;  Helvetica, Arial, sans-serif; font-size: 14px; font-weight: 400; line-height: 18px;' >
                  <p style='margin: 20px;'>
                    <a href='' target='_blank' style='color: #111111; font-weight: 700;'><img width='37px' src='' alt=''></a>
                    <a href='' target='_blank' style='color: #111111; font-weight: 700;'><img width='37px' src='' alt=''></a>
                    <a href='' target='_blank' style='color: #111111; font-weight: 700;'><img width='37px' src='' alt=''></a>
                  </p>
                </td>
              </tr>
              <!-- PERMISSION REMINDER -->
              <tr>
                <td bgcolor='#f4f4f4' align='left' style='padding: 0px 30px 30px 30px; color: #666666;  Helvetica, Arial, sans-serif; font-size: 14px; font-weight: 400; line-height: 18px;' >
                  <p style='margin: 20px;'>Vous avez reçu cet email parceque vous êtes inscrits à la tombola $afna.</p>
                </td>
              </tr>

            </table>
            <!--[if (gte mso 9)|(IE)]>
            </td>
            </tr>
            </table>
            <![endif]-->
        </td>
    </tr>
</table>

</body>
</html>

";
$mail2->send();
}

any idea on how to notify all users no matter how much they are with this code, without getting 500 error for all website. Thanks for your help!

First you should have a look at the logfile at your hoster to determine what the error message is. If you get some timeout your script takes to long for this. Bulk email can be very tricky on larger databases, so you should think about using a queue worker or taking an external service provider, as your hoster may also restrict sending many emails in a short period.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.