如何使用PHP发送带有pdf附件的电子邮件?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了如何使用PHP发送带有pdf附件的电子邮件?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2527字,纯文字阅读大概需要4分钟。
内容图文
![如何使用PHP发送带有pdf附件的电子邮件?](/upload/InfoBanner/zyjiaocheng/710/21d0d74a5d5e4b01a8e4efaae825843b.jpg)
嗨,我正在尝试发送带有doc,docx和pdf附件的邮件.我收到了doc和docx的电子邮件.但是当我尝试用pdf附件发送邮件时,它无法正常工作.因此,我收到错误消息’上传的文件不支持文件类型’.我找不到错误.谁能帮我?我的代码:
<?php
$to = $_POST["txtTo"];
$subject = $_POST["txtSubject"];
$from = $_POST["txtFormEmail"];
$message = $_POST["txtDescription"];
$random_hash = md5(date('r', time()));
$headers .= "From: ".$from."<".$from.">\nReply-To: ".$_POST["txtFormEmail"]."";
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";
$strFilesName = $_FILES["fileAttach"]["name"];
$attachment = chunk_split(base64_encode(file_get_contents($_FILES["fileAttach"]["tmp_name"])));
ob_start();
//Turn on output buffering
?> --PHP-mixed-<?php echo $random_hash; ?> Content-Type: multipart/alternative; boundary="PHP-alt-<?php echo $random_hash; ?>" --PHP-alt-<?php echo $random_hash; ?> Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit --PHP-alt-<?php echo $random_hash; ?> Content-Type: text/html; charset="iso-8859-1" Content-Transfer-Encoding: 7bit <?php echo $message; ?> --PHP-alt-<?php echo $random_hash; ?>-- --PHP-mixed-<?php echo $random_hash; ?> Content-Type: <?php echo $_FILES["fileAttach"]["type"]; ?>; name="<?php echo $strFilesName; ?>" Content-Transfer-Encoding: base64 Content-Disposition: attachment <?php echo $attachment; ?> --PHP-mixed-<?php echo $random_hash; ?>-- <?php
$message = ob_get_clean();
if ($_FILES["fileAttach"]["type"] == "application/pdf"|| $_FILES["fileAttach"]["type"]=="application/msword"||$_FILES["fileAttach"]["type"]==application/vnd.openxmlformats-officedocument.wordprocessingml.document")if($_FILES["fileAttach"]["size"] < 1024000){if ($_FILES["fileAttach"]["error"] > 0){ echo "Error:
" . $_FILES["fileAttach"]["error"]."<br />";}else{$mail_sent = @mail( $to, $subject, $message, $headers );
}
echo $mail_sent ? "Mail sent" : "Mail failed";}else{echo ' File Size should be with in 1000 KB';} }else{echo 'The uploaded file is not supported file type.'; }?>
解决方法:
开始使用Swiftmailer,您的生活将更轻松.
使用示例:
require_once('swift/lib/swift_required.php');
$transport = Swift_MailTransport::newInstance();
$mailer = Swift_Mailer::newInstance($transport);
$message = Swift_Message::newInstance()
->setFrom(array($from))
->setTo(array($to))
->setEncoder(Swift_Encoding::get7BitEncoding())
->setSubject($subject)
->setBody($body, 'text/html')
->addPart(strip_tags($body), 'text/plain')
->attach(Swift_Attachment::fromPath($filename))
;
$mailer->send($message);
内容总结
以上是互联网集市为您收集整理的如何使用PHP发送带有pdf附件的电子邮件?全部内容,希望文章能够帮你解决如何使用PHP发送带有pdf附件的电子邮件?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。