How To Build A Bulk Email Sending Application In PHP

44,605 views
44,130 views

Published on

Techniques, tools, best practices and tools to create a full blown bulk mailing application in PHP.

Published in: Technology
11 Comments
17 Likes
Statistics
Notes
No Downloads
Views
Total views
44,605
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
478
Comments
11
Likes
17
Embeds 0
No embeds

No notes for slide

How To Build A Bulk Email Sending Application In PHP

  1. 1. Sending Bulk Email Using PHP By Sudheer Satyanarayana http://techchorus.net
  2. 2. Table Of Contents <ul><ul><li>Bulk email challenges
  3. 3. Tools and techniques
  4. 4. Best practices
  5. 5. Sample code
  6. 6. Question and answers </li></ul></ul>
  7. 7. Challenges <ul><ul><li>User's expectations
  8. 8. Outgoing emails restrictions set by hosting providers
  9. 9. Incoming email filters set by recipient servers and their users
  10. 10. Compliance - legal and tacit agreements
  11. 11. Avoiding getting blacklisted </li></ul></ul>
  12. 12. Prerequisites <ul><ul><li>Opt-in list
  13. 13. Own email server for full blown solution </li></ul></ul><ul><ul><ul><li>Servers are becoming less expensive </li></ul></ul></ul><ul><ul><ul><ul><li>  Amazon - $0.085 per hour / INR 3.99 per hour
  14. 14.   Rackspace Cloud - 1.5¢ per hour / INR 0.7 per hour </li></ul></ul></ul></ul>
  15. 15. Identity And Branding <ul><ul><li>Use dedicated public static IP address
  16. 16. Have correct reverse DNS record for your IP address
  17. 17. Have up to date contact information in WHOIS record
  18. 18. Monitor postmaster and abuse mailboxes
  19. 19. Have a consistent from header
  20. 20. Have SPF records
  21. 21. Sign messages using DKIM </li></ul></ul>
  22. 22. Identity And Branding - Reverse DNS <ul><ul><li>Forward DNS lookup maps domain name to an IP address
  23. 23. Reverse DNS lookup maps IP address to a domain
  24. 24. Server hosting providers offer a way to set the reverse DNS entry in their control panels </li></ul></ul>Example 31.41.51.61 pointing to myserver.example.com
  25. 25. Headers Email contains two major sections Header   Header sections fields   Example fields - From, To, Subject, Date, Message-Id Body   Unstructured
  26. 26. Sample Email With Headers
  27. 27. Use A PHP Library Prefer SMTP over sendmail     Popular PHP libraries that support SMTP <ul><ul><li>Zend Framework - Zend_Mail
  28. 28. PHPMailer
  29. 29. Swift Mailer </li></ul></ul> 
  30. 30. Zend_Mail Sample Usage Configure SMTP Before You Send.   Use this snippet if SMTP authentication is not required  $config = array ( 'name' => 'myserver.example.com' ) ; $transport = new Zend_Mail_Transport_Smtp ( 'myserver.example.com' , $config ) ; Zend_Mail_Transport_Smtp ( 'myserver.server.com' , $config ) ; Zend_Mail:: setDefaultTransport ( $transport ) ;
  31. 31. Zend_Mail Sample Usage Use this snippet if SMTP authentication is required  $config = array (        'auth' => 'login' ,      'username' => 'myusername' ,      'password' => 'password' ) ; $transport = new Zend_Mail_Transport_Smtp ( 'myserver.server.com' , $config ) ; Zend_Mail:: setDefaultTransport ( $transport ) ;
  32. 32. Zend_Mail Sample Usage $mail = new Zend_Mail () ; $mail -> setBodyText ( 'Hello World!' ) ; $mail -> setFrom ( 'you@example.com' , 'You' ) ; $mail -> addTo ( 'me@example.com' , 'Me' ) ; $mail -> setSubject ( 'Greetings' ) ; $mail -> send () ; From, To, Subject headers are automatically set by Zend_Mail. Zend_Mail also handles MIME encoding.
  33. 33. Identity And Branding - SPF Record <ul><ul><li>Sender Policy Framework
  34. 34. Email Validation System
  35. 35. Have a text DNS record and publish your policy
  36. 36. Recipient servers validate sender by reading your DNS record </li></ul></ul>SPF Example: &quot;v=spf1 ip4:192.168.0.1/16 -all&quot; Allow any IP address between 192.168.0.1 and 192.168.255.255. Use the SPF record generator http://old.openspf.org/wizard.html
  37. 37. Identity And Branding - DKIM <ul><ul><li>DomainKeys Identified Mail  
  38. 38. A method for email authentication
  39. 39. Public key cryptography </li></ul></ul>Example Header DKIM-Signature: d=example.com s=myserver  b=MTI...I=;
  40. 40. List Management - Subscribing <ul><ul><li>Let users explicitly opt in to your lists
  41. 41. Verify the subscriber's email address
  42. 42. Consider double opt in
  43. 43. Avoid purchasing lists
  44. 44. Send subscription reminders to users. Offer methods to unsubscirbe from the lists the user may no longer be interested in. </li></ul></ul>     
  45. 45. List Management - Unsubscribing <ul><ul><li>A link in every email. Do not ask the user to login to unsibscribe
  46. 46. Do not force the user to fill in 'reason' field in the unsubscribe form field. Use unsubscribe form only for confirmation.
  47. 47. Provide a 'List-Unsubscribe' header
  48. 48. Automatically unsubscribe users whose addresses have reached a bounce threshold. Use VERP. </li></ul></ul> 
  49. 49. List Management - Unsubscribing List-Unsubscribe Header $mail = new Zend_Mail () ; $mail -> setBodyText ( 'Hello World!' ) ; $mail -> setFrom ( 'you@example.com' , 'You' ) ; $mail -> addTo ( 'me@example.com' , 'Me' ) ; $mail -> setSubject ( 'Greetings' ) ; $mail -> addHeader ( 'List-Unsubscribe', 'mylist-unsubscribe@my.example.com' ) ; $mail -> send () ;
  50. 50. List Management - Unsubscribing List-Unsubscribe Header With Unique Identifier $mail -> addHeader ( 'List-Unsubscribe', 'mylist-unsubscribe+myuniqueidentifier@my.example.com' ) ;
  51. 51. List Management - Unsubscribing <ul><ul><li>Return-Path Header
  52. 52. Return-Path header's value is usually same as From
  53. 53. Depends on your email server setting
  54. 54. When an email is bounced the details are sent to the email address specified in the Return-Path header </li></ul></ul>Return-Path: me@my.example.com From: me@my.example.com
  55. 55. List Management - Unsubscribing <ul><ul><li>VERP - Variable envelope return path
  56. 56. Automatic Bounce Handling
  57. 57. Make use of Return-Path header
  58. 58. Return-Path: me+specially_encoded_recipients_email@my.example.com
  59. 59. Server must support VERP </li></ul></ul> 
  60. 60. List Management - Unsubscribing Without VERP   Return-Path: me@my.example.com With VERP Return-Path: me+you=you.example.com@my.example.com Use POP to fetch mails of me@my.example.com and parse the Return-Path header.
  61. 61. List Management - Unsubscribing Automatic Bounce Handling - VERP Fetching mail using POP   $mail = new Zend_Mail_Storage_Pop3 ( array (        'host'      => 'localhost' ,      'user'      => 'test' ,      'password' => 'test' )) ; foreach ($mail as $messageNumber=>$message) { $headers = $message->getHeaders(); $to = $headers['to']; //... }
  62. 62. Delivering To Inbox <ul><ul><li>Ask the user to add your email address set in the from header to their address books
  63. 63. If falsely flagged as SPAM, ask the users to flag the email as not SPAM
  64. 64. Use separate from email address for transactional emails and promotional emails
  65. 65. Use separate IP address for transactional emails and promotional emails
  66. 66. Use separate domains for transactional emails and promotional emails </li></ul></ul>
  67. 67. Settings To Administer The Program <ul><ul><li>Number of outgoing emails per hour
  68. 68. Number of outgoing emails per hour per domain
  69. 69. List of blacklisted domains
  70. 70. Delay between outgoing emails
  71. 71. Process bounce handling every __ hours
  72. 72. Bounce treshold </li></ul></ul>
  73. 73. Tips And Tricks <ul><ul><li>Log every action
  74. 74. Respond to user's complaints
  75. 75. Apply for whitelists when you have a problem </li></ul></ul>Sample entities subscriber - email, preferred format, name list - name list_subscriber - lookup table. One subscriber can belong to many lists. message - subject, text body, HTML body message_queue - subscriber_id, message_id, list_id, sent, sent_time, unique_identifier
  76. 76. Thank You Contact - sudheer.s@sudheer.net http://techchorus.net http://projects.binaryvibes.co.in/projects/show/bare http://projects.binaryvibes.co.in/projects/show/bizsense http://binaryvibes.co.in
  77. 77. Licence Sending Bulk Email Using PHP by Sudheer Satyanarayana is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 2.5 India License . Based on a work at techchorus.net . Permissions beyond the scope of this license may be available at http://techchorus.net .

×