c_pattle Posted June 1, 2010 Share Posted June 1, 2010 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. $to = "[email protected]"; $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 Link to comment https://forums.phpfreaks.com/topic/203500-trouble-with-encoding/ Share on other sites More sharing options...
katierosy Posted June 2, 2010 Share Posted June 2, 2010 You may use this function. It works already tested. <?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; } ?> Link to comment https://forums.phpfreaks.com/topic/203500-trouble-with-encoding/#findComment-1066656 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.