【问题标题】:Adding attachment to email php mysql将附件添加到电子邮件 php mysql
【发布时间】:2012-03-07 21:49:44
【问题描述】:

大家好,我有一个时事通讯脚本,它将管理员在文本区域中放置的任何内容发送到数据库中的所有电子邮件,现在我希望该用户也能够将 .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>

【问题讨论】:

  • 免去你的痛苦,停止使用非常基本的(动力不足且速度慢)的mail()函数
  • 我觉得还可以吗?有什么不好的..?这可能吗
  • 可能,但该功能的“功能”非常有限,除了发送非常基本的电子邮件,没有人将其用于任何事情

标签: php mysql email attachment


【解决方案1】:

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

【讨论】:

  • 用php中的普通函数是不可能的吗?
【解决方案2】:

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

【讨论】:

  • 这不能只用邮件吗?
【解决方案3】:

你可以试试这个

    <?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.0\n" .
                       "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.\n\n" .
                      "--{$mime_boundary}\n" .
                      "Content-Type: text/html; charset=\"iso-8859-1\"\n" .
                      "Content-Transfer-Encoding: 7bit\n\n" .
                      $my_message . "\n\n";


                //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: base64\n\n" .
                          $data;
            }

            $final_message .= "--{$mime_boundary}--\n";

        $emailto = 'myeamil@test.com';

        mail($emailto, $subject, $final_message, $headers);


?>

我很久以前使用此代码附加附件。我不确定它是否还在工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-10-06
    • 2010-12-07
    • 2012-12-14
    • 1970-01-01
    • 1970-01-01
    • 2014-06-25
    相关资源
    最近更新 更多