How To Send Mails Using SMTP Server in PHP
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.
Fortunately 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
- Can send emails with multiple TOs, CCs, BCCs and REPLY-TOs
- Redundant SMTP servers
- Multipart/alternative emails for mail clients that do not read HTML email
- Support for 8bit, base64, binary, and quoted-printable encoding
- Uses the same methods as the very popular AspEmail active server (COM) component
- SMTP authentication
- Word wrap
- Address reset functions
- HTML email
- Tested on multiple SMTP servers: Sendmail, qmail, Postfix, Imail, Exchange, etc
- Works on any platform
- Flexible debugging
- Custom mail headers
- Multiple fs, string, and binary attachments (those from database, string, etc)
- 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.
Filed under Headline News, How To, Open Source Software, PHP, Tech Note, Web, Web Services |
|
RSS 2.0 |
Trackback this Article
|
Email this Article
You may also like to read |





































October 9th, 2006 at 1:21 pm
Just a mistake :
replace :
require(BASEPATH . ‘/class-phpmailer.php’);
by :
require(BASEPATH . ‘/class.phpmailer.php’);
October 9th, 2006 at 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.
November 7th, 2006 at 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.
November 7th, 2006 at 6:41 am
Ah, thanks. Corrected the typo.
January 29th, 2007 at 3:00 am
It’s good.
February 4th, 2007 at 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…
February 4th, 2007 at 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 ”;
?>
August 1st, 2007 at 11:29 am
Realy nice, but is this same for sending email using arabic languages, mean if format is arabic.