DEV Community

Cover image for Sending Gmail with Swiftmailer
kali kimanzi
kali kimanzi

Posted on • Updated on

Sending Gmail with Swiftmailer

adding mailing functionality to website is quite an essential part of website development. most website have this sections either in the contact us section, feedback sections among other sections like registration and sign up. It can be quite a hustle when you are beginner because we have you need to have a solutions that works without too much hassle, I prefer swift mailer because i find it straight of the box solutions compared to vanilla solutions and PHPMAILER(you are allowed to have an alternative view who cares though ). This tutorial assumes you have installed your XAMPP or whatever server you have there and also prior knowledge on PHP is a bonus .

To avoid too much stories let's get straight to it.

Steps

  1. Download swiftmailer
  2. create mailing script
  3. Enable your gmail account for IMAP
  4. Testing your mailing script

1. Download swiftmailer

In your htdocs folder create your working director in my case i have named it sendmail.

inside the sendmail directory install swiftmailer with composer. Check also on the swiftmailer official website here

composer require "swiftmailer/swiftmailer:^6.0"
Enter fullscreen mode Exit fullscreen mode

2. Create mailing script

This is a simple script which contains mailing function and html form, the message is hardcoded but for demonstration purpose. create this script inside your working directory eg sendmail

<?php

if (isset($_POST["email"])){

//echo $_POST["email"] ;

require_once './vendor/autoload.php';

// Create the Transport
$transport = (new Swift_SmtpTransport('smtp.gmail.com', 465, 'ssl'))
  ->setUsername('putyouremail@gmail.com')
  ->setPassword('putyouremailpassword')
;

// Create the Mailer using your created Transport
$mailer = new Swift_Mailer($transport);

// Create a message
$message = (new Swift_Message('Mail tutorial '))
  ->setFrom(['putyouremail@gmail.com' => 'sending mail tutorial'])
  ->setTo([$_POST["email"] => 'testing the mail sending option '])
  ->setBody('congrats you have send your firstmail cheers to your new skill')
  ;

// Send the message
$result = $mailer->send($message);

}
?>

<form action="" method="post">
<input type="text" name="email" placeholder = "Email Me">
<input type="submit" value="Email me">
</form>
Enter fullscreen mode Exit fullscreen mode

3. Enable your gmail account for IMAP

after you have created this script use the following address to enable IMAP settings due to google security settings

https://www.google.com/settings/security/lesssecureapps

4. Testing your mailing script

Run your script on local host and enjoying using it. you can create complicated mailing systems with swiftmailer among other things. Happy coding

ABOUT ME

I hope you have liked this article.
I am Kali Kimanzi a Masters student Energy Informatics at University of Applied Sciences Upper Austria, I am also a software developer at NTUITY a brand of neoom group Gmbh Wien | Freistadt. I take interest in Data sciences using python and I use python libraries like pandas, numpy and Matplotlib. I specialize in Python, PHP/Laravel, Java.
Social media links
Kali kimanzi @ Facebook: Kali Kimanzi
Kali Kimanzi @ Linkedin: Kali Kimanzi
Kali Kimanzi @ instagram : Kali_Kimanzi
Kali Kimanzi GitHub: Kali Kimanzi
kali kimanzi Twitter : Kali Kimanzi

Top comments (0)