Momo95
Erfahrenes Mitglied
Hallo zusammen,
ich habe ein kleines Newsletter-Tool für meinen Verein geschrieben. Teilweise gehen so 400 Emails raus.
In einem PHP-Script lade ich mir alle Email-Adressen in ein js-Array und rufe mit Ajax für jeweil ein weiteres PHP-Script mit einer Mail-Function auf.
Nach den ersten 300(+/-) Emails wird das ganze furchtbar langsam.
Ausschnitt des Scripts ohne HTML:
sendMail.php:
Woran könnte es liegen, dass es so langsam wird. Ist vielleicht auch die Datenbank überlastet und ich sollte den Mailtext über Ajax übergeben? Oder macht der Server irgenwann "dicht".
Vielen Dank und Grüße
Momo
ich habe ein kleines Newsletter-Tool für meinen Verein geschrieben. Teilweise gehen so 400 Emails raus.
In einem PHP-Script lade ich mir alle Email-Adressen in ein js-Array und rufe mit Ajax für jeweil ein weiteres PHP-Script mit einer Mail-Function auf.
Nach den ersten 300(+/-) Emails wird das ganze furchtbar langsam.
Ausschnitt des Scripts ohne HTML:
Code:
$ergebnis = mysqli_query($db, "SELECT DISTINCT email FROM email ".$query);
while($row = mysqli_fetch_object($ergebnis))
{
echo 'emails.push("'.$row->email.'");';
}
}
?>
var anzahl = emails.length;
var sent = 0;
$.each( emails, function( index, value ){
$.ajax({
method: "POST",
url: "sendMail.php",
data: { absender: "<?php echo $absender; ?>", email: value, id: "<?php echo $id; ?>"},
cache: false,
async: false
})
.done(function( msg ) {
sent++;
var valeur = Math.round(sent/anzahl*100);
$('.progress-bar').css('width', valeur+'%').attr('aria-valuenow', valeur).html(sent+"/"+anzahl);
if(sent == anzahl) {
$('#back_button').prop('disabled', false);
}
});
});
});
sendMail.php:
PHP:
function encrypt_decrypt($action, $string) {
$output = false;
$encrypt_method = "AES-256-CBC";
$secret_key = 'This is my secret key';
$secret_iv = 'This is my secret iv';
// hash
$key = hash('sha256', $secret_key);
// iv - encrypt method AES-256-CBC expects 16 bytes - else you will get a warning
$iv = substr(hash('sha256', $secret_iv), 0, 16);
if( $action == 'encrypt' ) {
$output = openssl_encrypt($string, $encrypt_method, $key, 0, $iv);
$output = base64_encode($output);
}
else if( $action == 'decrypt' ){
$output = openssl_decrypt(base64_decode($string), $encrypt_method, $key, 0, $iv);
}
return $output;
}
include("connect.php");
include("MailFunction.php");
$absender = $_POST['absender'];
$email = $_POST['email'];
$id = $_POST['id'];
$sql = "SELECT `text`, `betreff` FROM `newsletter_sent` WHERE `id` = $id";
$result = mysqli_query($db, $sql);
$row = mysqli_fetch_object($result);
$betreff = $row->betreff;
$text = $row->text;
$hashmail = encrypt_decrypt('encrypt', $email);
$text = str_replace("##HASHMAIL##", $hashmail, $text);
//function email ($absender, $empfaenger, $betreff, $mailtext)
email($absender, $email, $betreff, $text);
Woran könnte es liegen, dass es so langsam wird. Ist vielleicht auch die Datenbank überlastet und ich sollte den Mailtext über Ajax übergeben? Oder macht der Server irgenwann "dicht".
Vielen Dank und Grüße
Momo