Send Emails Using GMail Send Emails Using GMail

How to Send Emails Using SMTP Servers of GMail

how to send emails using SMTP servers of GMail Email Service providers have POP and SMTP servers for incoming and outgoing Emails. SMTP is for Outgoing (Sending) emails and POP is for Incoming (Receiving) emails. If you have an account in email services, it means that you can send or receive emails using these servers. This service is free, fast and secure. You can change the “From” address and the “From” name if you send emails using SMTP server. you know how to send email form contact form using php In this post I will tell you how to send emails using SMTP servers of GMail.

We are going to use PHPMailer class for sending emails. It’s an easy usable PHP library for sending emails first of all download the PHPMailer library from this link : http://sourceforge.net/projects/phpmailer/

after download phpmailer unzip it and upload in ualshibli_nf directory.

Simple php code for send email

copy-paste the following script using your editor and save this file as “sendmail.php” and try it in your localhost server

<?php

require_once(‘class.phpmailer.php’);

$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = ‘smtp.gmail.com’; // Specify main and backup server
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = ‘nafis@gmail.com’; // SMTP username
$mail->Password = ‘nafisflahi’; // SMTP password
$mail->SMTPSecure = ‘tls’; // Enable encryption, ‘ssl’ also accepted
$mail->Port = 587; //Set the SMTP port number – 587 for authenticated TLS
$mail->setFrom(‘nafis@gmail.com’, ‘Amit Agarwal’); //Set who the message is to be sent from
$mail->addReplyTo(‘ahmad@gmail.com’, ‘First Last’); //Set an alternative reply-to address
$mail->addAddress(‘josh@example.net’, ‘Josh Adams’); // Add a recipient
$mail->addAddress(‘ellen@example.com’); // Name is optional
$mail->addCC(‘cc@example.com’);
$mail->addBCC(‘bcc@example.com’);
$mail->WordWrap = 50; // Set word wrap to 50 characters
$mail->addAttachment(‘/usr/nafis/file.doc’); // Add attachments
$mail->addAttachment(‘/images/image.jpg’, ‘new.jpg’); // Optional name
$mail->isHTML(true); // Set email format to HTML

$mail->Subject = ‘Here is the subject’;
$mail->Body = ‘This is the HTML message body in bold!‘;
$mail->AltBody = ‘This is the body in plain text for non-HTML mail clients’;

//Read an HTML message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body
$mail->msgHTML(file_get_contents(‘contents.html’), dirname(__FILE__));

if(!$mail->send()) {
echo ‘Message could not be sent.’;
echo ‘Mailer Error: ‘ . $mail->ErrorInfo;
exit;
}

echo ‘Message has been sent’;

?>

That’s it
here using this simple script you can send emails, try it in your localhost server, but make sure that you are connected to the internet

Leave a Reply