trouble with encoding emails

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
c_pattle
Forum Newbie
Posts: 9
Joined: Sat Apr 24, 2010 7:35 am

trouble with encoding emails

Post by c_pattle »

Hey everyone

I've got a loop that loops through attachments and then sends them by email. When I sent it to my address the attachments got there fine. However when I sent it to a different address the attachments got there but were all empty as if they hadn't been encoded properly. Do you think this could be problem with my script or a problem with the different email addresses? I've copied my script below.

Code: Select all

<?php 

$to = "alex@colourbrush.co.uk"; 
$subject = "You have been sent some content by " . $_REQUEST['order_company_name']; 
$email = $_REQUEST['order_email'] ; 


foreach ($_FILES['att']['error'] as $key => $error) {
  if (UPLOAD_ERR_OK === $error) {

$att_path = $_FILES['att']['tmp_name'][$key];
$att_name = $_FILES['att']['name'][$key];
$att_size = $_FILES['att']['size'][$key];
$att_type = $_FILES['att']['type'][$key];

$fp = fopen( $att_path, "rb");
$file = fread( $fp, $att_size );
fclose ($fp);

$num = md5(time());
$str = "==multipart_Boundary_x{$num}x";

$file = chunk_split(base64_encode($file));

$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: multipart/mixed;";
$headers .= "boundary=\"{$str}\"\r\n";
$headers .= "From: $email"; 

$msg .= "This is a multi-part message in MIME format\r\n\n";
$msg .= "--{$str}\r\n";
$msg .= "Content-Type: text/plain; charset=\"iso-8859-1\"\r\n";
$msg .= "Content-Transfer-Encoding: 8bit\r\n";
$msg .= "--{$str}\r\n";

$msg .= "Content-Type: {$att_type}; ";
$msg .= "name=\"{$att_name}\"\r\n";
$msg .= "Content-Disposition: attachment; ";
$msg .= "filename =\"{$att_name}\"\r\n";
$msg .= "Content-Transfer-Encoding: base64\r\n";
$msg .= "$file\r\n\n";
$msg .= "--{$str}";

$sent = mail($to, $subject, $msg, $headers) ; 
if($sent) 
{print "<p>Thank you.  Your attachment was sent successfully</p>"; }
else 
{print "<p>Sorry.  We encountered an error sending your mail</p>"; }

$msg = "";
$headers = "";
}
}
?>
Thanks for any help
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: trouble with encoding emails

Post by Christopher »

It looks like you are attaching each file to an entire email. You should create the email headers -- then loop and add attachments.
(#10850)
katierosy
Forum Commoner
Posts: 27
Joined: Wed Apr 07, 2010 8:39 am

Re: trouble with encoding emails

Post by katierosy »

You may use this function it works fine

<?php

function SendMail($emailaddress,$from,$fromaddress,$emailsubject="",$body="", $html = true,$attachment="",$encoding="utf-8"){

if (strtoupper(substr(PHP_OS,0,3)=='WIN')) {
$eol="";
} elseif (strtoupper(substr(PHP_OS,0,3)=='MAC')) {
$eol="\r";
} else {
$eol="\n";
}

if ($encoding != "utf-8" && !empty($body)) {
$body = mb_convert_encoding($body, $encoding, "utf-8");
}

$msg = "";

# Common Headers
$headers .= "From: ".$from." <".$fromaddress.">".$eol;
$headers .= "Reply-To: ".$from." <".$fromaddress.">".$eol;
$headers .= "Return-Path: ".$from." <".$fromaddress.">".$eol;
$headers .= "Message-ID: <".time()." serv@".$_SERVER['SERVER_NAME'].">".$eol;
$headers .= "X-Mailer: PHP v".phpversion().$eol;
$headers .= 'MIME-Version: 1.0'.$eol;

if (!empty($attachment)) {
//send multipart message
# Boundry for marking the split & Multitype Headers
$mime_boundary=md5(time());

$headers .= "Content-Type: multipart/related; boundary=\"".$mime_boundary."\"".$eol;

# File for Attachment

$f_name = $attachment;
$handle=fopen($f_name, 'rb');
$f_contents=fread($handle, filesize($f_name));
$f_contents=chunk_split(base64_encode($f_contents));
$f_type=filetype($f_name);
fclose($handle);

# Attachment
$msg .= "--".$mime_boundary.$eol;
$msg .= "Content-Type: application/jpeg; name=\"".$file."\"".$eol;
$msg .= "Content-Transfer-Encoding: base64".$eol;
$msg .= "Content-Disposition: attachment; filename=\"".basename($attachment)."\"".$eol.$eol; // !! This line needs TWO end of lines !! IMPORTANT !!
$msg .= $f_contents.$eol.$eol;
# Setup for text OR html
$msg .= "Content-Type: multipart/alternative".$eol;

$contentType = "text/plain";
if ($html) {
$contentType = "text/html";
}

# Body
$msg .= "--".$mime_boundary.$eol;
$msg .= "Content-Type: ".$contentType."; charset=\"".$encoding."\"".$eol;
$msg .= "Content-Transfer-Encoding: 8bit".$eol.$eol; // !! This line needs TWO end of lines !! IMPORTANT !!
$msg .= $body.$eol.$eol;

# Finished
$msg .= "--".$mime_boundary."--".$eol.$eol; // finish with two eol's for better security.
} else {
$headers .= "Content-Type: text/plain; charset=\"".$encoding."\"".$eol;
$headers .= "Content-Transfer-Encoding: 8bit".$eol.$eol; // !! This line needs TWO end of lines !! IMPORTANT !!
$msg .= $body.$eol.$eol;
}

ini_set(sendmail_from, $fromaddress); //needed to hopefully get by spam filters.
$success = mail($emailaddress, $emailsubject, $msg, $headers);
ini_restore(sendmail_from);

return $success;
}
?>
LogoVendor
Forum Newbie
Posts: 3
Joined: Wed May 19, 2010 6:12 am

Re: trouble with encoding emails

Post by LogoVendor »

use all the headers for the email and replace _REQUEST with _POST or _GET
Post Reply