will adding bcc to php mail save resources? - php

I am writing an admin notification sytem for my custom CMS. So I am planning of adding a bcc to all the users of sites. So here are my questions?
Is bcc better than the foreach($user){mail()} function? in terms of server resource usage?
How many bcc's will the server support?
Thank you.

A direct mail to a user is usually the 'nicer' option, as it allows you to personalize it. The advantage for you is that you may get the chance to get some feedback by adding a unique id for each mail to each url that links back to your site. Using bcc is just one mail (from the view of your php server), but you shouldn't really worry about server resources here.
Limits for bcc seem to depend on your provider. I have seen limits ranging from 5 too 500.
Sending too many mails at once may also not work due php processing time limits. For many users you will have to split the sending in both cases.

PHP is just the transporter of your request to the SMTP Server, so there is no limitations in PHP What so ever.
You can check your SMTP Servers limitations to see how many you can actually attach the the email.
using bin carbon copies will not really save you that much resources as the SMTP Server Still has to send an email to all attached recipients, this being said the only thing BCC Does for you is hide the email addresses in each email dispatched.

BCC is, lets say, hidden recipients. So if you'll add recipients to BCC users will receive your message, but their emails will not be displayed in "to" or "copy" list.
Max count of BCC recipients depends on mail server.

Well, this only works if you send the exactly same message with no personal/individual information in it, like all would receive this same message:
hello world
and not
Dear Mr. XY
or
Dear Ms. AB

Related

Difference between PHP mail function and using email delivery service like Sendgrid, Mandrill

I am building a program and need to send multiple emails based on certain events. I have coded using the PHP mail function but was advised against its use.
Can somebody explain the difference between the workings of the PHP mail function vs using commercial services like Sendgrid, Mandrill etc? Why would one use these services when I can very well use the free PHP mail function to send mails?
Bulk mailing and campaign management is a complex organizational and technological challenge but will not be helped by most opensource php lib for emails (which simply add html formatting and support for attachments).
They won't handle IPR, SPF, spam trap avoidance, bounce handling, throttling, subscription management and more. Which good bulk emaillers provide.
If you know what you are doing, have full admin rights on your system and NS records and time to write the code, then, yes, you could build the same service - but its not just comparing ants to elephants - its comparing a spanner to an orange.
You cannot send bulk emails in one go using this function.
For example, if you want to send emails to 100k users, you will have to iterate over the emails ids. Yes, you can use the cc though but what if you have a template where in every email the user name will be added like:
Dear User abc
You will have to send one by one. You can't achieve this burst email shoot feature in mail function unless you use multiple processes.
MailQueue is good but I have used it, it does the same things, saves the emails into the database and dequeues the emails and send them one by one.
Again we have swift mailer too.
Again, this is on your requirement. If you don't need to send hundreds of notifications that are time sensitive, you don't need these tools then.
comparing mail() to commercial services is like comparing ant to elephant. mail() is good for some fast notifications from the server if you dont need lots of customisation, but if you want to do something like email campaigns you would have to do a lot of additional server setup and build tons of custom functionallity arround your mail() function. Commercial services are good for campaigns but not notifications (waste of money imho). Try using some opensource php lib for emails, that will have a lot of that custom functionality sorted for you.
First of all, by profession I am a part of a global ESP "Pepipost" which helps brands in delivering the business critical triggered and transactional emails to their customers at the most .
But, the intend of myself here answering this question is not to promote an ESP over PHP mail function. I will share you some facts which we observed during our research at Pepipost Labs.
PHP mail function is just a basic method/the core function to generate the email. But, it cannot deliver your emails at a scale, neither it can help in better deliverability, both of these are key to any businesses.
With the evolution of more sophisticated technologies and algorithms, ISPs (Gmail, yahoo, Outlook and others) and Spam Filters are getting more and more smarter. Before reaching the mailbox, each email passes through all these filters where the content, reputation and other parameters are scanned. Hence, its very important to have good reputed IPs and envelope domains.
Once the email reaches the recipient server either it will get deliver, throttle or bounce. In case of Bounce, Spam Complaint, Unsubscribe, the recipient server will communicate your server, and in that case you need to handle all these properly else the reputation of your domain and IP will goes down.
In case of throttling, another set of complex deliver logics required.
In short, if you are planning to send few hundred emails, then PHP mail function might be ok to use, but to scale further it's important to use a good ESP (Email Service Provider).
Mail() function works very simply on *nix systems, it just executes local command sendmail to send message. It does have disadvantages like:
do not work in many cheap hosting environments
You do not have any information if message is accepted by sending MTA, it could be rejected or quarantined or stay forever in local queue
in some environments it could generate garbled headers because of wrong EOL characters (\r\r\n instead of \r\n)
I think You should use open source library like PHPMailer or Swift Mailer, because it has many more functionality besides simply sending mail.
Some advantages of these libraries:
You could easily send HTML mail with attachments and images
You could send messages through external SMTP services
they have DKIM and S/MIME signing support
PS. I do not use commercial services like Sendgrid, Mandrill, thus can't compare.

Confusion about mail in CakePHP

I'm confused about how mail works in PHP and CakePHP.
1.) What is the difference between sending an email either using the PHP mail function / CakePHP email helper or SMPTP as shown here: http://book.cakephp.org/1.3/en/view/1290/Sending-A-Message-Using-SMTP as the outcome looks the same?
2.) To specify who the email is coming from, you pass in the email in the header, but you can put whatever you want, so what is stopping you from just putting in anything? like yourbank.com? mail('you#gmail.com', "Subject", "Message", "From: <dave#yourbank.com>"); I just tried it and it worked fine and I couldn't find out anyway in Gmail to see if it didn't come from dave at yourbank.com...
Hopefully I can get some light on these two questions. Thanks.
1). CakePHP has a bunch of helpers & functionality implemented to make life easier when developing applications. As you've discovered, Cake has mail functionality. I suggest reading this whole page http://book.cakephp.org/2.0/en/core-utility-libraries/email.html (It's 2.0 not 1.3, so please not there have been some big alterations between the two versions). The article covers in depth on why you may configure something in a particular manner.
CakePHP is using the default mail function with PHP. It's just allow you to incorporate views into the content and configure the outgoing mail in a much easier manner.
2) As for putting in potentially any email address within the From Header.... this can potentially fall under the category of Email Spoofing, essentially sending an email when it's not authorized from the source (From Header). Again this links back to configuring specific mail servers.
By default mail clients and generally setup to prevent spam and junk, this is done by taking a large amount of steps. Some may be..
Keyword checking, (Checking the contents of an email for any
keywords classified as spam).
Header checking, <--- This is the one that answers your
question.
Essentially... headers are examined and checked to see if the server that the mail was sent from has the authority to use the given from address.
As I don't have enough technical knowledge, i'll throw a few pages your way which discuss setting up records against your DNS/Domain so emails are validated correctly and not put within spam folders.
http://www.ipswitch.com/support/imail/guide/imailgsv8.1/Appendix%20A%20dns4.html
http://help.postageapp.com/kb/application-features/dkim-and-spf-setup-and-validation
How to properly set up DNS SPF records?
I hope my jumbled ramblings make some sort of sense.
Question 1: PHP mail function uses your own server's built in email functionality to send email. If you use SMTP, you're connecting to another server (eg. Google's mail servers) and using that server to send the email.
CakePHP's email component can use either PHP mail, or SMTP, depending on how you configure it.
The outcome is basically the same in many respects. Which way is best for you will depend on your set up, the volume of email you're sending, whether your own server has any restrictions with regards to sending mail, etc. If you Google "PHP mail versus SMTP" or similar, then you'll get some info to help you decide which is best for you.
If you're not sending much email, eg. if you're just wanting to send the results of an enquiry form that gets submitted a few times each day, then just use PHP mail and don't worry about it.
Question 2: Although email programs put various measures in place to make sure mail is legitimate, basically nothing stops fake emails completely. You can send Fake email. Check out this site: http://deadfake.com/Send.aspx and in particular, their FAQ section: http://deadfake.com/FAQ.aspx
Spam filters do their best to catch fake emails, but ultimately it's up to the end user to keep their wits about them, especially with banking emails!

How should I send emails to a long list? (PHP)

I figure I can either use one mail() command with a large Bcc list or have a loop that sends many individual emails.
I was just going to use Bcc since that is easiest to program and seemed easiest for the server to handle, but then I have to choose some address for the To field. I can just send mail to the websites own address but it would look more sensible to recipients if it was addressed to them. Also, it would be nice to customize each message by saying "Hello [firstname]" at the beginning.
I'm just concerned that sending to too many people will take too long. The maximum number of recipients will be 2000. Users on the website choose a list of people to send to, type a message, and press Send. Would they be waiting forever if it was sending to 2000 people? Would the server choke?
Also what considerations are there regarding mail servers thinking of this as spam?
EDIT: Apparently my client has an SMTP server he says can throttle the outgoing emails. Still not sure though if the PHP would be slow when sending to 1000+ people...
Sending large number of emails at once can really bog down your server, or if its a shared hosting, there is a limit to the number of emails that can be sent over an hour (with bluehost its 700 per hour). So i would recommend you to send emails in chunk.
Create table email_queue with two fields email_to and email_content. Now whenever you wish to send an email, just insert a record into this table with the email address that you wish to send the email to stored in the email_to column and the raw email content in the email_content column.
Next you would create a cron job that runs every hour, that cron job would check the email_queue table to see if it has to send any email, it will pick up 100 records from the email_queue table and send those 100 emails, when the emails have been send those 100 records would be deleted.
This I think would be an ideal way to send out emails in large numbers.
It's a pretty complex subject with respect to making sure the emails don't start looking like spam. You can really do yourself some favours by hooking it up to something like MailChimp.com and letting them deal with the nasty details for you.
I would actually recommend looking at a third party that can be accessed by an API. Mailing out large numbers of e-mails can be detrimental to your server as it can end up black listed. Check out a company like www.postmark.com or something similar that will throttle your message queue, manage white listed servers, etc.

Handling Incoming Mail to Multiple Recipients in PHP

Alright, this may take a moment or two to explain:
I'm working on creating an Email<>SMS Bridge (like Teleflip). I have a few set parameters to work in:
Dreamhost Webhosting
PHP 5 (without PEAR)
Postfix
MySQL (If Needed)
What I have right now, is a catch-all email address that forwards the email sent to a shell account. The shell account in turn forwards it to my PHP script.
The PHP script reads it, strips a few Email Headers in an effort to make sure it sends properly, then forwards it to the number specified as the recipient. 5551234567#sms.bridge.gvoms.com of course sends an SMS to +1 (555) 123-4567.
This works really well, as I am parsing the To field and grabbing just the email address it is sending to. However, what I realized that I did not account for is multiple recipients. For example, an email sent to both 5551234567 and 1235554567 (using the To line, the CC line, or any combination of those).
The way email works of course, is I get two emails received, end up parsing each of them separately, and 5551234567 ends up getting the same message twice.
What is the best way to handle this situation, so that each number specified in TO and CC can get one copy of the message.
In addition, though I doubt its possible: Is there a way to handle BCC the same way?
If you check the headers of the mail, you should find a Message-ID field (according to RFC2822 - section 3.6.4). So you could test if you have already sent an SMS for a mail with the same Message-ID & phone number to prevent sending the same message to the same number twice.
Why not use something like imap to check the catch-all mailbox, loop through the messages and then delete them once finished? That way you don't need to forward them to a seperate account.
Stupid dirty solution: parse all recipients from the mail, then send them SMS, then put em all into temporary table with md5 of message text. And check all incoming mails against this table.
Although wimvds had the best answer here, I found out elsewhere that Dreamhost includes a "X-DH-Original-To" header in the way I'm running it through the system. Using this, I'm able to send to each number individually upon receipt of the email without checking it against a database. This should also work with Blind Carbon Copy (I don't know the specifics of how email works enough to tell you how that works).

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.

Categories