<?php
/**
* Start the timer
*/
$start_time = microtime(true);
/*Autoload file from phpmailer library
you can download this library from composer*/
require "../vendor/phpmailer/phpmailer/PHPMailerAutoload.php";
require "../classes/Config.php";
//Enable errors to display
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
//creating object for PHPMailer class in library
$mail = new PHPMailer();
try {
//configuration to send email
$mail->isSMTP();
$mail->Host = Config::SMTP_HOST;
$mail->Port = Config::SMTP_PORT;
$mail->SMTPAuth = true;
$mail->Username = Config::SMTP_USER;
$mail->Password = Config::SMTP_PASSWORD;
$mail->SMTPSecure = 'tls';
$mail->CharSet = 'UTF-8';
//Allow to display more debugging information from phpmailer
//$mail->SMTPDebug = 2;
//Send an email
$mail->setFrom('phpmailmail@gmail.com', 'gopibabu');
$mail->addAddress('gopinyca.php@gmail.com');
$mail->addAddress('s.gopibabu@gmail.com');
$mail->addCC('gopinyca.php@gmail.com');
$mail->addBCC('gopinyca.php@gmail.com');
//Add different reply address
$mail->addReplyTo('s.gopibabu@gmail.com','gopibabu');
//Adding Attachments and these attachements are in same directory where script resides.
//__FILE__ gives the full path of the current file_exists
//dirName() give the path of the directory where current file residing, without including file name.
$mail->addAttachment(dirName(__FILE__).'/sampleDoc.txt', 'new.txt');
//Allow HTML in Content
$mail->isHTML(true);
$mail->Subject = 'Testing PHPMailer Messages with styles and new Imag';
$mail->Body = 'Hi this is testing for sending email from <b style="color: red; font-style: italic">
php mail service.</b>
and it is send by <b>Gopi</b>'.'<br>'.'<img src="cid:newImage">';
//This is alternative message that we need to send for non-html supported clients.
$mail->AltBody = "Hello/ \n this is just alternative text for non html supported email clients";
//Displaying Inline image in the body of the message.
$mail->addEmbeddedImage(dirName(__FILE__).'/sampleImage.jpg', 'newImage');
//This Action will send the message that we build
$mail->send();
}
catch (Exception $e) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
}
$end_time = microtime(true);
$time = number_format($end_time - $start_time, 5);
/**
* Return to index.php
*/
header("Location:index.php?time=$time");
exit();
<?php
/**
* Here we are saving configuration details related to mail server that we are using to send email
*
* Class Config
*/
class Config
{
/**
* @var string
*/
const SMTP_HOST = 'smtp.gmail.com';
/**
* @var string
*/
const SMTP_PORT = 587;
/**
* @var string
*/
const SMTP_USER = 'phpmailmail@gmail.com';
/**
* Please make sure that your email not having 2 factor authentication
* If you have 2 factor authentication, you should enter custom app specific password
* you can access that from:
* https://security.google.com/settings/security/apppasswords
* @var string
*/
const SMTP_PASSWORD = 'Iamgopibabu';
}