Mail attachments

the mail() function

Moderators: macek, egami, gesf

Speegster
New php-forum User
New php-forum User
Posts: 7
Joined: Fri Mar 04, 2005 2:18 am

Mail attachments

Postby Speegster » Tue Mar 15, 2005 12:46 am

Does the mail() funtion in PHP support email attachments? Does anyone know any scripts that will send emails with attachments?

User avatar
Alexej Kubarev
Site Admin
Site Admin
Posts: 2223
Joined: Fri Mar 05, 2004 7:15 am
Location: Täby, Stockholms län
Contact:

Postby Alexej Kubarev » Tue Mar 15, 2005 12:47 am

Please read the manual before you post... i belive there is a script someone posted in the comments that will allow you sending attachments..


And please post everything realted to Mail() function into php coding => Mail() forum... this post is now moved..

User avatar
Alexej Kubarev
Site Admin
Site Admin
Posts: 2223
Joined: Fri Mar 05, 2004 7:15 am
Location: Täby, Stockholms län
Contact:

Postby Alexej Kubarev » Tue Mar 15, 2005 2:06 am

if you somehow in the world cant find it for yourself here is the link: http://se.php.net/manual/en/ref.mail.php they have a very good script there... in the comments.. something like this:

Code: Select all

How to add multiple attachment to an email:

An email can be split into many parts separated by a boundary followed by a Content-Type and a Content-Disposition.

The boundary is initialized as follows:
<?php
$boundary = '-----=' . md5( uniqid ( rand() ) );
?>

You can attach a Word document if you specify:
<?php
$message .= "Content-Type: application/msword; name=\"my attachment\"\n";
$message .= "Content-Transfer-Encoding: base64\n";
$message .= "Content-Disposition: attachment; filename=\"$theFile\"\n\n";
?>

When adding a file you must open it and read it with fopen and add the content to the message:
<?php
$path = "whatever the path to the file is";
$fp = fopen($path, 'r');
do //we loop until there is no data left
{
       $data = fread($fp, 8192);
       if (strlen($data) == 0) break;
       $content .= $data;
     } while (true);
$content_encode = chunk_split(base64_encode($content));
$message .= $content_encode . "\n";
$message .= "--" . $boundary . "\n";
?>

Add the needed headers and send!
<?php
$headers  = "From: \"Me\"<me@here.com>\n";
$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-Type: multipart/mixed; boundary=\"$boundary\"";
mail('myAddress@hotmail.com', 'Email with attachment from PHP', $message, $headers);
?>

Finally, if you add an image and want it displayed in your email, change the Content-Type from attachment to inline:

<?php
$message .= "Content-Disposition: inline; filename=\"$theFile\"\n\n";
?>

User avatar
ruturajv
php-forum Super User
php-forum Super User
Posts: 1280
Joined: Sat Mar 22, 2003 9:42 am
Location: Mumbai, India
Contact:

Postby ruturajv » Wed Mar 16, 2005 8:30 pm

check phpmailer


Return to “PHP coding => Mail”

Who is online

Users browsing this forum: Bing [Bot] and 1 guest