Reputation: 21617
Trying to accomplish this with just PHP and I'm at a snag where the HTML in the email is fine but the boundary
sections appear as just normal text and the attachment wont attach.
$to = '[email protected]';
$subject = 'Contact Submission';
$name = strip_tags($_POST['name']);
$emailto = strip_tags($_POST['emailto']);
$comments = strip_tags($_POST['comments']);
$attachment = chunk_split(base64_encode(file_get_contents($_FILES['file']['tmp_name'])));
$filename = $_FILES['file']['name'];
$boundary =md5(date('r', time()));
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: noreply@emailcom' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$comments="This is a multi-part message in MIME format.
--_1_$boundary
Content-Type: multipart/alternative; boundary=\"_2_$boundary\"
--_2_$boundary
Content-Type: text/html; charset=ISO-8859-1\"
Content-Transfer-Encoding: 7bit
<table width=\"600px\" cellpadding=\"0\" cellspacing=\"0\"style=\"\">
<tr valign=\"top\">
<th align=\"left\" style=\" font-family:Verdana, Geneva, sans-serif; color:#000; font-size:14px; padding-left:10px; line-height:32px\">Contact Submission</th>
</tr>
</table>
<br />
<table width=\"600\" cellpadding=\"0\" cellspacing=\"0\"style=\"border: 1px solid #000;\">
<tr valign=\"top\">
<td colspan=\"4\" style=\" font-family:Verdana, Geneva, sans-serif; color:#000; font-size:12px; padding-left:10px; border-bottom: 1px solid #000;background:#ececec; line-height:32px;\" align=\"left\"><strong>Contact Information</strong></td>
</tr>
<tr valign=\"top\">
<td style=\" font-family:Verdana, Geneva, sans-serif; color:#000; font-size:12px; padding-left:10px; line-height:32px; font-weight:bold\" align=\"right\" width=\"120\">Name</td>
<td style=\" font-family:Verdana, Geneva, sans-serif; color:#000; font-size:12px; padding-left:10px; line-height:32px;padding-left:40px;\">$name</td>
</tr>
<tr valign=\"top\">
<td style=\" font-family:Verdana, Geneva, sans-serif; color:#000; font-size:12px; padding-left:10px; line-height:32px; font-weight:bold; background:#e6edf2;\" align=\"right\" width=\"120\">Email</td>
<td style=\" font-family:Verdana, Geneva, sans-serif; color:#000; font-size:12px; padding-left:10px; line-height:32px;background:#e6edf2;padding-left:40px;\">$emailto</td>
</tr>
<tr valign=\"top\">
<td style=\" font-family:Verdana, Geneva, sans-serif; color:#000; font-size:12px; padding-left:10px; line-height:32px; font-weight:bold\" align=\"right\">Comments</td>
<td style=\" font-family:Verdana, Geneva, sans-serif; color:#000; font-size:12px; padding-left:10px; line-height:32px;padding-left:40px;\" colspan=\"3\">$comments</td>
</tr>
</table>
--_2_$boundary--
--_1_$boundary
Content-Type: application/octet-stream; name=\"$filename\"
Content-Transfer-Encoding: base64
Content-Disposition: attachment
$attachment
--_1_$boundary--";
mail($to, $subject, $comments, $headers);
Any help would be great from anyone as to what needs changing/added/removed to get this email to keep my HTML table and also attach the file
Upvotes: 1
Views: 235
Reputation: 455
$to = "email";
$subject = "your subject";
$base = basename($file1);
$file = fopen('file path','rb');
$size = filesize('file path');
$data = fread($file,$size);
fclose($file);
$data = chunk_split(base64_encode($data));
$message="<html><body>";
$message="<table border='1'>";
$message.="<tr><td colspan='3' align='center' style='color:#FFFFFF;font-size:large; background:#000000'>Your Information</td></tr>";
$message.="<tr><td style='font-weight:bold'>field 1</td><td style='font-weight:bold'>field 2</td></tr>";
$message.="</table>";
$message.="</body></html>";
//boundary
$div = "==Multipart_Boundary_x".md5(time())."x";
//headers
$head = "From: $email\n".
"MIME-Version: 1.0\n".
"Content-Type: multipart/mixed;\n".
" boundary=\"$div\"";
//message
$mess = "--$div\n".
"Content-Type: text/html; charset=\"iso-8859-1\"\n".
"Content-Transfer-Encoding: 7bit\n\n".
"$message\n\n".
"--$div\n".
"Content-Type: application/octet-stream; name=\"$base\"\n".
"Content-Description: $base\n".
"Content-Disposition: attachment;\n".
" filename=\"$base\"; size=$size;\n".
"Content-Transfer-Encoding: base64\n\n".
"$data\n\n".
"--$div\n";
$return = "-f$email";
mail($to,$subject,$mess,$head,$return);
try this code
Upvotes: 2