Getting around email restrictions with hosting providers - php

So my hosting provider is limiting emails sent out to 2 at a time, which means no array of emails.Right now I'm using Pear to send html emails to multiple users who sign up together.
Is there a solution to loop through the mail() function multiple times ultimately sending out multiple emails? How will the performance be?
The hosting company actually suggested we look for some solution to by pass their restrictions, but i don't know where to look.
Thanks

PEAR's Mail_Queue package comes to mind here.

You could just iterate over the array of email addresses? I assume your use case is more complex than this, but it should get you on the right path:
<?php
foreach ($emails_array as $email)
{
mail($email, ....); //or whatever your mail call is
}
?>

You can use Cron job to setup sending out when the hourly limit hits.
or,
you can use mail chimp or constant contact to do the job for you.

Related

Sending Bulk emails in Laravel 5.3

I need to send like 4000+ emails but individual emails to each using. Doing a foreach takes forever and the page loads until it finish (What really end up doing is an error).
I need to send those emails the best way possible and just leave the job in the background and return a response while that is happening.
If I didn't explain myself correctly, let me know.
Thanks a lot!
If you are looking for the best way to sending emails, I would highly suggest using a third party service.
There are many email services that guarantees sending your emails accurately to receivers inbox. Emails we send from our own email services provided by hosting providers mostly wind up in receivers spam folder. And also queuing up that much emails is not a solution as it would also bring up the question of what would happen if queue service failed.
I recently worked with a useful service which is named sendgrid. It helped me send that much emails without that much trouble.
You only need to loop though your data to format recipient and email content. Then send that data to sendgrid and they handle the emails.
refer this for more info.

PHP newsletter mailer

I want to make a PHP newsletter sender for a few hundred people, say monthly.Does it work for me to send all the emails through a for() loop? i.e. will it use an excessive amount of resources? Also, should I sleep() for a period between each email I send? Note: to send the emails, i use the PEAR Mail::factory('smtp' ...); function.
If you're sending to a couple of hundred people, there shouldn't be too many problems.
Things to bear in mind are whether you're going to be sending individual emails, or if you're sending the same email to multiple people using BCC - the former will mean you send fewer emails, but you can't then personalise them at all.
You can definitely loop through the database with a for loop; if you're going to be sending individual emails, then I would recommend the occasional sleep(), too, just so you don't hit the mailserver too hard. It might be worth talking to them first, to let them know - they'd probably appreciate knowing that you're going to be doing that, especially if your newsletter subscribers grow into the thousands.
You can use a for loop for sending 100 emails, but I don't know about the sleep(). I have sent 200 mails at a time using PHP mailer in a for loop.

Sending bulk email in PHP

I want to send mail to hundreds of email addresses in PHP using php mail function.
How can I do that?
You can also use SwiftMailer
Please also look at a similar post here
You can try phplist
It will be a better option
I wouldn't try writing your own bulk mailer, unless you have a lot of time. There is allot of issues to cover including throttling emails, sending emails out into small batches so AOL and other service providers don't consider your server as SPAMMING people. Also you want to probably include tracking for opened emails, track bounced emails, clicked links, etc.
I would look at some open source options such as www.phplist.com which is a very good open source program for doing bulk emails.
Or on the commercial side take a look at Interspire Email Markerer, it's a very slick product and well worth the money, plus you can host it on your own servers.
www.interspire.com/emailmarketer/
You must use PHP? If so, I'd use PHPMailer, I have a bit of experience with it and it hasn't failed me for bulk email sending.
Alternatively, if you have access to the server, look at using Postfix or Sendmail, they are better options for that quantity of mail.
If you're sending a lot of mail, you don't want to do it all # once or else you will consume too many resources.
Here is a Tutorial on how to use the Mail_Queue component of the PEAR library: http://pear.php.net/manual/en/package.mail.mail-queue.mail-queue.tutorial.php
It basically will incrementally send segments of your email blast without throttling your server.
Use PHPMailer library, it has a lot of functionality. Store the email addresses as an array and using for loop just keep adding each email address as a string to the mail->AddAddress field. It will take care of sending the bulk mail. I have only used this to send bulk mails to 6/7 addresses at a time. It should also work for hundreds of addresses.
You can use swiftmailer for it. By using batch process.
<?php
$message = Swift_Message::newInstance()
->setSubject('Let\'s get together today.')
->setFrom(array('myfrom#domain.com' => 'From Me'))
->setBody('Here is the message itself')
->addPart('<b>Test message being sent!!</b>', 'text/html')
;
$data = mysql_query('SELECT first, last, email FROM users WHERE is_active=1') or die(mysql_error());
while($row = mysql_fetch_assoc($data))
{
$message->addTo($row['email'], $row['first'] . ' ' . $row['last']);
}
$message->batchSend();
?>

Sending mass email using PHP

I am currently writing a music blog. The administrator posts a new article every 2-3 days. Once the administrator posts an article, a mass email will be sent to around 5000 subscribers immediately.
What is the best way to implement the mass mail feature?
Does the following function work?
function massmail()
{
$content = '...';
foreach ($recipients as $r) {
$_content = $content . '<img src="http://xxx/trackOpenRate.php?id='.$r.'">';
mail($r, 'subject', $_content);
}
}
Another question: If all 5000 subscribers are using Yahoo Mail, will Yahoo treat it as a DDOS attack and block the IP address of my SMTP server?
First off, using the mail() function that comes with PHP is not an optimal solution. It is easily marked as spammed, and you need to set up header to ensure that you are sending HTML emails correctly. As for whether the code snippet will work, it would, but I doubt you will get HTML code inside it correctly without specifying extra headers
I'll suggest you take a look at SwiftMailer, which has HTML support, support for different mime types and SMTP authentication (which is less likely to mark your mail as spam).
I would insert all the emails into a database (sort of like a queue), then process them one at a time as you have done in your code (if you want to use swiftmailer or phpmailer etc, you can do that too.)
After each mail is sent, update the database to record the date/time it was sent.
By putting them in the database first you have
a record of who you sent it to
if your script times out or fails and you have to run it again, then you won't end up sending the same email out to people twice
you can run the send process from a cron job and do a batch at a time, so that your mail server is not overwhelmed, and keep track of what has been sent
Keep in mind, how to automate bounced emails or invalid emails so they can automatically removed from your list.
If you are sending that many emails you are bound to get a few bounces.
This is advice, not an answer: You are much, much better off using dedicated mailing list software. mailman is an oft-used example, but something as simple as mlmmj may suffice. Sending mass mails is actually a more difficult task than it actually appears to be. Not only do you have to send the mails, you also have to keep track of "dead" addresses to avoid your mail, or worse, your mailserver, being marked as spam.
You have to handle people unsubscribing for much the same reason.
You can implement these things yourself, but particularly bounce handling is difficult and unrewarding work. Using a mailing list manager will make things a lot easier.
As for how to make your mail palatable for yahoo, that is another matter entirely. For all its faults, they seem to put great stock in SPF and DomainKey. You probably will have to implement them, which will require co-operation from your mail server administrator.
You may consider using CRON for that kind of operation. Sending mass mail at once is certainly not good, it may be detected as spam, ddos, crash your server etc.
So CRON could be a great solution, send 100 mails at once, then wait a few minutes, next 100, etc.
Do not send email to 5,000 people using standard PHP tools. You'll get banned by most ISPs in seconds and never even know it. You should either use some mailing lists software or an Email Service Provider do to this.
Why don't you rather use phplist? It's also built on top of PHP Mailer and a lot of industry leaders are using it. I've used it myself a couple of times to send out bulk mails to my clients. The nice thing about phplist is that you can throttle your messages on a domain level plus a time limit level.
What we've also done with a couple of internal capture systems we've got was to push our user base to the mailling list and then have a cron entry triggering a given mail each day. The possibilities are endless, that's the awesome thing about open source!
Also the Pear packages:
http://pear.php.net/package/Mail_Mime
http://pear.php.net/package/Mail
http://pear.php.net/package/Mail_Queue
sob.
PS: DO NOT use mail() to send those 5000 emails. In addition to what everyone else said, it is extremely inefficient since mail() creates a separate socket per email set, even to the same MTA.
Also have a look at the PHPmailer class. PHPMailer
You can use swiftmailer for it. By using batch process.
<?php
$message = Swift_Message::newInstance()
->setSubject('Let\'s get together today.')
->setFrom(array('myfrom#domain.com' => 'From Me'))
->setBody('Here is the message itself')
->addPart('<b>Test message being sent!!</b>', 'text/html');
$data = mysql_query('SELECT first, last, email FROM users WHERE is_active=1') or die(mysql_error());
while($row = mysql_fetch_assoc($data))
{
$message->addTo($row['email'], $row['first'] . ' ' . $row['last']);
}
$message->batchSend();
?>
I already did it using Lotus Notus and PHP.
This solution works if you have access to the mail server or you can request something to the mail server Administrator:
1) Create a group in the mail server: Sales Department
2) Assign to the group the accounts you need to be in the group
3) Assign an internet address to the group: salesdept#DOMAIN.com
4) Create your PHP script using the mail function:
$to = "salesdept#DOMAIN.com";
mail($to, $subject, $message, $headers);
It worked for me and all the accounts included in the group receive the mail.
The best of the lucks.
There is more into it aside from using a software. If you could create a bulk emailer program that sends intermittently. Say if you will send 5,000 recipients, create a loop that would send 38 lists per sending then pause for 10 seconds. I have an actual experience sending 500 manually per days for the past weeks and so far i have good results.
Another consideration are the content of your email. Nowadays it is a standard that you need to put your physical office address and the "unsubscribe" opt-out. These are factors that majority of recipient emails servers are checking. If you don't have these they will classify you as spammer.
Mailchimp is my best recommendation to use if you want a paid service provider in sending to your email subscriber NOT sending unsolicited or cold email marketing.
Hope it helps.

Is there a limit when using php mail function?

I am using php and mysql.
I am going to send 10k++ (ten thousands plus) emails to update my subscribers, and this is the first time I am going to send them. I will use php mail function, basically here is what I will do:
First get the data from database:
Select name, email FROM data
After that, using while loop to send the data:
while($r = mysql_fetch_assoc($exe)){
...
if($mail){
echo "success<br>";
} else {
echo "failed<br>";
}
}
echo "Sent all";
I include the if.. else statement, to ensure that each email is sent successfully. Is there anything I need to take care of? Will I have any problems when SENDING TO 10K++ users?
Is there a limit of numbers of emails that you are going to sent?
Please be aware of this note from the mail documentation:
Note: It is worth noting that the mail() function is not suitable for larger volumes of email in a loop. This function opens and closes an SMTP socket for each email, which is not very efficient.
For the sending of large amounts of email, see the » PEAR::Mail, and » PEAR::Mail_Queue packages.
No limit to the emails number, but there is the time limit of the PHP script. See the max_execution_time set in your php.ini, typically it's 20 or 30 seconds. If you don't know that, use phpinfo() to find it out.
Moreover, you should take some steps to prevent users from getting too much emails. You should mark them as sent, so they don't receive double posts if you accidentally start the script twice.
Other than that, you should note that php's mail function is inherently not optimized at all. You could try some libraries, like phpmimemessage or any other, which will allow you do to some caching, for example, among many other features.
You should build a queue of emails sent/failed, so you can try to resend failed attempts and avoid re-sending emails if something should go wrong.
Do not create a loop that tries to send 10k emails via mail()
Also, the most likely limit you'll hit will be that of the mail server of your ISP or host.
You may also want to look at setting up a "real" mailing list tool, such as mailman, or at least using alias groups (if possible).
Also, see the related questions on serverfault: https://serverfault.com/questions/67154/sending-an-email-to-about-10k-users-not-spam, where PHPlist is mentioned, along with others. And here - https://serverfault.com/questions/68357/whats-the-best-way-to-send-bulk-email.
You can use pear::Mail_Queue http://pear.php.net/package/Mail_Queue/
It will really do a good job.

Categories