Sending Email with PHP Networking (
Page 1 of 5 )
In this article we will look at the protocol that is involved in sending email messages. We will also examine the thorny issue of how to send an attachment with an email message. This article is the second of two parts.Setting up PHP to send email
Before we do anything else, let's enable PHP to send email. This is a very simple process. Open up your configuration file and go to the section that deals with emailing. To save time, open up the find dialog box and enter "mail." This will take you to the mail section of the configuration file. It looks something like this:
[mail function]
; For Win32 only.
SMTP = localhost
smtp_port = 25
; For Win32 only.
;sendmail_from = me@example.com
; For Unix only. You may supply arguments as well (default: "sendmail -t -i").
;sendmail_path =
You need to change the first two settings here, like so:
SMTP = my.mailserver.com
This is where you put the name of your outgoing mail server.
The second setting is the return address that is sent with all your email messages, so change it to whatever email address you want there:
; For Win32 only.
;sendmail_from = myreturn@emailaddress.com
These two settings are for Windows users. The third option is for Unix systems, as indicated in the configuration file. The default settings are usually correct and work most of the time. If these settings don't work, then your system or network administrator will be able to help.
That's about it. So how does PHP help in sending email messages? PHP provides a function called mail(). This function takes the following arguments:
-
To - this is the email address to which the message goes.
-
subject - subject of the message.
-
Message - the actual message goes here.
-
Headers (optional) - Any extra headers, such as from, CC and BCC.
The functions returns true if the message has been accepted for delivery and false if not. In its simplest form, you use the mail() function like so:
<?php
$to = "
esme@holidays.com
";
$subject= "Lunch?";
$message = "This is my first test of the mail() function.";
// Send
$result=mail($to,$subject, $message);
?>
With Headers it would look something like this:
<?php
$to = "
esme@holidays.com
";
$subject= "Lunch?";
$message = "This is my first test of the mail() function.";
$headers = 'From: webmaster@example.com' . "rn" .
çc: webmaster@example.com' . "rn" .
bcc: ac@milan.com;
$result=mail($to, $subject, $message, $headers);
?>