Skip to content

Instantly share code, notes, and snippets.

@rahman541
Created May 27, 2019 15:14
Show Gist options
  • Save rahman541/9740a35436b8e72b3f87e33ec1ba543e to your computer and use it in GitHub Desktop.
Save rahman541/9740a35436b8e72b3f87e33ec1ba543e to your computer and use it in GitHub Desktop.
SMTP Gmail sending mail.
<?php
// REF: https://swiftmailer.symfony.com/docs/introduction.html
// composer require "swiftmailer/swiftmailer:^6.0"
require_once './vendor/autoload.php';
$MAIL_HOST = 'smtp.gmail.com';
$MAIL_PORT = '465';
$MAIL_USERNAME = 'server@gmail.com';
$MAIL_PASSWORD = 'mypwd';
$MAIL_ENCRYPTION = 'ssl';
$RECEIVER_MAIL = 'test@yahoo.com';
// Create the Transport
$transport = (new Swift_SmtpTransport($MAIL_HOST, $MAIL_PORT, $MAIL_ENCRYPTION))
->setUsername($MAIL_USERNAME)
->setPassword($MAIL_PASSWORD);
// Create the Mailer using your created Transport
$mailer = new Swift_Mailer($transport);
// Create a message
$message = (new Swift_Message('Wonderful Subject'))
->setFrom(['john@doe.com' => 'John Doe'])
->setTo([$RECEIVER_MAIL => 'A name'])
->setBody('Here is the message itself');
// Send the message
$result = $mailer->send($message);
echo $result;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment