Sending mail from myhost/gmail

When I was new to php I was sending mail using php’s basic mail() function, but later I came across phpMailer class, which can be used to send mail using any host of your choice (Gmail, for example).
This is an simple code script needed to send an mail using your host and login details.

<?php
//including phpMailer class
require_once("phpmailer/phpmailer.php");
//creating object for phpMailer class
$mail = new PHPMailer();
IsSMTP();
$mail->SMTPAuth = true;
$mail->From = "Your Email";// example stufflies@domain.com
$mail->FromName = "Your Name";// example Stuff Lies
$mail->Username = "Your Username"; //example stufflies, for gmail username@gmail.com
$mail->Password = "Your Password";//example *******
$mail->Host = "Your Host name"; //example for gmail smtp.gmail.com
$mail->SMTPSecure = "ssl";//ssl/tls
$mail->Port = "Your Host port number";//example for gmail 465
$mail->AddReplyTo("Your Email","Your name");
$mail->AddAddress("Recepient email address","Recepient name");
$mail->IsHTML(true);
$mail->Subject = "Your subject"; //example Stufflies phpMailer
//$mail->AddAttachment("temp file", "filename");//remove comments if attachment required
$mail->Body = "Your message"; //example Testing message from stufflies.wordpress.com
if($mail->send())
{ echo "Mail sent"; }
else { echo "Sending failed"; }
?>

The code uses phpMailer class which can be downloaded from phpMailer Download.

Although every effort is made to make this code simple , yet if any issue arise feel free to comment or contact me.

8 responses to “Sending mail from myhost/gmail

  1. I got what you mean, thanks for putting up. Woh I am pleased to chance this website through google. Thanks For Share Sending mail from myhost/gmail STUFF LIES.

  2. Thanks for sharing mate ! šŸ™‚ useful post !!

  3. WONDERFUL Post.thanks for share..more wait .. ?

  4. Jeremy Peterson

    Hello rootnode, I was searching for this stuff from long time but not getting an proper one, it worked and i can send attachments too. Thanks

  5. Yes Michael you are right that we can use an external SMTP server with PHP SMTP package, I have used it also but I found phpMailer as more standard and efficient way to mail, so i mentioned it here.
    The phpMailer package is enclosed with two classes a phpMailer class and also an SMTP class while SMTP package contains only one single smtp class, the two classes in phpMailer package makes it more effective and flexible to use.

  6. I tried to use PHP STMP Package before, that is much better to mail().
    You can use an external STMP Server.

Leave a reply to LA Weight Loss Cancel reply