How To Send Mails Using SMTP Server in PHP
By Angsuman Chakraborty, Gaea News NetworkMonday, 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.
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.
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. SMTP configuration is done in that server . Thanks In Advance … Regards |
![]() ammu |
![]() ammu |
![]() Best Free Ads |
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” “Now you can send a mail using any SMTP server. Here is a simple example” “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: FName: Lname : Email-id: Company: Designation: Address1: Address2: Address3: City: State: Pincode: Phone: Fax: Mobile:
‘;/* To send HTML mail, you can set the Content-type header. */ /* additional headers */ /* and now mail it */ |
February 4, 2007: 11:09 pm
hi… 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: FName: Lname : Email-id: Company: Designation: Address1: Address2: Address3: City: State: Pincode: Phone: Fax: Mobile:
‘;/* To send HTML mail, you can set the Content-type header. */ /* additional headers */ /* and now mail it */ and html Salutation table td.title{ function validation() return valid; Registration Form Salutation: Dr *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 |
![]() 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. |
October 9, 2006: 1:21 pm
Just a mistake : replace : by : |
Balaram