我有一个问题,发送邮件与附件使用php。邮件是用坏的附件发送的,例如当我试图发送大小为2 Mb
的example.doc
时,在邮件中我收到大小为1Kb
的noname
我使用两个文件php.
第一个mailClass.php
包含:
<?php
class mail
{
function emailWithAttach($fromAdress,$toAdress,$mailSubject,$mailMessageHead,
$mailMessageMain,$mailMessageSign,$filePath,$fileName)
{
$fileatt_name = $fileName;
$fileatt = $filePath.$fileName;
$fileatt_type = "application/doc";
$email_from = $fromaddress;
$email_subject = $mailSubject;
$email_message = $mailMessageHead. "<br>";
$email_message .= $mailMessageMain. "<br>";
$email_message .= $mailMessageSign;
$email_to = $toAdress;
$headers = "From: " .$email_from;
$file = fopen ($fileatt."rb");
$data = fread ($file, filesize($fileatt));
fclose($file);
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$headers .= "'nMIME-Version: 1.0 'n".
"Content-Type : multipart/mixed:'n".
"boundary='"{$mime_boundary}'"";
$email_message .= "This is a multip-part message in MIME format. 'n'n".
"--{$mime_boundary}'n".
"Content-Type : text/html; charset='"iso-8859-1'"'n".
"Content-Transfer-Encoding: 7bit'n'n".
$email_message .= "'n'n";
$data = chunk_split(base64_encode($data));
$email_message .= "--{$mime_boundary}'n".
"Content-Type: {$fileatt_type}:'n".
"name='"{$fileatt_name}'"'n".
"Content-Transfer-Encoding: base64 'n'n".
$data .= "'n'n".
"--{$mime_boundary}--'n";
if(@mail($email_to,$email_subject,$email_message,$headers))
{
return true;
}
}
}
?>
第二个文件index.php
包含:
<?php
include "mailClass.php";
$testEmail = new mail;
$from = "sender@gmail.com";
$sendTo = "receiver@gmail.com";
$subject = "email with attachment";
$bodyHead = "welcome to the attachment email test";
$bodyMain = "hello iteb";
$bodyEnd = "Thank you";
$filePath = "";
$fileName = "example.doc";
if ($testEmail->emailWithAttach($from,$sendTo,$subject,$bodyHead,$bodyMain,$bodyEnd,$filePath,$fileName))
{
echo "Email Send successful!!";
}
else
{
echo "Email Send Failed";
}
?>
使用phpmailer发送邮件:Get it here
它还有一个优点,你可以发送很多邮件,而不需要每次打开和关闭连接,就像php的mail()函数一样。