Home arrow PHP arrow Sending Email with PHP Networking

Sending Email with PHP Networking

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.

  1. Sending Email with PHP Networking
  2. Sending mail with PHP
  3. Application Code
  4. Code Examined
  5. Sending Mail using the PEAR::Mail
By: David Web
Rating: starstarstarstarstar / 3
September 15, 2008

print this article



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:


$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:


$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);


>>> More PHP Articles          >>> More By David Web

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: