用附件php发送邮件

时间:2018-05-01 22:54:34

标签: php phpmailer

我的代码无效

当电子邮件到达时,文件内容为base64,格式不正确。

如何解决这个问题?

我的代码

$headers = 'From: ' .     "$from\r\n" . 'Reply-To: ' . "$from\r\n";


$file = $arquivo['tmp_name'];
$originalFile = $arquivo['name'];
$file_size = filesize($file);
$handle = fopen($file, "rb");
$anexo = fread($handle, $file_size);
fclose($handle);
$anexo = chunk_split(base64_encode($anexo));

// a random hash será necessário para separar conteúdos diversos a serem enviados
$separator = md5(time());

// Define o retorno de carro a ser utilizado
$eol = PHP_EOL;

// main header (multipart mandatory)
$headers = "From: $from" . $eol;
$headers .= "MIME-Version: 1.0" . $eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"" . $separator . "\"" . $eol . $eol;


// message
$msg .= "--" . $separator . $eol;
$msg .= "Content-Type: text/html; charset=\"utf-8\"" . $eol;
$msg .= "Content-Transfer-Encoding: 8bit" . $eol . $eol;
$msg .= $mensagemHTML . $eol . $eol;

// attachment
$msg .= "--" . $separator . $eol;
$msg .= "Content-Type: ".$arquivo["type"]."; name=".$arquivo['name']." \n" . $eol;
$msg .= "Content-Transfer-Encoding: base64" . $eol;
$msg .= "Content-Disposition: attachment; filename=".$arquivo['name']." " . $eol . $eol;
$msg .= $anexo . $eol . $eol;
$msg .= "--" . $separator . "--";


$envio = mail($emaildestinatario, $assunto, $msg, $headers); 

这个附件

enter image description here

enter image description here

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

0 个答案:

没有答案