With mail() function in PHP you can send email from your system to any email address easily. If you using a Linux system, you don’t have to set anything to use mail() function. But in Windows you need to add extra line in php.ini to specify your SMTP settings.
See the code below:
[mail function] ; For Win32 only. SMTP = "my.mailserver.dom" smtp_port = 25
But sending bulk emails using PHP mail() function, often consider spamming. As PHP mail() will send all the email at once during the iterations. The code is something like this:
while($data=mysql_fetch_array($sql)) //Fetch members email from database { $headers = "MIME-Version: 1.0" . "rn"; $headers .= "Content-type:text/html;charset=iso-8859-1" . "rn"; $headers .= 'From: Yourname <[email protected]>' . "rn"; $to = $data['name']. " <".$data['email'].">"; $res=mail($to,'Your subject here','Your message here',$headers); }
That method above will definitely catch as spam. Imagine that you have 10K of emails, and it will iterate 10K times to send it all. That code is not SAFE. Another technique is you create your own queuing system to send the email with schedule. But again it’s not very safe because you don’t use SMTP authentication when sending your email.
To use SMTP authentication in PHP, you can use 3rd party library called SwiftMailer. Swift Mailer integrates into any web app written in PHP 5, offering a flexible and elegant object-oriented approach to sending emails with a multitude of features.
SwiftMailer main features:
- Send emails using SMTP, sendmail, postfix or a custom Transport implementation of your own
- Support servers that require username & password and/or encryption
- Protect from header injection attacks without stripping request data content
- Send MIME compliant HTML/multipart emails
- Use event-driven plugins to customize the library
- Handle large attachments and inline/embedded images with low memory use
Sample code use SwiftMailer to send safe bulk email in PHP:
require_once './mailer/swift/lib/swift_required.php'; //include swiftmailer lib $transport = Swift_SmtpTransport::newInstance('SMTP host', 25) ->setUsername('SMTP username') ->setPassword('SMTP password'); //Create the Mailer using your created Transport $mailer = Swift_Mailer::newInstance($transport); /*Register anti flood plugin The AntiFlood plugin is designed to help lessen the load on the HTTP server and the SMTP server. It can also be used to send out very large batches of emails when the SMTP server has restrictions in place to limit the number of emails sent in one go. */ $mailer->registerPlugin(new Swift_Plugins_AntiFloodPlugin(100, 30)); while($allemailsent){ $message = Swift_Message::newInstance($subjectToSend) ->setFrom(array('your email' => 'your name')); $message->setBody($yourmessage, 'text/html'); $message->setTo(array($member_mail => $member_name)); //pull the trigger $mailer->send($message, $failures); }
That code will iterate untill all the email is sent. And with the Anti Flood Plugin, the email will stop for a given time when a certain emails count reach. For example the mailer will stop for 30 seconds after sending 100 emails. This will reduce the risk of catch as spammer.
Hope my code helps. For more documentation about SwiftMailer you can go to their official website.