基本上,我试图通过PHPMail发送PDF。 电子邮件已发送,我完全接收到了Outlook。问题是附件被破坏而且没有打开。我甚至尝试发送HTML并且也是空的。
我尝试在论坛中进行研究,尝试了几个“工作代码”,其他人也使用了这段代码...我不知道为什么不能为我工作。 我正在使用最新版本的PHPMail 5.2.2
$mail = new PHPMailer();
$staffEmail = "staffemail";
$mail->From = $staffEmail;
$mail->FromName = "name";
$mail->AddAddress('my@email.com');
$mail->AddReplyTo($staffEmail, "name");
$mail->AddAttachment('test.pdf');
$mail->Subject = "PDF file attachment";
$mail->Body = "message!";
$mail->Send();
答案 0 :(得分:7)
这个自定义PHP函数将向(开始)PHP开发人员展示如何构建带附件功能的电子邮件脚本。请注意,邮件功能内部没有可用的验证功能。
<?php
function mail_attachment($filename, $path, $mailto, $from_mail, $from_name, $replyto, $subject, $message) {
$file = $path.$filename;
$file_size = filesize($file);
$handle = fopen($file, "r");
$content = fread($handle, $file_size);
fclose($handle);
$content = chunk_split(base64_encode($content));
$uid = md5(uniqid(time()));
$name = basename($file);
$header = "From: ".$from_name." <".$from_mail.">\r\n";
$header .= "Reply-To: ".$replyto."\r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
$header .= "This is a multi-part message in MIME format.\r\n";
$header .= "--".$uid."\r\n";
$header .= "Content-type:text/plain; charset=iso-8859-1\r\n";
$header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$header .= $message."\r\n\r\n";
$header .= "--".$uid."\r\n";
$header .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n"; // use different content types here
$header .= "Content-Transfer-Encoding: base64\r\n";
$header .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
$header .= $content."\r\n\r\n";
$header .= "--".$uid."--";
if (mail($mailto, $subject, "", $header)) {
echo "mail send ... OK"; // or use booleans here
} else {
echo "mail send ... ERROR!";
}
}
?>
接下来,我们将展示如何使用此函数发送带有一个附加zip文件的电子邮件的示例:
<?php $my_file = "somefile.zip";
$my_path = $_SERVER['DOCUMENT_ROOT']."/your_path_here/";
$my_name = "Olaf Lederer";
$my_mail = "my@mail.com";
$my_replyto = "my_reply_to@mail.net";
$my_subject = "This is a mail with attachment.";
$my_message = "Hallo,\r\ndo you like this script? I hope it will help.\r\n\r\ngr. Olaf";
mail_attachment($my_file, $my_path, "recipient@mail.org", $my_mail, $my_name, $my_replyto, $my_subject, $my_message);
?>
您是否在寻找发送多个附件的脚本?试试我们的邮件附件类脚本。
如果您想通过SMTP和Gmail发送网站邮件,请查看我们的PHPMailer教程。
答案 1 :(得分:2)
我建议您将文件上传到服务器,然后将其附加到电子邮件中。例如,您可以使用以下步骤:
试试这个,因为我认为这就是你的问题的原因。
祝你好运答案 2 :(得分:1)
可能有两个问题:路径不正确附件或php没有该文件夹的权限
答案 3 :(得分:0)
这个正在为我努力发送简历作为附件在zend
$mail = new Zend_Mail ();
$mail->setBodyHTML ( stripslashes ($message) );
// add attachment
$fileContents = file_get_contents($attachemnet);
$resume = $mail->createAttachment($fileContents);
$resume->filename = $EmployeeDeatils['resume'];
//$mail->createAttachment($attachemnet);
$mail->setFrom ( $mail_template ['from_email'], $mail_template ['from_caption'] );
$mail->addTo ( $clientemail, $employee_name );
$mail->setSubject ($subject );
try {
$mail->send ();
} catch ( Exception $e ) {
$this->_helper->errorlog ( " Send mail to member with activation link : " . $e->getMessage () );
}
答案 4 :(得分:0)
您可以尝试使用此功能,该功能使用PHP的简单邮件功能:
function mail_attachment ($from , $to, $subject, $message, $attachment){
$fileatt = $attachment; // Path to the file
$fileatt_type = 'application/octet-stream'; // File Type
$start= strrpos($attachment, '/') == -1 ? strrpos($attachment, '//') : strrpos($attachment, '/')+1;
$fileatt_name = substr($attachment, $start, strlen($attachment)); // Filename that will be used for the file as the attachment
$email_from = $from; // Who the email is from
$email_subject = $subject; // The Subject of the email
$email_txt = $message; // Message that the email has in it
$email_to = $to; // Who the email is to
$headers = "From: ".$email_from;
$file = fopen($fileatt,'rb');
$data = fread($file,filesize($fileatt));
fclose($file);
$msg_txt=""; //\n\nMail created from websiteaddress systems : http://websiteaddress.com
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$headers .= "\nwebsiteaddress-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";
$email_txt .= $msg_txt;
$email_message = "This is a multi-part message in websiteaddress format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type:text/html; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n".$email_txt. "\n\n";
$data = chunk_split(base64_encode($data));
$email_message .= "--{$mime_boundary}\n" .
"Content-Type: {$fileatt_type};\n" .
" name=\"{$fileatt_name}\"\n" .
//"Content-Disposition: attachment;\n" .
//" filename=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mime_boundary}--\n";
$ok = @mail($email_to, $email_subject, $email_message, $headers);
if($ok) {
//echo "Attachment has been mailed !";
//header("Location: index.php");
} else {
die("Sorry but the email could not be sent. Please go back and try again!");
}
}
希望这会对你有所帮助。