“As we enjoy great advantages from inventions of others, we should be glad of an opportunity to serve others by any invention of ours; and this we should do freely and generously.”
Benjamin Franklin


You are not logged in.

#1 2010-06-15 03:49:13

decibelplaces
New member
Registered: 2010-06-14
Posts: 3

attach a file to the emails (code snippet)

My implementation requires attaching the same file, eg "download.zip" to the emails.

After testing some code snippets that I found via Google for multi-part mime attachments, I debugged them and inserted the code into example.php:

Code:

			
if ($sendMessage===-1)
{
$message_subject=$_POST['email_box'].$message['subject'];
$message_body=$message['body'].'<br /><br />'.$_POST['message_box'];
$headers="From: {$_POST['email_box']}";

//begin attach file

$fileatt = "/home/decibelp/public_html/OpenInviter/download.zip"; // Path to the file
$fileatt_type = "application/zip"; // File Type
$fileatt_name = "download.zip"; // Filename that will be used for the file as the attachment
$email_from = $_POST['email_box']; // Who the email is from
$email_subject = $message_subject; // The Subject of the email
$email_txt = $message_body; // Message that the email has in it
$file = fopen($fileatt,'rb');
$data = fread($file,filesize($fileatt));
fclose($file);
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";
$email_message .= "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type:text/html; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" . $email_txt;
$email_message .= "\n\n";
$data = chunk_split(base64_encode($data));
$email_message .= "--{$mime_boundary}\n" .
"Content-Type: {$fileatt_type};\n" .
" name=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mime_boundary}--\n"; 

//end attach file

foreach ($selected_contacts as $email=>$name)
    mail($email,$email_subject,$email_message,$headers);
$oks['mails']="Mails sent successfully";
}

Tested in Yahoo, Gmail, SMTP via Thunderbird

Offline

 

Board footer

Powered by PunBB
© Copyright 2002–2008 PunBB



All these trademarks/logos are properties of their respective companies and are hereby acknowledged. None of these trademarks may be used in connection with any product or service in any manner that is likely to cause confusion among customers, or in any manner that disparages or discredits their owner.