向电子邮件php-mysql添加附件



大家好,我有一个时事通讯脚本,它会将管理员在文本区域中放入的任何内容发送到数据库中的所有电子邮件,现在我希望用户也能够将.pdf文件上传到该电子邮件,如有帮助,不胜感激!这是实际发送电子邮件的代码:

Plain是包含消息的文本区域主题就是主题需要在表单上添加一个上传输入,以验证此脚本

<?php
include "connect.php";
$subject = stripslashes($_POST["subject"]);
$plain = stripslashes($_POST["plain"]);
$result = mysql_query("SELECT email FROM member");
$emails = array();
while ($row = mysql_fetch_row($result))
$emails[] = $row[0];
$subject = $_POST['subject'];
$from = "noreply@wgtfgb.com";
$headers = "From:" . $from;
$to = implode(", ", $emails);
mail($to, $_POST['subject'], $_POST["plain"], $headers);
?>
<link rel="stylesheet" type="text/css" href="view.css" media="all">
<script type="text/javascript" src="view.js"></script>
<form id='register' action='updateprofile.php' method='post' accept-charset='UTF-8'>
<body id="main_body" >
    <img id="top" src="top.png" alt="">
<div id="form_container">
<h1>Newsletter Sent</h1>
    <form id="form_362567" class="appnitro"  method="post" action="">
                <div class="form_description">
        <h2>&nbsp;Newsletter Sent</h2>
        <p></p>
    </div>                      
        <ul >
                <li class="section_break">
        <p></p>
    </li>       <li id="li_2" >
    <label class="description" for="email">
    <?php
echo "Newsletter successfully sent, you will be redirected back to the member area in     5     seconds.";
?>
</form>
        <div id="footer">
<meta http-equiv="refresh" content="5; URL=index.php">
    </div>
</div>
<img id="bottom" src="bottom.png" alt="">
</body>

使用类似SwiftMailer的库(http://swiftmailer.org/docs/messages.html)。。。更容易添加附件。

PEAR_mail是一个简单且受良好支持的PHP内置邮件函数的替代品。它支持mime编码和附件,并为您试图实现的目标提供了出色的文档。

你可以试试这个

    <?php
            $fp = fopen('myfile.pdf','rb'); 
            $dt = fread($fp,filesize('myfile.pdf'));
            $attachments[] = Array(
               'data' => $dt,
               'name' => $name,
               'type' => 'application/pdf'
            ); 
              //Generate a boundary string
            $semi_rand = md5(time());
            $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
            $emailcontent="<style type='text/css'>
                        body {
                            font-family: arial, Geneva, sans-serif;
                            font-size: 12px;
                        }
                        </style>";

            $subject = 'Subject';
            $my_message = 'My message';
            $headers = "MIME-Version: 1.0n" .
                       "From: {$from}n" .
                        "Content-Type: multipart/mixed;n" .
                       " boundary="{$mime_boundary}"";

                //Add a multipart boundary above the plain message

            $final_message = "This is a multi-part message in MIME format.nn" .
                      "--{$mime_boundary}n" .
                      "Content-Type: text/html; charset="iso-8859-1"n" .
                      "Content-Transfer-Encoding: 7bitnn" .
                      $my_message . "nn";

                //Add sttachments
            foreach($attachments as $attachment){
               $data = chunk_split(base64_encode($attachment['data']));
               $name = $attachment['name'];
               $type = $attachment['type'];
               $final_message .= "--{$mime_boundary}n" .
                          "Content-Type: {$type};n" .
                          " name="{$name}"n" .              
                          "Content-Transfer-Encoding: base64nn" .
                          $data;
            }
            $final_message .= "--{$mime_boundary}--n";
        $emailto = 'myeamil@test.com';
        mail($emailto, $subject, $final_message, $headers);

?>

我很久以前就用这个代码来附加附件。我不确定它是否仍然有效。

相关内容

  • 没有找到相关文章

最新更新