<

Sending Email using PHP

Published on
10,487 Points
6,187 Views
3 Endorsements
Last Modified:
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
Comment
6 Comments
 
LVL 40

Expert Comment

by:Richard Quadling
0
 
LVL 49

Expert Comment

by:Roonaan
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?
0
 
LVL 40

Expert Comment

by:Richard Quadling
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.

0
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 

Expert Comment

by:sakthibe82
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
0
 
LVL 15

Expert Comment

by:Insoftservice
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.
0
 
LVL 12

Expert Comment

by:Avinash Zala
i think this link has all the things which you need..

PHP Send Mail using SMTP
0

Featured Post

Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

Join & Write a Comment

This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month