Sending Email using PHP

Published:
Updated:
Are you going to be sending email in PHP? Don't use the mail() function, unless your site's email needs are very basic and you are running on a Linux/Unix machine. It is light on features and not built in to the Windows version.

The best approach is to use a 3rd party mail program. In the past I have used PHPMailer and I love it. I make many posts with this solution to SMTP email problems in PHP and this is the fix!

http://sourceforge.net/projects/phpmailer

Below is the PHP code to use it. Place class.phpmailer.php into your php includes directory.

Replace text in << >> with your server information: http://www.emailaddressmanager.com/tips/mail-settings.html
<?php
 
require("class.phpmailer.php");
 
$mail = new PHPMailer();
 
$mail->IsSMTP(); // send via SMTP
$mail->Host     = "<<smtpserver>>:<<stmpport>>"; // SMTP servers
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "<<uname>>"; // SMTP username
$mail->Password = "<<pword>>"; // SMTP password
$mail->From = "<<email address from>>";
$mail->FromName = "<<email name from>>";
$mail->AddAddress("<<email address to>>"); 
$mail->AddReplyTo("<<email address from>>","<<email name from>>");
$mail->WordWrap = 50; // set word wrap
$mail->IsHTML(true); // send as HTML
$mail->Subject  =  "Subject";
$mail->Body     =  "Body text";
$mail->AltBody  =  $mail->Body;
        
$mail->Send();
 
?>

Open in new window

3
7,034 Views

Comments (6)

CERTIFIED EXPERT
Top Expert 2006

Commented:
Can you please clarify:
>>It is ... not built in to the Windows version.

Is this a new 6.X default setting I am not aware of?
Richard QuadlingSenior Software Developer

Commented:
Mail is a standard function within PHP, for both win32 and non win32 platforms.

As long as you know how to build MIME mail, the builtin mail() function is fine.

As of 5.3.0 it can also produce a log of all mail sent.

http://docs.php.net/manual/en/function.mail.php

But there is a significant difference between the win32 and non win32 implementations as summarised in this note on that above page ...

Note: The Windows implementation of mail() differs in many ways from the Unix implementation. First, it doesn't use a local binary for composing messages but only operates on direct sockets which means a MTA is needed listening on a network socket (which can either on the localhost or a remote machine).
Second, the custom headers like From:, Cc:, Bcc: and Date: are not interpreted by the MTA in the first place, but are parsed by PHP.
As such, the to parameter should not be an address in the form of "Something <someone@example.com>". The mail command may not parse this properly while talking with the MTA.


So, whilst it is not perfect, it does exist.

And having said all that, using a class to coordinate all of this is a LOT easier.

SWIFT is one the PHP built in code to access and send the mail using "SMTP Sever". Here you can found library as well as tutorial to implement it.
http://swiftmailer.org/

Regards,
SAkthivel
Hi,
May i know what else setting has to be done on the linux server. So as to send the mail.
Does any services like 'sendmail','postfix','qmail' has to be configured.
I want to send the basic mail through my server.
Avinash ZalaWeb Expert

Commented:
i think this link has all the things which you need..

PHP Send Mail using SMTP

View More

Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.

Get access with a 7-day free trial.
You Belong in the World's Smartest IT Community