{"id":122,"date":"2012-12-05T14:46:10","date_gmt":"2012-12-05T22:46:10","guid":{"rendered":"http:\/\/nuclearprojects.com\/blog\/?p=122"},"modified":"2012-12-05T14:46:10","modified_gmt":"2012-12-05T22:46:10","slug":"setting-up-swift-mailer-to-send-through-google-apps-smtp-servers","status":"publish","type":"post","link":"https:\/\/nuclearprojects.com\/blog\/setting-up-swift-mailer-to-send-through-google-apps-smtp-servers\/","title":{"rendered":"Setting-up Swift Mailer to send through Google Apps SMTP servers"},"content":{"rendered":"<p>I send all my mail through my Google Apps account, using Google&#8217;s SMTP servers. I spent some time setting up Swift Mailer to do this, so I figured I&#8217;d show how I did it.<br \/>\n<a href=\"http:\/\/nuclearprojects.com\/blog\/wp-content\/uploads\/2012\/12\/swift.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/nuclearprojects.com\/blog\/wp-content\/uploads\/2012\/12\/swift.png\" alt=\"\" title=\"swift\" width=\"268\" height=\"197\" class=\"alignright size-full wp-image-131\" \/><\/a><br \/>\nSetting-up Swift Mailer to send mail through Google&#8217;s SMTP servers is quite easy once you put all the correct pieces together. Here are the steps:<\/p>\n<p>&nbsp;<br \/>\n<strong>Step 1:<\/strong> Download <a href=\"http:\/\/swiftmailer.org\/\" target=\"_blank\">SwiftMailer<\/a>, unzip it, then upload the &#8220;lib&#8221; folder to your server.<\/p>\n<p>&nbsp;<br \/>\n<strong>Step 2:<\/strong> Now you can start writing the PHP code. Begin by including the Swift library:<br \/>\n<code lang=\"php\">require_once '\/swiftmailer\/lib\/swift_required.php';<\/code><br \/>\nOf course, make sure to insert the correct path.<\/p>\n<p>&nbsp;<br \/>\n<strong>Step 3:<\/strong> Setup the transport method for sending. This is where you put in your Google account info:<br \/>\n<code lang=\"php\">\/\/ Create the Transport<br \/>\n$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 587,'tls');<br \/>\n$transport->setUsername('you@yourdomain.com');<br \/>\n$transport->setPassword('yourpassword');<\/code><br \/>\nYou&#8217;ll notice that the port is set to <strong>587<\/strong>, and also the encryption method is properly set to <strong>TLS<\/strong>.<\/p>\n<p>&nbsp;<br \/>\n<strong>Step 4:<\/strong> Create the message. This should be straight forward:<br \/>\n<code lang=\"php\">\/\/ Create the Mailer using your created Transport<br \/>\n$mailer = Swift_Mailer::newInstance($transport);<\/p>\n<p>\/\/ Create the message<br \/>\n$message = Swift_Message::newInstance();<\/p>\n<p>\/\/ Give the message a subject<br \/>\n$message->setSubject('Subject Goes Here');<\/p>\n<p>\/\/ Set the From address with an associative array<br \/>\n$message->setFrom(array('you@yourdomain.com' => 'Your Name'));<\/p>\n<p>\/\/ Set the To addresses with an associative array<br \/>\n$message->setTo(array('recipient@domain.com'));<\/p>\n<p>\/\/ Give it a body<br \/>\n$message->setBody('The main body of your message','text\/html');<\/code><\/p>\n<p>Two things to point out here. Firstly, the message content is being set to &#8216;<strong>text\/html<\/strong>&#8216; so that you may include HTML in your message. Also, while I&#8217;m showing you how to set the From address, Google will re-write the header to reflect your actual email address and account name. Google does this automatically.<\/p>\n<p>&nbsp;<br \/>\n<strong>Step 5:<\/strong> Time to send!<br \/>\n<code lang=\"php\">\/\/ Send the message!<br \/>\n$result = $mailer->send($message);<\/code><\/p>\n<p>&nbsp;<\/p>\n<p>That&#8217;s it! For further information, consult the documentation: <a href=\"http:\/\/swiftmailer.org\/docs\/introduction.html\" target=\"_blank\">http:\/\/swiftmailer.org\/docs\/introduction.html<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I send all my mail through my Google Apps account, using Google&#8217;s SMTP servers. I spent some time setting up Swift Mailer to do this, so I figured I&#8217;d show how I did it. Setting-up Swift Mailer to send mail through Google&#8217;s SMTP servers is quite easy once you put all the correct pieces together. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":131,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-122","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-web-programming"],"_links":{"self":[{"href":"https:\/\/nuclearprojects.com\/blog\/wp-json\/wp\/v2\/posts\/122","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nuclearprojects.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nuclearprojects.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nuclearprojects.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nuclearprojects.com\/blog\/wp-json\/wp\/v2\/comments?post=122"}],"version-history":[{"count":10,"href":"https:\/\/nuclearprojects.com\/blog\/wp-json\/wp\/v2\/posts\/122\/revisions"}],"predecessor-version":[{"id":133,"href":"https:\/\/nuclearprojects.com\/blog\/wp-json\/wp\/v2\/posts\/122\/revisions\/133"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nuclearprojects.com\/blog\/wp-json\/wp\/v2\/media\/131"}],"wp:attachment":[{"href":"https:\/\/nuclearprojects.com\/blog\/wp-json\/wp\/v2\/media?parent=122"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nuclearprojects.com\/blog\/wp-json\/wp\/v2\/categories?post=122"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nuclearprojects.com\/blog\/wp-json\/wp\/v2\/tags?post=122"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}