Michael TGCM
Michael TGCM

Reputation: 45

PHPmailer is giving me a 504 gateway timeout error

i am getting a 504 gateway timeout error from my server when using phpmyadmin to send out indivudual emails to a list of about 1200 users

i need the emails to go out to the users one by one as i do not want expose any of the email addresses in the list to any other users

it seems to work well when i am sending out to a small list, but when i want to send to a large list i am getting this error

what would be the best way to send out one email, reset the request and send out the next email in the list as it goes through the loop?

Also i'd like to see message sent to [name] each time the email goes out

heres my php

require('connection.inc.php');
include ("PHPMailer/class.phpmailer.php");
include ("PHPMailer/class.smtp.php");
require 'PHPMailer/PHPMailerAutoload.php';

$mail = new PHPMailer();
if (isset($_POST['submit'])) {
    error_reporting(E_STRICT | E_ALL);
    date_default_timezone_set('Etc/UTC');
    $message = $_POST['message'];
    $UeMail = uniqid();
    $UDomain = uniqid();
    $domain = '@zzz.org';
    $mail = new PHPMailer;
    $mail->isSMTP();
    $mail->SMTPDebug = 0;
    $mail->Host = 'zzz.zzz.com';
    $mail->SMTPSecure = 'ssl';
    $mail->Port = 465;
    $mail->SMTPAuth = true;
    $mail->SMTPKeepAlive = true; // SMTP connection will not close after each email sent, reduces SMTP overhead
    $mail->Username = '[email protected]';
    $mail->Password = 'zzz';
    $mail->setFrom('zzz' . $UeMail . $domain);
    $mail->addReplyTo('[email protected]');
    $mail->WordWrap = 9999; // set word wrap
    // I ADDED THIS TO BYPASS SSL ERRORS
    $mail->SMTPOptions = array(
        'ssl' => array(
            'verify_peer' => false,
            'verify_peer_name' => false,
            'allow_self_signed' => true
        )
    );
    // SET ENVOIRMENT #1 (DONT FORGET TO SET #2)
    // LIVE ENVIORMENT EMAIL TO SMS
    $result = mysqli_query($con, "SELECT name, mob_email FROM `user` WHERE mob_email<>''");
    // TEST ENVIORMENT
    // $result = mysqli_query($con, "SELECT email, name FROM user_test WHERE email<>''");
    foreach($result as $row) {
        set_time_limit(60);
        $mail->Body = $message; //HTML Body
        $mail->AltBody = $message; //Text Body
        // SET ENVOIRMENT #2
        // LIVE ENVIORMENT EMAIL TO SMS
        $mail->addAddress($row['mob_email'], $row['name']);
        // TEST ENVIORMENT
        // $mail->addAddress($row['email'], $row['name']);
        if (!$mail->send()) {
            echo "Mailer Error (" . str_replace("@", "&#64;", $row["email"]) . ') ' . $mail->ErrorInfo . '<br />';
            break; //Abandon sending
        }
        else {
            echo "Message sent to :" . $row['name'] . ' (' . str_replace("@", "&#64;", $row['email']) . ')<br />';
            // Mark it as sent in the DB (NOT USING THIS RIGHT NOW)
            /*
            mysqli_query(
            $mysql,
            "UPDATE mailinglist SET sent = TRUE WHERE email = '" .
            mysqli_real_escape_string($mysql, $row['email']) . "'"
            );
            */
        }
        // Clear all addresses and attachments for next loop
        $mail->clearAddresses();
        $mail->clearAttachments();
    }
}

and heres my html

<!DOCTYPE html>
<html>
    <head>
        <title>Form submission</title>
    </head>
    <body>
        <form action="" method="post">
        Message:<br><textarea rows="5" name="message" cols="30" maxlength="110"></textarea><br>
        <input type="submit" name="submit" value="Submit">
        </form>
    </body>
</html>

all code is in a single file (sendEmail.php)

Upvotes: 0

Views: 1915

Answers (1)

Synchro
Synchro

Reputation: 37770

I can see that you've based your code on the PHPMailer mailing list example (though it looks like you may have started with an old version, so check you're using the latest), and it looks basically correct.

However, sending that many messages on page load is never going to work reliably. You need to run this from cron so that it is not subject to such timeouts. There's also no point in the call to set_time_limit(60); that will not extend the overall timeout you're hitting.

Also never disable SSL certificate verification unless you exactly why you are doing so - fix it properly. The PHPMailer troubleshooting guide gives lots of info about how to check what's wrong.

Upvotes: 1

Related Questions