how to use gmail to send emails using PHPMailer

5
Apr

How to use gmail to send emails using PHPMailer

1 Download PHPMailer from http://phpmailer.sourceforge.net
2 Extract to folder phpmailer
3 Create a file email.php
4 Paste this code and change the values in blue as you need (I modified the sample code given on the PHPMailer homepage)
IsSMTP(); // send via SMTP
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "username@gmail.com"; // SMTP username
$mail->Password = "password"; // SMTP password
$webmaster_email = "username@doamin.com"; //Reply to this email ID
$email="username@domain.com"; // Recipients email ID
$name="name"; // Recipient's name
$mail->From = $webmaster_email;
$mail->FromName = "Webmaster";
$mail->AddAddress($email,$name);
$mail->AddReplyTo($webmaster_email,"Webmaster");
$mail->WordWrap = 50; // set word wrap
$mail->AddAttachment("/var/tmp/file.tar.gz"); // attachment
$mail->AddAttachment("/tmp/image.jpg", "new.jpg"); // attachment
$mail->IsHTML(true); // send as HTML
$mail->Subject = "This is the subject";
$mail->Body = "Hi,
This is the HTML BODY "; //HTML Body
$mail->AltBody = "This is the body when user views in plain text format"; //Text Body
if(!$mail->Send())
{
echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
echo "Message has been sent";
}
?>
5 Open the file class.smtp.php in phpmailer directory
6 Paste this code

$host = "ssl://smtp.gmail.com";
$port = 465;

Hint: Search for #connect
7 Open this page in browser and it will send the email using GMail.
Hint: When you want to email the details from a form, set the variables using the form variables.
eg. $mail->Username=$_POST['email']

Share/Save
9.42857
Average: 9.4 (7 votes)
Your rating: None

4 comments

13
Feb

I am a n00b to php. Can you

I am a n00b to php. Can you explain in more detail how I could use this in a form. Maybe give me a quick example. I appreciate it.

13
Feb
14
Feb

Thanks, but that did not help

Thanks, but that did not help solve my problem, and none of the links on that site even work. I do appreciate it thought. I will say it increased my understanding a little.

14
Feb

Thanks, but that did not help

Thanks, but that did not help solve my problem, and none of the links on that site even work. I do appreciate it thought. I will say it increased my understanding a little.

Post new comment

 
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.

Donate to Us


Activity Stream

Who's online

There are currently 0 users and 15 guests online.
Theme designed by Donny Carette - Powered by Drupal - copyright © 2010