如何用PHP附件发送电子邮件? [英] How to send email with pdf attachment using php?

查看:137
本文介绍了如何用PHP附件发送电子邮件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我正在尝试发送带有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。> \\\
Reply-To:$ _ POST [txtFormEmail]。
$ headers。=\r\\\
Content-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();
//打开输出缓冲
?> --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-1Content-Transfer-Encoding:7bit --PHP-alt-<?php echo $ random_hash; ?> Content-Type:text / html; charset =iso-8859-1Content-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){echoError:
。$ _FILES [fileAttach] [error]。< br />;} else {$ mail_sent = @mail($ to,$ subject,$ message,$ headers);
}

echo $ mail_sent?Mail sent:Mail failed;} else {echo'File Size should with with in 1000 KB';}} else {echo'上传的文件不支持文件类型';}?>
Swiftmailer

/ a>,你的生活会更容易。



使用示例:

  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);


Hi i am trying to send a mail with doc, docx and pdf attachment. i am getting email with doc and docx. but when i try to send mail with pdf attachment is it not working. Thus i am getting the error message 'The uploaded file is not supported file type'. I cannt find the error. Can anyone help me? My Code:

<?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.'; }?>

解决方案

Start using Swiftmailer, your life will be easier.

Use example :

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附件发送电子邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆