phpeveryday.com

The best tutorial of php, php framework, php strategies, object oriented oriented,


Email: Send Attachement Mail

Tag: email   Category: PHP Basic
post: 19 Nov 2007 read: 5,388


After learn send email with dual format (HTML/text), we will learn how to send email with attachment. In this situation, we will make the primary MIME type be multipart/mixed so that we can do an attachment or two.


<?php
// Setting a timezone, mail() uses this.
date_default_timezone_set('America/New_York');
// recipients
$to  = "you@phpeveryday.com" . ", " ; // note the comma 
$to .= "we@phpeveryday.com"; 

// subject
$subject = "Test for Attachement"; 

// Create a boundary string.  It needs to be unique
$sep = sha1(date('r', time()));

// Add in our content boundary, 
// and mime type specification: 
$headers .=
    "\r\nContent-Type: multipart/alternative; 
boundary=\"PHP-alt-{$sep}\"";

// Read in our file attachment
$attachment = file_get_contents('attachment.zip');
$encoded = base64_encode($attachment);
$attached = chunk_split($encoded);

// additional headers
$headers .= "To: You <you@phpeveryday.com>, 
We <we@phpeveryday.com>\r\n"; 
$headers .= "From: Me \r\n"; 
$headers .= "Cc: he@phpeveryday.com\r\n"; 
$headers .= "Bcc: she@phpeveryday.com\r\n";

// Your message here:
$body =<<<EOBODY
--PHP-alt-{$sep}
Content-Type: text/plain

Hai, It's me!

--PHP-alt-{$sep}
Content-Type: text/html

<html>
<head>
<title>Test HTML Mail</title>
</head>
<body>
<font color='red'>Hai, it is me!</font>
</body>
</html>

--PHP-alt-{$sep}--

--PHP-mixed-{$sep}
Content-Type: application/zip; name="attachment.zip"
Content-Transfer-Encoding: base64
Content-Disposition: attachment

{$attached}

--PHP-mixed-{$sep}--
EOBODY;

// Finally, send the email
mail($to, $subject, $body, $headers);
?>


Series this article:
Email: Sending a Simple Email (Part 1)
Email: Sending a Simple HTML Mail (Part 2)
Email: Sending Dual Format (Part 3)
Email: Send Attachement Mail
PHP Email: Using Embedded Images in HTML Email
PHP Email: Sending Mass Email use BCC
PHP Email: Protecting Email Address from Spam Collectors

| Give Your Opinion | Recommend
Share and Bookmark to: These icons link to social bookmarking sites where readers can share and discover new web pages.
digg del.icio.us technorati Ma.gnolia BlinkList

Recommended articles by other readers:
Web Services: How PHP Kiss VB.NET? (Part 1)
Chart: How to Build Cool Animation Real Time Chart
Joomla: Fast Road to Understand Component Programming
Email: Send Attachement Mail
SMS : Sending SMS with PHP and ActiveXperts (Part 1)

1 Responses to Email: Send Attachement Mail:

You can use PHPMailer, too. Here is a link: http://weblatam.com/wp/enviar-e-mails-con-php-usando-phpmailer/196/

What do You Think?
Your Name *:
Email *:
(Will not be published)
Website/URL:
Your Comment *:
* Required


624
posting