-2

I have a database containing more than 2000 contacts, I want to use the mass mailing, but I do not know how to do it, I used this code but I think it is not useful because the number of mails is important and mail() can't work for mass mailing.

<?php

require ("bdd.php");

$sql = "SELECT adresse FROM newsletter WHERE envoie='1';";
$result = mysql_query($sql);

while($row = mysql_fetch_array($result))
{
     $en_tete ='From: '. $row['adresse'] .''."\n";
     $en_tete .='Reply-To: adresse@fai.fr'."\n";
     $en_tete .='Content-Type: text/html; charset="iso-8859-1"'."\n";
     $en_tete .='Content-Transfer-Encoding: 8bit';
     $contenu ='<html><head><title>Envoie html</title></head><body><p>TEST !</p></body></html>';

     mail($row['adresse'], 'Envoie mail', $contenu,  $en_tete)
}

mysql_close();

?>
AymDev
  • 6,626
  • 4
  • 29
  • 52
  • For one, you have a parse error and is probably at the root of all this. – Funk Forty Niner Aug 29 '18 at 14:52
  • What error you are facing? –  Aug 29 '18 at 14:52
  • Why are you closing your connection then reopening it again after? There's no need. – Funk Forty Niner Aug 29 '18 at 14:53
  • i am asking if there is any other solution that can replace mail() to can send more than 2000 mails with no problems – Hamdi Jdidi Aug 29 '18 at 14:53
  • If deliverability is important, you should not try to use the internal mail function (as it's much more likely for your mails to be regarded as spam while in transit or upon arriving at the destination servers). Use a trusted email delivery platform like Mailgun. It's free for low monthly volumes and very easy to implement – Javier Larroulet Aug 29 '18 at 14:54
  • your host is probably not letting you send out that many emails in one go. Again; parse error `mail($row['adresse'], 'Envoie mail', $contenu, $en_tete)` <<< there. – Funk Forty Niner Aug 29 '18 at 14:54
  • Other solution you say? There are many; use a service that will handle that many emails. You very well might be blacklisted if you plan on doing this yourself. – Funk Forty Niner Aug 29 '18 at 14:55
  • @FunkFortyNiner thankyou and i can integrate Mailgun in my web solution ? – Hamdi Jdidi Aug 29 '18 at 14:56
  • Give it a go but they might have some restrictions/throttling. I don't remember how they work though but I have gone through the docs a few years ago. – Funk Forty Niner Aug 29 '18 at 14:57
  • @FunkFortyNiner i am looking to buy a professional email from ovh – Hamdi Jdidi Aug 29 '18 at 14:57
  • adding a sleep of 2 seconds or 3 after each e-mail sending to not be detected like spamer – Inazo Aug 29 '18 at 15:04

1 Answers1

0

You can use PHPMAILER

I don't know whether below solution is useful for you or not.. But I am just suggesting alternative thinking to send mass emails without any issue like spamming or hosting provider's limit of email per hour etc.....

========

Additionally you can use your query with limit and update database column when email sent...

e.g.

Add additional column 'email_sent' in your database table...

and change query to

$sql = "SELECT adresse FROM newsletter WHERE envoie='1' and email_sent='0' order by id desc limit 25"; 
 // Limit can be any...10 or 25 or 50 etc... this will limit total emails at a time...


//in while clause, add query to Update database >> `email_sent`=>1...

With cronjob you can reset email_sent to 0 again....or with simple link to script which contains query to reset / update email_sent to 0 for all 2000+ rows can be used...

Please avoid mysql...... Try Mysqli or PDO rather..