There are 500 unique users in our system and we have created a notification system that will send all the users a plain-text email when there is an update to one of the sections. The system uses swiftmailer and creates an email object and then BCCs the 500 users before sending it.
I just want some reassurance that BCCing 500 users means the server will consider this as sending out 1 email but to a lot of users. I don't want to run into any email limit restrictions set by my server host.
It will not count as one email. The fact that the content of the message is the same is irrelevant, as you still have 500 recipients. In addition, your host's email server probably caps the number of BCC recipients per message at a more reasonable value, so I'd be surprised if you could even do this at all. This sort of blast should be sent via individual messages. If your host balks at the volume, you'll likely have to go to a delivery service like Constant Contact et al.
I also tried to send 1000 emails using this method of BCC and it seemed that the limit for me was around 100 emails. So don't use this. Instead try to use the plugin that SwiftMailer provides.
Here is some sample code:
$mailer = Swift_Mailer::newInstance(
Swift_SmtpTransport::newInstance('smtp.example.org', 25)
);
// Use AntiFlood to re-connect after 100 emails
$mailer->registerPlugin(new Swift_Plugins_AntiFloodPlugin(100));
// And specify a time in seconds to pause for (30 secs)
$mailer->registerPlugin(new Swift_Plugins_AntiFloodPlugin(100, 30));
$content = 'email body'
$message = Swift_Message::newInstance('Email subject')
->setFrom(array('no-reply#email.com'=> 'From me'))
->setBody($content,'text/html');
$emails = array('email1#email.com','email2#email.com','email3#email.com');
foreach($emails as $recipient){
$message->setTo($recipient);
$mailer->send($message);
}
I hope this helps with your problem as a simpler solution.
Related
I am using Laravel 5.5 and Mailgun. I want to send email campaigns to over 700 recipients and likely this list will grow into the thousands. Currently I am looping through each email address and sending the emails one at a time. As a result usually only about 530 emails go out. I have searched the net and cannot seem to find a good explanation on how to approach this in a more efficient way so that all the emails are sent. Any suggestions would be greatly appreciated.
public function mailCampaign()
{
//Code to get all email data in JSON
$emails= json_decode($data->getBody());
$baseUrl = config('constants.base_url');
foreach($emails as $key => $email){
Mail::to($email)
->send(new EmailInstance($variable, $email, $baseUrl));
}
$data = ['message' => 'Success. Emails have been sent.'];
return response()->view('emails.mail', $data, 200);
}
You shouldn't send emails one by one. Use Mailgun API instead. You can send thousands of emails using API with Bogardo/Mailgun package.
Mailgun supports the ability send to a group of recipients through a single API call. This is achieved by specifying multiple recipient email addresses as to parameters and using Recipient Variables.
I'm trying to send an email to more then 2000 email ids at a time but It's throwing 502 Bad Gateway error.
If I send an email to within 600 mails it's working fine but it's taking 10 minutes time to send to all. please help me if anyone has know about it.
Here my code
foreach ($submail as $mail) {
$email = new Email();
$email->template('abc');
$email->emailFormat('both');
$email->from(['abc#abc.com' => 'abc']);
$email->to($mail);
$email->subject('abc');
if ($email->send()) {
} else {
}
}
It might be better create a queue for your emails, and use a CLI worker to actually send your emails. Check out one of the many plugins:
lorenzo-cakephp-email-queue
nodesagency-cakephp-email-queue
etc.
queueing your emails and using a CLI worker dedicated to sending the emails will open up some extra opportunities like sending the emails at a specific time with cronjobs etc.
Im using swiftmailer for sending mails via PHP. Most times it works fine. But sometimes, my mail Mails are landing in Spam-Folder.
Here my code, which sends the mails
function sendMail2($from,$to,$subject,$body,$attachment=NULL) {
require_once 'include_apth/swiftmailer/swift_required.php';
$transport = Swift_MailTransport::newInstance();
$mailer = Swift_Mailer::newInstance($transport);
$message = Swift_Message::newInstance($subject);
$message->setFrom($from);
$message->setTo($to);
$message->setBody($body, 'text/html');
if($attachment) {
$message->attach(Swift_Attachment::fromPath($attachment));
}
if(#$mailer->send($message)) {
return true;
}
else {
return false;
}
}
any ideas, why its landing sometimes in spam-folder?
I was having the same issues with deliver-ability of emails. Getting all of the correct DNS settings, headers and the like isn't enough.
Most, if not all cloud-hosting, and home-ISPs IP ranges are on various lists of IPs from where emails are not expected to be sent from - and so they are more likely to be marked as spam.
The easiest way to solve that is to use a dedicated service where emails are well known to come from, and that the company spends a great deal of effort to get email delivery properly configured.
There are a number of well known such companies, many of which offer significant free tiers, as long as you are well behaved and send appropriate emails that aren't being marked as spam, or bounce. If you are hosted on Amazon EC2 for example, you can get over 60,000 emails delivered per month via AWS/SES. My my own systems, I have an account, currently free, with Mailgun, and a 'limit' of 10,000 email sends per month.
For Swiftmailer, there are a number of plugins that can, for example, use a HTTP API to send email to the service, which then is sent over SMTP in the usual way - with greatly improved deliverabilty.
Add the below code and it will work perfectly
$headers =& $message->getHeaders();
$headers->addIdHeader('Message-ID', "b3eb7202-d2f1-11e4-b9d6-1681e6b88ec1#domain.com");
$headers->addTextHeader('MIME-Version', '1.0');
$headers->addTextHeader('X-Mailer', 'PHP v' . phpversion());
$headers->addParameterizedHeader('Content-type', 'text/html', ['charset' => 'utf-8']);
Solution get from the below question
Swiftmailer mails go into SPAM Folder
I need to send over 2000 mails and I am using Swift Mailer library for it.
We have own server and it has both SMTP and sendmail transports. I am using SMTP:
$transport = Swift_SmtpTransport::newInstance('localhost', 25);
All mails are send fine to few people, but I'm afraid that we will be banned when we send mass mail.
I don't really know what means "banned" and how it looks like, but I'm afraid about the aftermath.
So, is it true, that such "ban" exists and how to implement mass mailing with Swift Mailer in a right way?
P.S.: My code looks like:
// Create the Transport
$transport = Swift_SmtpTransport::newInstance('localhost', 25);
// Create the Mailer using your created Transport
$mailer = Swift_Mailer::newInstance($transport);
// Create a message
$message = Swift_Message::newInstance($message_theme)
->setFrom(array($sender => $name))
->setTo($emails)
->setBody($message_text,"text/html")
;
try {
// Send the message
$result = $mailer->send($message);
}
catch(Exception $e) {
echo "Error: ".$e->getMessage();
}
As I'm hoping you will not use this for spam!!!
Here are some things to do:
try to same different Emails (change name of the recipient in body)
send emails once every 3-4 seconds and not 100 emails/second - it should send 2000 emails in about 2-3 hours.
Blacklisting/graylisting does indeed exist and there are some best practices you can implement to try to avoid these issues. For 2,000 emails, as long as your headers are legitimate, there is nothing fishy going on in your body text, and your recipients are on different domains, you should not run into this issue. However, as khomyakoshka mentions, the above code is incorrect and you should use a loop to send each email. This avoids exposing your entire mail list to each user.
Some additional best practices:
1) Swiftmailer offers plugins (http://swiftmailer.org/docs/plugins.html) that will help you send bulk email. Of particular note are the Throttler and AntiFlood plugins.
2) If you intend on modifying the contents of the mail to tailor to the recipient, consider using the Decorator plugin (also mentioned on the plugins page) for this task.
Hope these tips help.
When sending mass mails with PHP, is it better to send each subscriber an e-mail (running a for loop through all the e-mail addresses) or is it better to just add all in BCC in a comma separated list and thus sending only one e-mail?
Thank you.
There's a good chance the number of addresses in the BCC field is limited on the SMTP server (to avoid spamming). I'd go with the safe route and send an e-mail to each individual subscriber. That will also allow you to customize the e-mail for each subscriber if needed.
Also note that mail() is probably not the best way to send bulk mail (due to the fact that it opens a new connection to the SMTP server each time it's invoked). You may want to look into PEAR::Mail.
Best practice is to send an email per recipient.
If it's a linux mail server, it can handle massive throughputs so volume should not be an issue unless it's a crap server!
If it's a shared webserver your host may not be happy - if this si the case I'd split it into chuncks and spread the send. If it's dedicated then do as you will :)
If the sending process for some reason failed (example cause might me unresolvable domain) for one of the BCC recipients, the whole operation would be canceled (which is in 99% of cases unwanted behavior).
I you send the emails in a PHP loop, even if one of the emails fails to send, other emails will be sent.
As the others says one mail per recipient is the better fit.
If you want a library to do the dirty job for you, give a try to SwiftMailer http://swiftmailer.org
Here is an example directly from the docs:
require_once 'lib/swift_required.php';
//Create the Transport
$transport = Swift_SmtpTransport::newInstance('localhost', 25);
//Create the Mailer using your created Transport
$mailer = Swift_Mailer::newInstance($transport);
//Create a message
$message = Swift_Message::newInstance('Wonderful Subject')
->setFrom(array('john#doe.com' => 'John Doe'))
->setTo(array('receiver#domain.org', 'other#domain.org' => 'A name'))
->setBody('Here is the message itself')
;
//Send the message
$numSent = $mailer->batchSend($message);
printf("Sent %d messages\n", $numSent);
/* Note that often that only the boolean equivalent of the
return value is of concern (zero indicates FALSE)
if ($mailer->batchSend($message))
{
echo "Sent\n";
}
else
{
echo "Failed\n";
}
*/
It also has a nice Antiflood plugin: http://swiftmailer.org/docs/antiflood-plugin-howto