I try to send a table html with php to my email but I need to send it as attached not as email body. I try this solution but doesn't work. Can someone help me?
THIS IS MY FUNCTION
<script>
function sendMail(){
//this is the table I want to send not as email body but as attachmment so I send this string in post with ajax call
var datatosend = '<table border="1"><tr><th>Month</th><th>Savings</th></tr><tr><td>January</td><td>$100</td></<tr><td>February</td><td>$100</td></tr><tr><td colspan="2">Sum: $180</td></tr></table>'
//I read the mail insered into the input to send the mail to the right mail
var mailtosend = $('#sendpage #inputMailToSend').val();
//I link the php script that recive the data from ajax call post
var url = 'SendMailAtt.php';
var ogg = 'Paolo_Carlo_Giovanni_Marco_Luca';
$.ajax({
type: "POST",
url: url,
data: "corpo=" + datatosend + "&dest=" + mailtosend + "&ogg=" + ogg,
dataType: "html",
success: function(msg){
alert('ok');
},
error: function(){
alert("ko");
}
});
AND THIS IS THE PHP SCRIPT
<?php
$fp = fopen('log.txt', 'w');
$destinatario = $_POST['dest'];
$mittente = "miamail@gmail.com";
$oggetto = $_POST['ogg'];
$messaggio = $_POST['corpo'];
$allegato_name = date("d-m-Y").".html";
$headers = "From: " . $mittente . "\r\n";
$headers .= "Reply-To: " . $mittente . "\r\n";
$msg = "";
fwrite($fp, "log1\n");
$data = $messaggio;
//base64_encode
$data = chunk_split(base64_encode($data));
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
fwrite($fp, "log2\n");
// Aggiungo le intestazioni necessarie per l'allegato
$headers .= "\nMIME-Version: 1.0\n";
$headers .= "Content-Type: multipart/mixed;\n";
$headers .= " boundary=\"{$mime_boundary}\"";
$msg .= "--{$mime_boundary}\n";
//text message
$msg .= "Content-Type: multipart/mixed; charset=\"iso-8859-1\"\n";
$msg .= "Content-Transfer-Encoding: 7bit\n\n";
$msg .= $messaggio . "\n\n";
$msg .= "--{$mime_boundary}\n";
// add the attachment
$msg .= "Content-Disposition: attachment;\n";
$msg .= " filename=\"{$allegato_name}\"\n";
$msg .= "Content-Transfer-Encoding: base64\n\n";
$msg .= $data . "\n\n";
$msg .= "--{$mime_boundary}--\n";
fwrite($fp, "log3\n");
$errorMail = "";
// Invio la mail
if ($errorMail = mail($destinatario, $oggetto, $msg, $headers)){
fwrite($fp, "log IF\n");
}else{
fwrite($fp, "log ELSE\n");
fwrite($fp, "HEADERS: ".$headers."\n");
fwrite($fp, "MSG: ".$msg."\n");
fwrite($fp, "OGGETTO: ".$oggetto."\n");
fwrite($fp, "DESTINATARIO: ".$destinatario."\n");
fwrite($fp, "ERROR: ".$errorMail ."\n");
}
fwrite($fp, "log4\n");
fclose($fp);
?>
注意最后的输出,边界之间可能没有足够的\\ n。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.