Slide 1

Slide 1 text

Swift Mailer The missing manual and more. PHP Conference Kansai 2014 2014-06-28 @suzuki

Slide 2

Slide 2 text

Agenda • Intro • Basic • Message • Mailer • Transport • Plugin • Thirdparty

Slide 3

Slide 3 text

Intro / About me • Love to get account of 'suzuki'. • https://twitter.com/suzuki • https://github.com/suzuki

Slide 4

Slide 4 text

Intro / About me I receive some mentions
 about SUZUKI motorcar, every day.

Slide 5

Slide 5 text

Intro / About me • Career • 2000 - 2012 Email Service Provider • 2013 - Crocos Inc ! ! • CakePHP2࣮ફೖ໳ (2012-09) • ୈ9ষɿCakeEmailΫϥεΛ࢖ͬͨϝʔϧૹ৴ • http://gihyo.jp/book/2012/978-4-7741-5324-7

Slide 6

Slide 6 text

Intro This slide is not related to the series of O'Reilly.

Slide 7

Slide 7 text

Basic • Overview • Install • Structure

Slide 8

Slide 8 text

Basic / Overview Swift Mailer Free Feature
 rich PHP Mailer http://swiftmailer.org/

Slide 9

Slide 9 text

Basic / Install • Installed by Composer ! ! • composer.json $ php composer.phar require swiftmailer/swiftmailer @stable {! "require": {! "swiftmailer/swiftmailer": "@stable"! }! }

Slide 10

Slide 10 text

Basic / Structure Message Mailer

Slide 11

Slide 11 text

Basic / Structure ! Mailer Transport Plugin

Slide 12

Slide 12 text

Basic / Structure / Sample // create mailer! $transport = Swift_SmtpTransport::newInstance(HOST, PORT);! $mailer = Swift_Mailer::newInstance($transport);! ! // create message! $message = Swift_Message::newInstance();! $message! ->setFrom(MAIL_FROM)! ->setTo(MAIL_TO)! ->setSubject('Hi !')! ->setBody('Hello, Swift Mailer.')! ;! ! // send message! $result = $mailer->send($message); Transport Message Mailer

Slide 13

Slide 13 text

Message • Header, Body • AddPart • Attach • Embed • S/MIME • DKIM Message

Slide 14

Slide 14 text

Message / Header, Body ! Message Header Body

Slide 15

Slide 15 text

Message / Header, Body Header / Body code sample From $message->setFrom('[email protected]'); Subject $message->setSubject('This is sample'); To, Cc, Bcc $message->setTo('[email protected]')! ->setCc('[email protected]')! ->setBcc('[email protected]');! Body $message->setBody('Hello World');

Slide 16

Slide 16 text

Message / Headers list Header gettter / setter Message-ID getId() / setId()! Return-Path getReturnPath() / serReturnPath()! From getFrom() / setFrom()! Sender getSender() / setSender()! To getTo() / setTo()! Cc getCc() / setCc()! Bcc getBcc() / setBcc()! Reply-To getReplyTo() / setReplyTo()! Subject getSubject() / setSubject()! Date getDate() / setDate()! Content-Type getContentType() / setContentType()! Content-Transfer-Encoding getEncoder() / setEncoder()!

Slide 17

Slide 17 text

Message Message / AddPart ! Body TextPart HtmlPart addPart() addPart()

Slide 18

Slide 18 text

Message / AddPart / Sample $message = Swift_Message::newInstance();! $message! ->setFrom(MAIL_FROM)! ->setTo(MAIL_TO)! ->setSubject('Multipart mail sample')! ;! ! // POINT of this sample! $message->addPart(! 'This is TEXT part.',! 'text/plain'! );! $message->addPart(! 'This is HTML part.',! 'text/html'! );

Slide 19

Slide 19 text

Message / Attach Message ! Body File attach()

Slide 20

Slide 20 text

Message / Attach / Sample $message = Swift_Message::newInstance();! ! [snip]! ! // POINT of this sample! $attachment = Swift_Attachment::fromPath(! './images/panda.jpg',! 'image/jpeg'! );! $message->attach($attachment);

Slide 21

Slide 21 text

Message / Embed Message ! Body Content-ID: abc123 Embeded File embed() “cid” means “Content-ID” via RFC 2557 MHTML

Slide 22

Slide 22 text

Message / Embed / Sample $message = Swift_Message::newInstance();! ! [snip]! ! // POINT of this sample! $embedImage = Swift_Image::fromPath(! './images/panda.jpg'! );! $message->setBody(! '',! 'text/html'! );

Slide 23

Slide 23 text

Message / S/MIME Message ! Body S/MIME Signature attachSigner() SmimeSigner S/MIME signature is like an attached file. It named 'smime.p7s'.

Slide 24

Slide 24 text

Message / S/MIME / Sample $message = Swift_Message::newInstance();! ! [snip]! ! // POINT of this sample! $smimeSigner = Swift_Signers_SMimeSigner::newInstance();! $smimeSigner->setSignCertificate(! SMIME_CERT_FILE,! [SMIME_SECRET_FILE, SMIME_SECRET_PASSPHRASE]! );! $message->attachSigner($smimeSigner);!

Slide 25

Slide 25 text

Message / DKIM Message Body attachSigner() DKIMSigner Header DKIM Signature missing ! DKIM.org http://dkim.org/ http://suzuki.tdiary.net/20140203.html

Slide 26

Slide 26 text

Message / DKIM / Sample $message = Swift_Message::newInstance();! ! [snip]! ! // POINT of this sample! $privateKey = file_get_contents(DKIM_PRIVATE_KEY);! $dkimSigner = Swift_Signers_DKIMSigner::newInstance(! $privateKey,! DKIM_DOMAIN,! DKIM_SELECTOR! );! $message->attachSigner($dkimSigner); missing !

Slide 27

Slide 27 text

Mailer • Transport • Plugin ! Mailer Transport Plugin

Slide 28

Slide 28 text

Transport • SMTP • Sendmail, Mail • Failover • Loadbalanced • Null • FileSpool, MemorySpool Transport

Slide 29

Slide 29 text

Transport / SMTP ! Mailer SmtpTransport SMTP Server

Slide 30

Slide 30 text

Transport / SMTP / Samples // POINT of this sample! $transport = Swift_SmtpTransport::newInstance(! SMTP_HOST,! SMTP_PORT! );! $mailer = Swift_Mailer::newInstance($transport);

Slide 31

Slide 31 text

Transport / SMTP / Samples // POINT of this sample! $transport = Swift_SmtpTransport::newInstance(! SMTP_AUTH_HOST,! SMTP_AUTH_PORT! );! ! $transport! ->setUsername(SMTP_AUTH_USER)! ->setPassword(SMTP_AUTH_PASS)! ->setEncryption('ssl')! ;! ! $mailer = Swift_Mailer::newInstance($transport);!

Slide 32

Slide 32 text

Transport / Sendmail, Mail ! Mailer SendmailTransport,
 MailTransport SMTP Server /usr/sbin/sendmail,
 mail()

Slide 33

Slide 33 text

Transport / Sendmail / Sample // POINT of this sample! $transport = Swift_SendmailTransport::newInstance(! '/usr/sbin/sendmail -bs'! );! // -bs means 'Stand-alone SMTP server mode'! ! $mailer = Swift_Mailer::newInstance($transport);

Slide 34

Slide 34 text

Transport / Mail / Sample // POINT of this sample! $transport = Swift_MailTransport::newInstance();! $mailer = Swift_Mailer::newInstance($transport);! !

Slide 35

Slide 35 text

Mailer FailoverTransport Transport / Failover missing ! SmtpTransport 1 SMTP Server 1 SmtpTransport 2 SmtpTransport n SMTP Server 2 SMTP Server n Usually use SMTP Server1. When Server 1 goes down, use Server 2.

Slide 36

Slide 36 text

Transport / Failover / Sample // POINT of this sample! $transport1 = Swift_SmtpTransport::newInstance(! SMTP_HOST,! SMTP_PORT! );! $transport2 = Swift_SmtpTransport::newInstance(! SMTP_HOST2,! SMTP_PORT2! );! ! $transport = Swift_FailoverTransport::newInstance([! $transport1,! $transport2,! ]);! $mailer = Swift_Mailer::newInstance($transport); missing !

Slide 37

Slide 37 text

Mailer FailoverTransport Transport / Loadbalanced missing ! SmtpTransport 1 SMTP Server 1 SmtpTransport 2 SmtpTransport n SMTP Server 2 SMTP Server n Used in rotation from SMTP Server 1 to STMP Server n

Slide 38

Slide 38 text

Transport / Loadbalanced / Sample // POINT of this sample! $transport1 = Swift_SmtpTransport::newInstance(! SMTP_HOST,! SMTP_PORT! );! $transport2 = Swift_SmtpTransport::newInstance(! SMTP_HOST2,! SMTP_PORT2! );! ! $transport = Swift_LoadBalancedTransport::newInstance([! $transport1,! $transport2,! ]);! $mailer = Swift_Mailer::newInstance($transport); missing !

Slide 39

Slide 39 text

Transport / Null missing ! ! Mailer NullTransport SMTP Server Not send. But counting the number of recipients. Not send

Slide 40

Slide 40 text

Transport / Null / Sample // POINT of this sample! $transport = Swift_NullTransport::newInstance();! $mailer = Swift_Mailer::newInstance($transport); missing !

Slide 41

Slide 41 text

Transport / FileSpool missing ! Mailer SMTP Server SpoolTransport FileSpool Storage File 1 File 2 File 3 File n Not send Create one file by one mail. ! Each files are Swift_Message serialized object.

Slide 42

Slide 42 text

Transport / FileSpool / Sample // POINT of this sample! $path = FILE_SPOOL_PATH;! $spool = new Swift_FileSpool($path);! $transport = Swift_SpoolTransport::newInstance($spool);! ! $mailer = Swift_Mailer::newInstance($transport);! missing !

Slide 43

Slide 43 text

Transport / MemorySpool missing ! Mailer SMTP Server SpoolTransport MemorySpool Memory $array[0] $array[1] $array[3] $array[n] Not send Add one element to array by one mail.

Slide 44

Slide 44 text

Transport / MemorySpool / Sample // POINT of this sample! $spool = new Swift_MemorySpool();! $transport = Swift_SpoolTransport::newInstance($spool);! ! $mailer = Swift_Mailer::newInstance($transport);! missing !

Slide 45

Slide 45 text

Plugin • AntiFlood • Throttler • Logger • Decorator • Impersonate • Redirecting • Reporter Plugin

Slide 46

Slide 46 text

Plugin / AntiFlood ! Mailer SmtpTransport SMTP Server AntiFloodPlugin Re-connect to SMTP Server in every 100 emails (100 is default). You may want to sleep a specified number of seconds. re-connect

Slide 47

Slide 47 text

Plugin / AntiFlood / Sample // POINT of this sample! // re-connect by 1 send, and 10 sec sleep (option)! $mailer->registerPlugin(! new Swift_Plugins_AntiFloodPlugin(1, 10)! );

Slide 48

Slide 48 text

Plugin / Throttler ! Mailer SmtpTransport SMTP Server ThrottlerPlugin This can limit the number of transmissions per minute, second or bytes. ex. 100 mails per minute

Slide 49

Slide 49 text

Plugin / Throttler / Sample // POINT of this sample! // 1 send per minute! $mailer->registerPlugin(! new Swift_Plugins_ThrottlerPlugin(! 1,! ɹɹ Swift_Plugins_ThrottlerPlugin::MESSAGES_PER_MINUTE! )! );

Slide 50

Slide 50 text

Plugin / Logger Mailer SmtpTransport SMTP Server LoggerPlugin ArrayLogger Display logs used dump() method. Logging to array. EchoLogger or Echo each process log.

Slide 51

Slide 51 text

Plugin / Logger / Sample // POINT of this sample! // store log to array! $logger = new Swift_Plugins_Loggers_ArrayLogger();! $mailer->registerPlugin(! new Swift_Plugins_LoggerPlugin($logger)! );! ! [snip]
 
 $result = $mailer->send($message);! ! // POINT of this sample! echo $logger->dump();

Slide 52

Slide 52 text

Plugin / Decorator ! Mailer SmtpTransport SMTP Server DecoratorPlugin Replace keywords for each recipients.

Slide 53

Slide 53 text

Plugin / Decorator $replacements = [! '[email protected]' => [! '{username}' => 'Norio Suzuki',! ],! '[email protected]' => [! '{username}' => 'Foo Bar',! ],! ]; To: [email protected] ! Hi, {username} To: [email protected] ! Hi, {username} To: [email protected] ! Hi, Norio Suzuki To: [email protected] ! Hi, Foo Bar replace keywords for each recipients

Slide 54

Slide 54 text

Plugin / Decorator / Sample // POINT of this sample! $replacements = [! MAIL_TO => [! '{username}' => 'USER NAME1',! ],! MAIL_TO2 => [! '{username}' => 'USER NAME2',! ],! ];! $decorator = new Swift_Plugins_DecoratorPlugin($replacements);! $mailer->registerPlugin($decorator);! ! $message = Swift_Message::newInstance();! $message! ->setFrom(MAIL_FROM)! ->setSubject('Info for {username}, Decorator Plugin sample')! ->setBody('Hi, {username}. This is a mail.')! ;

Slide 55

Slide 55 text

Plugin / Impersonate missing ! ! Mailer SmtpTransport SMTP Server ImpersonatePlugin Replace Return-Path address.

Slide 56

Slide 56 text

Plugin / Impersonate Return-Path: [email protected] From: [email protected] To: [email protected] ! This is a mail. Replace Return-Path Return-Path: [email protected] From: [email protected] To: [email protected] ! This is a mail.

Slide 57

Slide 57 text

Plugin / Impersonate example.com SMTP Server example.jp SMTP Server Bounced mails return to example.jp Normally

Slide 58

Slide 58 text

Plugin / Impersonate example.com SMTP Server example.jp SMTP Server errors.example.jp SMTP Server Bounced mails return to errors.example.jp Using ImpersonatePlugin

Slide 59

Slide 59 text

Plugin / Impersonate / Sample // POINT of this sample! // Replace 'Return-path' to $sender! $sender = MAIL_SENDER;! $mailer->registerPlugin(! new Swift_Plugins_ImpersonatePlugin($sender)! ); missing !

Slide 60

Slide 60 text

Plugin / Redirecting missing ! ! Mailer SmtpTransport SMTP Server RedirectingPlugin All recipients are redirected to specific address.

Slide 61

Slide 61 text

Plugin / Redirecting Return-Path: [email protected] From: [email protected] To: [email protected] ! This is a mail. Return-Path: [email protected] From: [email protected] To: [email protected] ! This is a mail. Replace recipients

Slide 62

Slide 62 text

Plugin / Redirecting Return-Path: [email protected] From: [email protected] To: [email protected] ! This is a mail. Return-Path: [email protected] From: [email protected] To: [email protected] ! This is a mail. Not replace if exist in whitelist. $whitelist = [! '/^white@/',! ];

Slide 63

Slide 63 text

Plugin / Redirecting / Sample // POINT of this sample! // All recipients are replace to $recipient.! // Except a pattern in whitelist.! $recipient = MAIL_REDIRECT_TO;! $whiteList = [WHITE_LIST_PCRE_PATTERN];! ! $mailer->registerPlugin(! new Swift_Plugins_RedirectingPlugin(! $recipient,! $whiteList! )! ); missing !

Slide 64

Slide 64 text

Plugin / Reporter missing ! Mailer SmtpTransport SMTP Server ReporterPlugin HitReporter Find a failed recipients using FailedRecipients() method. HtmlReporter or Reporting success or failed recipients by HTML format.

Slide 65

Slide 65 text

Plugin / Reporter / Sample // POINT of this sample! $reporter = new Swift_Plugins_Reporters_HitReporter();! $mailer->registerPlugin(! new Swift_Plugins_ReporterPlugin($reporter)! );! ! $recipients = [MAIL_TO, MAIL_FAILED_ADDRESS];! foreach ($recipients as $recipient) {! $message->setTo($recipient);! $result = $mailer->send($message);! }! ! // POINT of this sample! $failedRecipients = $reporter->getFailedRecipients();! var_dump($failedRecipients); missing !

Slide 66

Slide 66 text

Thirdparty • Transport • Amazon SES • Plugin • Filter • CSS Inliner • Google Campaign

Slide 67

Slide 67 text

Transport / Amazon SES ! Mailer AWSTransport Amazon SES Sending email by Amazon SES http://aws.amazon.com/ses/

Slide 68

Slide 68 text

Transport / Amazon SES / install {! "require": {! "swiftmailer/swiftmailer": "@stable",! "jmhobbs/swiftmailer-transport-aws-ses": "dev-master"! }! } composer.json

Slide 69

Slide 69 text

Transport / Amazon SES / Sample // POINT of this sample! $transport = Swift_AWSTransport::newInstance(! AWS_ACCESS_KEY,! AWS_SECRET_KEY! );! $transport->setEndpoint(AWS_ENDPOINT);! $mailer = Swift_Mailer::newInstance($transport);

Slide 70

Slide 70 text

Plugin / Filter ! Mailer SmtpTransport SMTP Server FilterPlugin Filtering recipients using whitelist and blacklist.

Slide 71

Slide 71 text

Plugin / Filter / install {! "require": {! "swiftmailer/swiftmailer": "@stable",! "openbuildings/swiftmailer-filter": "dev-master"! }! } composer.json

Slide 72

Slide 72 text

Plugin / Filter / Sample use Openbuildings\Swiftmailer\FilterPlugin;! ! [snip]! ! // POINT of this sample! // set email address or domain! $whiteList = [MAIL_TO];! $blackList = [MAIL_TO2];! $mailer->registerPlugin(! new FilterPlugin($whiteList, $blackList)! );

Slide 73

Slide 73 text

Plugin / CSS Inliner ! Mailer SmtpTransport SMTP Server CssInlinerPluginPlugin Convert to "inline CSS" from CSS written in tag.

Slide 74

Slide 74 text

Plugin / CSS Inliner .main { background-color: #f9ba34; text-align: center; height: 200px; line-height: 200px; }

Hi, This is CSS Inliner sample

.main { background-color: #f9ba34; text-align: center; height: 200px; line-height: 200px; }

Hi, This is CSS Inliner sample

Slide 75

Slide 75 text

Plugin / CSS Inliner Without CSS Inliner plugin (Gmail) With CSS Inliner plugin (Gmail)

Slide 76

Slide 76 text

Plugin / CSS Inliner / install {! "require": {! "swiftmailer/swiftmailer": "@stable",! "openbuildings/swiftmailer-css-inliner": "dev-master"! }! } composer.json

Slide 77

Slide 77 text

Plugin / CSS Inliner / Sample use Openbuildings\Swiftmailer\CssInlinerPlugin;! ! [snip]! ! // POINT of this sample! $mailer->registerPlugin(! new CssInlinerPlugin()! );!

Slide 78

Slide 78 text

Plugin / Google Campaign ! Mailer SmtpTransport SMTP Server GoogleCampaignPluginPlugin Add parameters of Google Analytics to each URLs.

Slide 79

Slide 79 text

Plugin / Google Campaign To: [email protected] ! example.com Add Google Analytics parameters To: [email protected] ! example.com

Slide 80

Slide 80 text

Plugin / Google Campaign / install {! "require": {! "swiftmailer/swiftmailer": "@stable",! "openbuildings/swiftmailer-google-campaign": "dev-master"! }! } composer.json

Slide 81

Slide 81 text

Plugin / Google Campaign / Sample use Openbuildings\Swiftmailer\GoogleCampaignPlugin;! ! // POINT of this sample! $mailer->registerPlugin(! new GoogleCampaignPlugin([! 'utm_source' => 'source',! 'utm_campaign' => 'email',! 'utm_medium' => 'email',! ])! );

Slide 82

Slide 82 text

Appendix • All sample codes are here: • https://github.com/suzuki/swiftmailer-samples

Slide 83

Slide 83 text

Appendix name URL Swift Mailer http://swiftmailer.org/ Amazon SES Transport https://github.com/jmhobbs/Swiftmailer-Transport--AWS-SES Filter Plugin https://github.com/OpenBuildings/swiftmailer-filter CSS Inliner Plugin https://github.com/OpenBuildings/swiftmailer-css-inliner Google Campaign Plugin https://github.com/OpenBuildings/swiftmailer-google-campaign Composer https://getcomposer.org/ Packagist https://packagist.org/

Slide 84

Slide 84 text

Thank you.