Swift Mailer can address be banned for mass mailing? - php

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.

Related

Swiftmailer - PHP-Mails landing in Spam folder

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

Sending an email in php - swiftmailer

I've been searching for days if not weeks on how to automatically send a simple e-mail in php after a user completes my form. I've tried Pear, PHP mail, Swiftmailer, changed my php.ini, tried different servers and I'm going mad from none of it working. I have not successfully sent one e-mail yet. I've searched endlessly but I still have no idea what to do and why nothing is working.
At the moment I'm using Swiftmailer (second time round). I set up a test page with code:
<?php
require_once 'swift/lib/swift_required.php';
// CREATE TRANSPORT CONFIG
$transport = Swift_MailTransport::newInstance();
// CREATE MSG
$message = Swift_Message::newInstance();
// SET PRIORITY TO HIGH
$message->setPriority(2);
// SUBJECT
$message->setSubject('Subject');
// FROM
$message->setFrom(array('example#btopenworld.com'));
// TO
$message->setTo(array('example#googlemail.com'));
// EMAIL BODY
$message->setBody('Test');
// SEND
$mailer = Swift_Mailer::newInstance($transport);
$mailer->send($message);
if (!$mailer->send($message, $failures)) {
echo "Failures:";
print_r($failures);
}
?>
Looking at a similar post someone suggested the last part of the code to see errors and my error is:
Failures:Array ( [0] => example#googlemail.com )
No matter what e-mail I change it to (all using e-mails I have, so real e-mails) it doesn't work. If anyone has any help or suggestions it would be hugely appreciated.
example#btopenworld.com
Unless you are using a btopenworld server to send the E-Mail from, this is not going to work. The E-Mails from address needs to be associated with the server you are sending the message from. You can put the BT address into the reply-to header.
Also, SwiftMailer should have a "debug" switch telling you exactly what goes wrong, but I can't find it in the docs right now. Here is a logger plugin for Swiftmailer that should help if all else fails.

Swiftmailer BCCing 500 users

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.

PHP Sending mass mails: One for each or one for all?

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

Processing an Email Bounce back in CakePHP and Postfix

I'm trying to handle bounced message and send to a responsible System Administrator.
I use CakePHP Email Component to send the message. On server side, I use postfix to transport the message.
function sendAsEmail($data) {
$Email->sendAs = 'html';
$Email->from = $user['Sender']['username'] . '#example.com';
$Email->return = Configure::read('App.systemAdminEmail');
$Email->bcc = array($data['Message']['recipient_text']);
$content = 'Some content';
$Email->send($content);
}
As you can see above, I set the $Email->return to sysadmin's email which it will send all the bounced message.
On postfix configuration, I tried creating a bounce.cf template and set bounce_template_file. http://www.howtoforge.com/configure-custom-postfix-bounce-messages
How do I get the bounced message and send it to System Administrator?
I think what you'll need to do is to use an SMTP (or I suppose POP3) connector for PHP. Then you'll basically have to create your own PHP email client that will login to the server, ask for the messages that have been bounced, and parse them appropriately.
I would think there would be a CakePHP component for this, but I can't find one.
I would recommend that you use an Envelope Header in your email. Otherwise you'll be stuck trying to parse the recipient server bounce, and those are very very inconsistent. If you use the VERP (variable envelope return protocol?) header, you can encode a unique hash into the email address which should be really easy to parse out in your PHPEmailClient.
More info on VERP: http://en.wikipedia.org/wiki/Variable_envelope_return_path
Cake-specific VERP stuff: http://www.mainelydesign.com/blog/view/setting-envelope-from-header-cakephp-email-component
I also highly recommend that you look into using SwiftMailer. It has a lot of plugins; you might find a base PHP SMTP client that you can easily modify to do what you need. http://swiftmailer.org/

Categories