Php swiftmailer gmail smtp

Send Email Using Gmail SMTP Server and Swift Mailer Library

Do you want to send your emails using Gmail SMTP server? When you use the SMTP server for your emails, there is a high chance of your emails going to the user’s inbox and not to the spam. SMTP server prevents your emails from being marked as spam. In this article, we study how to send email using the Gmail SMTP server and the Swift Mailer library.

As a site owner, you always want your email to go straight into the user’s inbox and not into the spam or junk. It increases the probability of the user reading your email and taking the action you wish for.

Why Need to Use SMTP Server?

For a website, it is normal to have a form that sends an email to users or administrators. It can be your newsletter, contact form, or registration process where you need to send an email as an acknowledgment.

PHP provides a mail() function for sending emails. However, if your server does not configure mail settings correctly then this method does not work. Another possibility is when you send emails using mail() method, it can end up in spam.

Читайте также:  Offline installers for java

To overcome these 2 situations, you should use the SMTP server for sending your emails.

Using the Swift Mailer library, the user can use any SMTP server like Gmail, Sendgrid, Mandrill, or your own hosting-provided SMTP server for sending emails. In this tutorial, I choose a Gmail SMTP server.

Having said that, let’s take a look at how to use the Swift Mailer library for sending emails.

Swift Mailer Installation

In order to use the Swift Mailer library, you should have PHP version 7.2 or higher on your server. If you are using the older version then upgrade it to the latest PHP version. Most hosting providers like Bluehost upgrade the PHP version on request. You don’t need to invest your time to update the version.

Going ahead, I recommend using Composer to install the Swift Mailer library. Open the terminal in your project root directory and run the command:

composer require swiftmailer/swiftmailer

As we are going to use Gmail SMTP, you need to change some settings on your Google account. Login to your Google account and click on Account. Once you are on the Google Account page, click on Security. Scroll down to the bottom and you will find ‘Less secure app access’ settings. Set it to ON.

less-secure-apps

Send Email Using Gmail SMTP Server and Swift Mailer Library

At this stage, you are ready with the Swift Mailer library and you also changed the Gmail account settings. Now, you are good to go ahead.

Let’s say you have a file sendmail.php where you need to write a code that sends emails. Write the below code in your PHP file.

setUsername('YOUR_GMAIL_USERNAME') ->setPassword('YOUR_GMAIL_PASSWORD') ; // Create the Mailer using your created Transport $mailer = new Swift_Mailer($transport); // Create a message $body = 'Hello, 

Email sent through Swift Mailer.

'; $message = (new Swift_Message('Email Through Swift Mailer')) ->setFrom(['FROM_EMAIL_ADDRESS' => 'FROM_NAME']) ->setTo(['RECEPIENT_1_EMAIL_ADDRESS']) ->setCc(['RECEPIENT_2_EMAIL_ADDRESS']) ->setBcc(['RECEPIENT_3_EMAIL_ADDRESS']) ->setBody($body) ->setContentType('text/html') ; // Send the message $mailer->send($message); echo 'Email has been sent.'; > catch(Exception $e) < echo $e->getMessage(); >

In the above code, I have passed the below values for Gmail SMTP server settings.

  • Google SMTP Server Address: smtp.googlemail.com
  • Gmail SMTP Port: 465
  • Encryption: ssl

Apart from these values, you need to change other placeholders like YOUR_GMAIL_USERNAME, YOUR_GMAIL_PASSWORD, etc.

After replacing all values, run this file on a browser. You should get the email in the inbox, not in the spam.

Send Single or Multiple Attachments in an Email

Sometimes you may need to send attachments in an email. Using Swift Mailer you can send single or multiple attachments as follows:

$message->attach(Swift_Attachment::fromPath(__DIR__. '/sample.png')); //absolute path for your attachment $message->attach(Swift_Attachment::fromPath(__DIR__. '/sample-ebook.pdf'));

All you need to do is use attach method and pass the absolute path of the file you need to send as an attachment. Here I assume you need to send ‘sample.png’ and ‘sample-ebook.pdf’ as attachments.

So our final code is as follows.

setUsername('YOUR_GMAIL_USERNAME') ->setPassword('YOUR_GMAIL_PASSWORD') ; // Create the Mailer using your created Transport $mailer = new Swift_Mailer($transport); // Create a message $body = 'Hello, 

Email sent through Swift Mailer.

'; $message = (new Swift_Message('Email Through Swift Mailer')) ->setFrom(['FROM_EMAIL_ADDRESS' => 'FROM_NAME']) ->setTo(['RECEPIENT_1_EMAIL_ADDRESS']) ->setCc(['RECEPIENT_2_EMAIL_ADDRESS']) ->setBcc(['RECEPIENT_3_EMAIL_ADDRESS']) ->setBody($body) ->setContentType('text/html') ->attach(Swift_Attachment::fromPath(__DIR__. '/sample.png')) ->attach(Swift_Attachment::fromPath(__DIR__. '/sample-ebook.pdf')) ; // Send the message $mailer->send($message); echo 'Email has been sent.'; > catch(Exception $e) < echo $e->getMessage(); >

It’s all about sending emails through the Swift Mailer library. You can also use Symfony Swift Mailer for sending messages via a few SMTP servers like Mailgun, Mandrill, etc. I would like to hear your thoughts and suggestions in the comment section below.

If you liked this article, then please subscribe to our YouTube Channel for video tutorials.

Источник

php-swiftmailerHow to set up Swiftmailer with Gmail SMTP?

Swiftmailer is a popular library for sending emails from PHP applications. Setting up Swiftmailer with Gmail SMTP is easy and straightforward.

// Create the Transport $transport = (new Swift_SmtpTransport('smtp.gmail.com', 465, 'ssl')) ->setUsername('[email protected]') ->setPassword('yourpassword'); // Create the Mailer using your created Transport $mailer = new Swift_Mailer($transport); // Create a message $message = (new Swift_Message('Wonderful Subject')) ->setFrom(['[email protected]' => 'John Doe']) ->setTo(['[email protected]', '[email protected]' => 'A name']) ->setBody('Here is the message itself'); // Send the message $result = $mailer->send($message); 

The output of the example code is $result which is an integer representing the number of successful recipients.

Code explanation

  1. $transport : This is the transport object which is used to connect to the Gmail SMTP server. It requires the hostname, port and encryption type as parameters.
  2. $mailer : This is the mailer object which is used to send the message. It requires the transport object as a parameter.
  3. $message : This is the message object which is used to create the email. It requires the from, to, subject and body as parameters.
  4. $result : This is the result of the send operation which is an integer representing the number of successful recipients.

More of Php Swiftmailer

Источник

Swiftmailer: Send mails from php easily and effortlessly

Carlos Delgado

Learn to use SwiftMailer instead of PHP mail function to send emails fast and reliably.

Swiftmailer: Send mails from php easily and effortlessly

Do not recreate the wheel to send mails ! Use Swift Mailer library.

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 easy to implement.

Learn in this article how to install and use this library.

Installation

The preferred way to install Swiftmailer is via Composer:

$ composer require swiftmailer/swiftmailer 

or add to your composer.json file in the require block

"swiftmailer/swiftmailer": "v5.4.0", 
"symfony/swiftmailer-bundle": "~2.3", 

If you don’t use Composer download the zip file in https://github.com/swiftmailer/swiftmailer

Sending mails

The algorithm to send a message is very easy to understan. You create a Transport, use it to create the Mailer, then you use the Mailer to send the message.

The mails can contain plain/text or html/text according to the configuration.

If the SwiftMailer class doesn’t exist, don’t forget to require it with php if the composer autoloader didn’t work

require_once 'lib/swift_required.php';

Sending mail from outlook (hotmail)

Sending mails from outlook is a little bit different, as you can see outlook uses TLS encryption instead SSL and uses the port 587.

$transport = \Swift_SmtpTransport::newInstance() ->setUsername('[email protected]')->setPassword('mypassword') ->setHost('smtp-mail.outlook.com') ->setPort(587)->setEncryption('tls'); $mailer = \Swift_Mailer::newInstance($transport); $message = \Swift_Message::newInstance() ->setSubject($param['title']) ->setFrom(array('[email protected]' => 'I am someone')) ->setTo(array('[email protected]' => "[email protected]")) ->addPart("

Welcome

",'text/html') ; $result = $mailer->send($message);

Sending mail from google (gmail)

Google uses SSL encryption and the port 465. Is important to know that sometimes , the gmail accounts doesn’t allow to send mails with swiftmailer and you need to uncheck the property «Allows the use on unsafe devices» in your gmail account.

$transport = \Swift_SmtpTransport::newInstance('smtp.gmail.com', 465,'ssl')->setUsername('[email protected]')->setPassword('mypassword'); $mailer = \Swift_Mailer::newInstance($transport); $message = \Swift_Message::newInstance('Our Code World Newsletter') ->setFrom(array('[email protected]' => 'Our Code World')) ->setTo(array("[email protected]" => "[email protected]")) ->setBody("

Welcome

", 'text/html'); $result = $mailer->send($message);

Sending mail from zoho(zoho mail)

Zoho uses SSL encryption and the port 465.

$transport = \Swift_SmtpTransport::newInstance('smtp.zoho.com', 465,'ssl')->setUsername('[email protected]')->setPassword('mypassword'); $mailer = \Swift_Mailer::newInstance($transport); $message = \Swift_Message::newInstance('Test') ->setFrom(array('[email protected]' => 'Our Code World')) ->setTo(array("[email protected]" => "[email protected]")) ->setBody("

Welcome

", 'text/html'); $mailer->send($message);

A frequent question for the swiftmailer user is what is the smtp for hotmail, zoho or gmail (or what is the smtp for my email account) ?

Well, the following list contains the SMTP configuration lot of mail clients in the market :

PROVIDER URL SMTP SETTINGS
1&1 1and1.com smtp.1and1.com
Airmail Airmail.net mail.airmail.net
AOL Aol.com Smtp.aol.com
AT&T Att.net Outbound.att.net
Bluewin Bluewin.ch smtpauths.bluewin.ch
BT Connect Btconnect.com mail.btconnect.tom
Comcast Comcast.net smtp.comcast.net
Earthlink Earthlink.net smtpauth.earthlink.net
Gmail Gmail.com smtp.gmail.com
Gmx Gmx.net mail.gmx.net
HotPop Hotpop.com mail.hotpop.com
Libero Libero.it mail.libero.it
Lycos Lycos.com smtp.lycos.com
O2 o2.com smtp.o2.com
Orange Orange.net smtp.orange.net
Outlook.com (former Hotmail) Outlook.com smtp.live.com
Tin Tin.it Mail.tin.it
Tiscali Tiscali.co.uk smtp.tiscali.co.uk
Verizon Verizon.net outgoing.verizon.net
Virgin Virgin.net smtp.virgin.net
Wanadoo Wanadoo.fr smtp.wanadoo.fr
Yahoo Yahoo.com smtp.mail.yahoo.com
Zoho zoho.com/mail/ smtp.zoho.com

a piece of cake isn’t ? Please share this article if it was useful for you.

Источник

Оцените статью