How To Send Mails Using SMTP Server in PHP

By Angsuman Chakraborty, Gaea News Network
Monday, October 9, 2006

In PHP on Microsoft Windows you have to simply configure two parameters to enable sending mails through SMTP server. In Unix / Linux it is slightly more complicated. The solution, however, is much more powerful and works also on Windows. Let’s first start with Windows.

In Microsoft Windows PHP installation you just have to change two variables in php.ini:

SMTP = smtp.server.com
smtp_port = 25

Replace smtp.server.com with your SMTP server name and 25 with your SMTP server port (normally 25).

You can also set the default sender information in Windows:
sendmail_from = me@example.com

On Linux / Unix PHP relies on sendmail. You can specify the sendmail path here:
sendmail_path = /usr/sbin/sendmail -t -i

Unfortunately this doesn’t work too well if your SMTP server is configured on a different machine or you are not using sendmail.

PHPMailerFortunately there is a much better solution in PHPMailer. The default mail capability provided by mail() function in PHP is very limited. PHPMailer is a full fledged mail API which can be used to do any kind of mailing tasks.

Features of PHPMailer

  1. Can send emails with multiple TOs, CCs, BCCs and REPLY-TOs
  2. Redundant SMTP servers
  3. Multipart/alternative emails for mail clients that do not read HTML email
  4. Support for 8bit, base64, binary, and quoted-printable encoding
  5. Uses the same methods as the very popular AspEmail active server (COM) component
  6. SMTP authentication
  7. Word wrap
  8. Address reset functions
  9. HTML email
  10. Tested on multiple SMTP servers: Sendmail, qmail, Postfix, Imail, Exchange, etc
  11. Works on any platform
  12. Flexible debugging
  13. Custom mail headers
  14. Multiple fs, string, and binary attachments (those from database, string, etc)
  15. Embedded image support

How to use PHPMailer
To use PHPMailer you need to first download the files and save the relevant files (upload) on your server.

You need to upload class.phpmailer.php, class.smtp.php (for SMTP support) and language/phpmailer.lang-en.php. You should download the lang file corresponding to the language of your blog. For my english language sites I use language/phpmailer.lang-en.php, where en is the language code.

In your PHP file include class.phpmailer.php as follows:

if(!class_exists('PHPMailer')) {
    require(BASEPATH . '/class.phpmailer.php');
}

Replace BASEPATH with the actual path of class-phpmailer.php file. You may also define BASEPATH to achieve the same result (preferred).

Note: This check ensures only one copy of the class is loaded. This in turn loads other required classes.

Now you can send a mail using any SMTP server. Here is a simple example:

$mail = new PHPMailer();

$mail->From     = $senderemail;
$mail->FromName = $sendername;
$mail->AddAddress($receiveremail, $receivername);
// Fill in Username and Password for servers requiring authentication
$mail->Username = $smtp_username;
$mail->Password = $smtp_password;

// SMTP server name
$mail->Host     = $smtp_server;
$mail->Mailer   = "smtp";

$mail->Subject = $mail_subject;
$mail->Body    = $mail_body;

if(!$mail->Send()) $results = 'Error message';
else $results = 'Success message';

You can read the documentation, advanced example or the tutorial if you need further help.

Discussion

Balaram
June 15, 2010: 3:51 pm

Hi all

I need to send a mail from server to ‘hfdiuf@gmail.com’ and inaddition to it I want to include the DB statistics like “df -h” ,”vmstat ” and other things in that mail. Please some one help me on this.
Pleaseeee reply to mail Id (balarami.dwaram-v@retail.adityabirla.com).

SMTP configuration is done in that server .

Thanks In Advance …

Regards
Balarami


ammu
May 6, 2010: 2:20 am

hi…………


ammu
May 6, 2010: 2:16 am

hi………..


Best Free Ads
November 2, 2009: 9:39 am

Its good. thanks


XTC
August 19, 2009: 7:18 am

Sorry dude but as a teacher and a tutorial this does absolutely nothing…

“download the files and upload to your server”
Upload what and to where and on what server?

“Now you can send a mail using any SMTP server. Here is a simple example”
Nope, that does absolutely nothing…

“You can read the documentation, advanced example or the tutorial if you need further help.”

Out of date links…

Anyone really know how to write a simple step by step guide that actually works when it is followed step by step ???

August 1, 2007: 11:29 am

Realy nice, but is this same for sending email using arabic languages, mean if format is arabic.

February 4, 2007: 11:11 pm

Registration Confirmed

Registration Page

Registration

Salutation:
‘.$salutation.’

FName:
‘.$fname.’

Lname :
‘.$lname.’

Email-id:
‘. $mail.’

Company:
‘. $company.’

Designation:
‘. $designation.’

Address1:
‘. $Add1.’

Address2:
‘. $Add2.’

Address3:
‘. $Add3.’

City:
‘. $City.’

State:
‘. $state.’

Pincode:
‘. $pincode.’

Phone:
‘. $phone.’

Fax:
‘. $fax.’

Mobile:
‘. $Mobile.’

 

‘;/* To send HTML mail, you can set the Content-type header. */
$headers = “MIME-Version: 1.0\r\n”;
$headers .= “Content-type: text/html; charset=iso-8859-1\r\n”;

/* additional headers */
$headers .= “From: “.$mail;

/* and now mail it */
mail($to, $subject, $message, $headers);
echo ”;
?>

February 4, 2007: 11:09 pm

hi…
im very new to this field…i want something to be done like,if i submit a form ,i should get a mail to intelevents@kestone.in….to do tjhis i have followed thw following steps….

1..i have uploaded my php file and html file on to server through winSCP…

2.in browser i opend the file by giving the url kestone.in/reg1.htm..

but im not getting the mail…

here is my php file

Registration Confirmed

Registration Page

Registration

Salutation:
‘.$salutation.’

FName:
‘.$fname.’

Lname :
‘.$lname.’

Email-id:
‘. $mail.’

Company:
‘. $company.’

Designation:
‘. $designation.’

Address1:
‘. $Add1.’

Address2:
‘. $Add2.’

Address3:
‘. $Add3.’

City:
‘. $City.’

State:
‘. $state.’

Pincode:
‘. $pincode.’

Phone:
‘. $phone.’

Fax:
‘. $fax.’

Mobile:
‘. $Mobile.’

 

‘;/* To send HTML mail, you can set the Content-type header. */
$headers = “MIME-Version: 1.0\r\n”;
$headers .= “Content-type: text/html; charset=iso-8859-1\r\n”;

/* additional headers */
$headers .= “From: “.$mail;

/* and now mail it */
mail($to, $subject, $message, $headers);
echo ”;
?>

and html

Salutation

table td.title{
text-align:center;font-family:arial;font-weight:normal;font-size:12px}

function validation()
{
valid = true;
objform=document.forms["regform"];
if(objform.Fname.value ==”")
{
alert(”Please enter the ‘First Name’”);
objform.Fname.focus();
valid=false;
}
else
if(objform.email.value==”")
{
alert(”Please enter the Email address”);
objform.email.focus();
valid=false;
}
else
if(objform.company.value==”")
{
alert(”Please enter the ‘Company’”);
objform.company.focus();
valid=false;
}
else
if(objform.designation.value==”")
{
alert(”Please enter the designation”);
objform.designation.focus();
valid=false;
}
else
if(objform.add1.value==”")
{
alert(”Please fill the ‘Address’”);
objform.add1.focus();
valid=false;
}
else
if(objform.city.value==”")
{
alert(”Please enter the ‘City’”);
objform.city.focus();
valid=false;
}
else
if(objform.pin.value==”")
{
alert(”Please enter the ‘Pin’”);
objform.pin.focus();
valid=false;
}
else
if(objform.mobile.value==”")
{
alert(”Please enter the ‘mobile number’”);
objform.mobile.focus();
valid=false;
}

return valid;
}

Registration Form

Salutation:                          

Dr
Mr
Mrs
Ms
Miss

*First Name: 

Last Name:   

*Email :          

*Company   : 

*Designation:

*Address1:    

Address2:     

Address3     :

*City:               

State:             

*Pin   :            

Phone:          

Fax:                

*Mobile:         

can anybody help in this issue….its very very very urgent…ill be very thanful to u…


chok
November 7, 2006: 6:41 am

Ah, thanks. Corrected the typo.


Olly
November 7, 2006: 2:49 am

He was actually referring to the fact that the file is named class.phpmailer.php and not class-phpmailer.php as in the original article.

October 9, 2006: 7:40 pm

Though your comment didn’t come out correctly, I am guessing you were referring to normal single quotes. This problem is caused by WordPress. I have corrected this post to remove fancy quotes.
Here’s how you can remove fancy quotes from your WordPress blogs or comments.

October 9, 2006: 1:21 pm

Just a mistake :

replace :
require(BASEPATH . ‘/class-phpmailer.php’);

by :
require(BASEPATH . ‘/class.phpmailer.php’);

YOUR VIEW POINT
NAME : (REQUIRED)
MAIL : (REQUIRED)
will not be displayed
WEBSITE : (OPTIONAL)
YOUR
COMMENT :