Home arrow PHP arrow Page 2 - Sending Email with PHP Networking

Sending mail with PHP - PHP

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



Now that we know what tools PHP provides, let's write a quick application that will help us send messages with attachments. In order to send a message with an attachment, you need to send a mail header instructing that the email be sent as an attachment. A header might look like this:

Content-disposition: attachment; filename=myfile.doc

The header instructs the email to send the message as an attachment with a file called myfile.doc. So if you want to send a short message with an attachment, this is how it could look:


$to = "esme@holidays.com";

$subject= "Lunch?";

$message = "This is my first test of the mail() function.";

$headers ="Content-disposition: attachment;


$headers .= "cc:salles@mycompany.comn";

$result=mail($to, $subject, $message, $headers);


Another very important step that you need to take when sending attachments is to set the content type for the file. The following are the available content types:

  • image/gif
  • image/jpeg
  • audio/x-wav
  • audio/vnd.rn-realaudio
  • video/mpeg
  • video/avi

You also may want to send application files. Some are text files, and some are binary files. For example, an RTF file is a text file, whereas a Word document is a binary file. Generally binary files are of type applica/octed stream. Binary files require encoding when they are sent through an email. PHP provides us with a function called chunk_split(), that does just that:

$encode = chunk_split(base64_encode($string));

In addition, when sending an encoded file, you need to inform the mail software that you intend to sent a encoded file. This is done in the headers section of the mail function:

Content-Transfer-Encoding: base64

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