Sending E-mail that reaches the destination using PHP
-
Upload
manuel-lemos -
Category
Technology
-
view
5.787 -
download
2
description
Transcript of Sending E-mail that reaches the destination using PHP
![Page 1: Sending E-mail that reaches the destination using PHP](https://reader031.fdocuments.in/reader031/viewer/2022020207/555493c4b4c905186d8b494b/html5/thumbnails/1.jpg)
Manuel [email protected]
http://www.ManuelLemos.net/
http://www.phpclasses.org/mimemessage
Sending E-mail that reaches the destination
![Page 2: Sending E-mail that reaches the destination using PHP](https://reader031.fdocuments.in/reader031/viewer/2022020207/555493c4b4c905186d8b494b/html5/thumbnails/2.jpg)
The problems
● Sending e-mail is fundamental
● PHP native support for sending e-mail is deficient
● The Internet e-mail standards are complicated
● Incorrectly composed messages are discarded
● Correctly composed messages are confused with SPAM
![Page 3: Sending E-mail that reaches the destination using PHP](https://reader031.fdocuments.in/reader031/viewer/2022020207/555493c4b4c905186d8b494b/html5/thumbnails/3.jpg)
Solution
Ready to use PHP components for sending e mail‑
● PHPMailer
● PEAR Mail
● SWIFT mailer
● MIME message
● Etc.
![Page 4: Sending E-mail that reaches the destination using PHP](https://reader031.fdocuments.in/reader031/viewer/2022020207/555493c4b4c905186d8b494b/html5/thumbnails/4.jpg)
What is MIME?● Multipurpose Internet Mail Extensions
● Standards for sending E-mail messages
● Defined by many RFC documents: Request For Comments
● New RFC document versions are compatible with past versions
![Page 5: Sending E-mail that reaches the destination using PHP](https://reader031.fdocuments.in/reader031/viewer/2022020207/555493c4b4c905186d8b494b/html5/thumbnails/5.jpg)
MIME message class
PHP class to compose and send e-mail
● Sends messages with text and HTML using any character set
● Embeds related files: images, CSS, etc..
● May attach multiple files
● Optimized for sending newsletters to many recipients
![Page 6: Sending E-mail that reaches the destination using PHP](https://reader031.fdocuments.in/reader031/viewer/2022020207/555493c4b4c905186d8b494b/html5/thumbnails/6.jpg)
Text message
require('email_message.php');
$m = new email_message_class;
$m->SetEncodedHeader('Subject', 'This is the subject');
$m->SetEmailEncodedHeader('From', '[email protected]', 'John');
$m->SetEmailEncodedHeader('To', '[email protected]', 'Joe');
$text = “Hello Joe,\n\nThis is the message.”;
$m->AddQuotedPrintableTextPart($text);
$m->Send();
![Page 7: Sending E-mail that reaches the destination using PHP](https://reader031.fdocuments.in/reader031/viewer/2022020207/555493c4b4c905186d8b494b/html5/thumbnails/7.jpg)
HTML message
$html = “<html><head><title>Messagm</title></head><body>Hello Joe,<br />\n<br />\nThis is the message.</body></html>”;
$text = strip_tags($html);
$m->CreateQuotedPrintableHTMLPart($html, '', $h);
$m->CreateQuotedPrintableTextPart($text, '', $t);
$alternatives = array($t, $h);
$m->AddAlternativeMultipart($alternatives);
$m->Send()
![Page 8: Sending E-mail that reaches the destination using PHP](https://reader031.fdocuments.in/reader031/viewer/2022020207/555493c4b4c905186d8b494b/html5/thumbnails/8.jpg)
HTML messages with embedded images
$image=array( 'FileName'=>'image.gif', 'Content-Type' => 'automatic/name', 'Disposition'=>'inline');
$e->CreateFilePart($image, $i);
$url = 'cid:'.
$m->GetPartContentID($i);
$html = "<html> <head><title>Message</title> </head><body><img src=\”$url\” /> Hello Joe,<br />\n<br />\nThis is the message.</body></html>";
$m->CreateQuotedPrintableHTMLPart ($html, '', $h);
$text = strip_tags($html);
$m->CreateQuotedPrintableTextPart ($text, '', $t);
$alternatives = array($t, $h);
$m->AddAlternativeMultipart ($alternatives);
$related = array( $alternatives, $i,);$m->AddRelatedMultipart ($related);
$m->Send()
![Page 9: Sending E-mail that reaches the destination using PHP](https://reader031.fdocuments.in/reader031/viewer/2022020207/555493c4b4c905186d8b494b/html5/thumbnails/9.jpg)
Message with attached files$attachment=array( 'Data'=>'This is an attachment called attachment.txt', 'Name'=>'attachment.txt', 'Content-Type'=>'automatic/name', 'Disposition'=>'attachment',);$m->AddFilePart($attachment);$attachment=array( 'FileName'=>'attachment.zip', 'Content-Type'=>'automatic/name', 'Disposition'=>'attachment',);$m->AddFilePart($attachment);$m->Send();
![Page 10: Sending E-mail that reaches the destination using PHP](https://reader031.fdocuments.in/reader031/viewer/2022020207/555493c4b4c905186d8b494b/html5/thumbnails/10.jpg)
Sending via SMTP
Supports several types of authentication using the SASL classes: LOGIN, MD5, NTLM, etc.
require('sasl.php'); require('smtp.php');
require('smtp_message.php');
$m = new smtp_message_class;
$m->smtp_host = 'smtp.gmail.com';
$m->smtp_port = 465;
$m->smtp_ssl = 1;
$m->smtp_user = 'some_user';
$m->smtp_password = 'some_password';
$m->direct_delivery = 0;
![Page 11: Sending E-mail that reaches the destination using PHP](https://reader031.fdocuments.in/reader031/viewer/2022020207/555493c4b4c905186d8b494b/html5/thumbnails/11.jpg)
Sending via sendmail, qmail and Microsoft Exchange
Faster delivery to the MTA queue
require('sendmail_message.php');
$m = new sendmail_message_class;
$m->delivery_mode = SENDMAIL_DELIVERY_DEFERRED;
require('qmail_message.php');
$m = new qmail_message_class;
require('pickup_message.php');
$m = new pickup_message_class;
![Page 12: Sending E-mail that reaches the destination using PHP](https://reader031.fdocuments.in/reader031/viewer/2022020207/555493c4b4c905186d8b494b/html5/thumbnails/12.jpg)
Mail() function alternatives
When the mail() function does not work well
● smtp_mail()
● sendmail_mail()
● qmail_mail()
● urgent_mail()
![Page 13: Sending E-mail that reaches the destination using PHP](https://reader031.fdocuments.in/reader031/viewer/2022020207/555493c4b4c905186d8b494b/html5/thumbnails/13.jpg)
The path of the messages
PHP script MTA
Local queue
Destination SMTP
Destination queueLocal SMTP
mail()
SMTP Pickup
Direct
![Page 14: Sending E-mail that reaches the destination using PHP](https://reader031.fdocuments.in/reader031/viewer/2022020207/555493c4b4c905186d8b494b/html5/thumbnails/14.jpg)
The best delivery methods
1.Drop a message file in the local queue
2.Pass to MTA with the mail function (sendmail)
3.Pass to the local SMTP server
4.Direct delivery to the destination SMTP server
![Page 15: Sending E-mail that reaches the destination using PHP](https://reader031.fdocuments.in/reader031/viewer/2022020207/555493c4b4c905186d8b494b/html5/thumbnails/15.jpg)
Optimizing the delivery of non personalized newsletters‑
$list = array(
'[email protected]'=>'Peter', '[email protected]'=>'Paulo', '[email protected]'=>'Mary'
);
$m->SetBulkMail(1);
$m->cache_body = 1;
foreach($list as $email => $name) {
$m->SetEncodedEmailHeader('To', $email, $name);
$m->Send();
}
$m->SetBulkMail(0);
![Page 16: Sending E-mail that reaches the destination using PHP](https://reader031.fdocuments.in/reader031/viewer/2022020207/555493c4b4c905186d8b494b/html5/thumbnails/16.jpg)
Optimizing the delivery of personalized newsletters
$m->SetBulkMail(1);
$m->cache_body = 0;
$template = 'Hello {name}, ...';
$m->CreateQuotedPrintableTextPart($template, '', $part_template);
$m->AddPart($part_template);
foreach($list as $email => $name) {
$m->SetEncodedEmailHeader('To', $email, $name);
$text = str_replace('{name}', $name, $template);
$m->CreateQuotedPrintableTextPart($text, '', $personalized);
$m->ReplacePart($part_template, $personalized);
$m->Send();
}
$m->SetBulkMail(0);
![Page 17: Sending E-mail that reaches the destination using PHP](https://reader031.fdocuments.in/reader031/viewer/2022020207/555493c4b4c905186d8b494b/html5/thumbnails/17.jpg)
Handling returned messages
1.Set the Return-Path message header to an address associated with a POP3 mailbox
2.Periodically poll that mailbox using a POP3 client class
3.Process the returned messages with the MIME parser class
4.Unsubscribe from the newsletters the e mail ‑addresses that are returning the messages
![Page 18: Sending E-mail that reaches the destination using PHP](https://reader031.fdocuments.in/reader031/viewer/2022020207/555493c4b4c905186d8b494b/html5/thumbnails/18.jpg)
Know when a message was received
Techniques that do not always work
1.Set the Disposition-Notification-To message header to an address to which reception notices will be sent
2.Insert a beacon image in an HTML message using the URL of a script that accounts messages that are viewed by recipients
<img src=”http://www.meusite.com.br/[email protected]”>
![Page 19: Sending E-mail that reaches the destination using PHP](https://reader031.fdocuments.in/reader031/viewer/2022020207/555493c4b4c905186d8b494b/html5/thumbnails/19.jpg)
Avoid confusion with SPAM
What types of messages you should not send?● Sent from an IP address without reverse DNS
record (PTR) or that is listed in blacklists● The recipient e-mail addresses are in Bcc● Only with an HTML part or has invalid HTML● With beacon image URL that has parameters● Link URLs do not match anchor text URL● Do not pass SpamAssassin checks
![Page 21: Sending E-mail that reaches the destination using PHP](https://reader031.fdocuments.in/reader031/viewer/2022020207/555493c4b4c905186d8b494b/html5/thumbnails/21.jpg)
References● MIME Message classhttp://www.phpclasses.org/mimemessage
● SMTP client classhttp://www.phpclasses.org/smtpclass
● SASL authentication classhttp://www.phpclasses.org/sasl
● POP3 client classhttp://www.phpclasses.org/pop3class
● MIME parser classhttp://www.phpclasses.org/mimeparser
● Verify IP addresses in multiple blacklistshttp://openrbl.org/
● SpamAssassinhttp://spamassassin.apache.org/