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.

TABLE OF CONTENTS:
  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
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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:


<?php

$to = "esme@holidays.com";

$subject= "Lunch?";

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

$headers ="Content-disposition: attachment;

filename=myfile.docn";

$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
   

PHP ARTICLES

- 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...
- PHP: Building Concrete Validators
- Sanitizing Input with PHP
- Executing Shell Commands with PHP
- Handling File Data with PHP
- File Security and Resources with PHP
- ArrayObject PHP Class Examples
- ArrayObject PHP Class: An Introduction
- Getting File System Data with PHP
- PHP Tools for Working with the File and Oper...
- Working with the File and Operating System w...
- PHP Proxy Patterns: Completing a Blog


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 6 - Follow our Sitemap

Dev Shed Tutorial Topics: