【问题标题】:Phpmailer: multiple dynamic attachments in one formPhpmailer:一种形式的多个动态附件
【发布时间】:2015-11-29 19:48:35
【问题描述】:

我正在创建一个邮件列表服务。 我需要动态地将一个或多个文件附加到电子邮件中,使用 php、phpmailer 和仅一个“槽”用于在 html 表单中添加文件。 我只知道如何在同一封电子邮件中发送一个附件,而不是两个或更多。

这里是 html 代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
</head>
    <body>
        <div class="main_container">

            <form action="phpmailer/sendmail.php" method="post" enctype="multipart/form-data" id="prenota">

                    <div class="form_title">
                        <h2>Mailing List</h2>
                    </div>

                    <div class="form_title">
                        <p>Message:</p>
                    </div>

                    <label for="message">
                        <textarea name="message"></textarea>
                    </label>

                    <label for="file">
                        Select one or more file to send
                        <input name="file" type="file" id="file_ok">
                    </label>

                    <div class="submit-container">
                        <input class="submit-button" type="submit" name="submit" value="Invia"><br>
                    </div>

                </form>

        </div>
    </body>
</html>

和 phpmailer 的 php(sendmail.php,它适用于我正在使用的托管服务):

<?php 
require_once('libs/PHPMailer/PHPMailerAutoload.php');


$db= new PDO('mysql:host=00.000.000.000;dbname=dbname_1', 'dbname', 'dbpass');//connection setup

$m_list = $db->query("
    SELECT id, nome, email
    FROM mailing_list
")->fetchAll(PDO::FETCH_ASSOC);


$mail             = new PHPMailer(true);

$mail->Port = 25;
$mail->CharSet = 'UTF-8';


//variables
$email_receiver =  $m_list['email'];  
$name_receiver = $m_list['nome'];

$nome_sender= "mr x"; 
$messagge= stripslashes($_POST['message']);

foreach ($m_list as $eachmail) {

    $email_receiver =  $eachmail['email'];  
    $name_receiver = $eachmail['nome'];

    $mail->From = "service@email.it";   

    $mail->FromName = "service";            

    $mail->AddReplyTo($email_receiver, $name_receiver);

    $mail->MsgHTML($body);

    $mail->AddAddress($email_receiver, "dynamic name");  

    $mail->Subject    = "Message from website";   

    $body             = '<strong>email send to:</strong> ' . $name_receiver . '<br/>
                    <strong>email:</strong> ' . $email_receiver . '<br/>
                    <strong>message:</strong> ' . stripslashes($message) . '<br/><br/>';

    $mail->MsgHTML($body);
    $mail->Send(true);
    $mail->ClearAllRecipients(true);

}

echo "mail sent";

?>

我想我需要将文件存储到一个数组中,然后为每个文件创建相关附件。不幸的是我不能。有什么帮助吗?

【问题讨论】:

  • 您的代码基于 PHPMailer 提供的 mailing list examplesend upload example 的组合。本质上,您只需在处理上传后为每个文件调用addAttachment
  • 谢谢,我按照你的建议做了,也发布了答案!
  • phpmailer 不在乎你正在做多少/几个附件。您只需致电-&gt;AddAttachment(),直到您完成添加。如果你根本不调用它,那也没关系。

标签: php html forms phpmailer


【解决方案1】:

首先,我使用了 Jquery 的“注入”,以便在主窗体的 dom 中添加尽可能多的输入文件,并在需要时删除它们。

$( document ).ready(function() {
    $( '#add_more' ).click(function() {
        var current_count = $('input[type="file"]').length;
        var next_count = current_count +1;
        $('#prenota').prepend('<p id="here_' + next_count + '"><input type="file" name="file_' + next_count + '" /></p>')
        $('#here_' + next_count + '').append('<a class="delete_this" href="#">delete</a>');
    });


    $('.delete_this').live('click', function(){
        var current_todelete = $(this).closest("p").attr("id"); 
        $('#' + current_todelete + '').remove();
    });
});

然后我在 php 文件中添加了一些字符串,指定用于收集帖子信息和发送电子邮件,使其能够携带附件(正如 Synchro 建议我的那样):

if (isset($_FILES)){
    foreach ($_FILES as $file) {

        $filename = $file['name'];
        $filetype = $file['type'];
        $filetmp_name = $file['tmp_name'];
        $filesize = $file['size'];

        if($filename){
            $temPath = '../../../../../public/' . basename($filename);

            //var_dump($temPath);
            //die();

            if (move_uploaded_file($filetmp_name, $temPath)) {
                $mail->AddAttachment($temPath, $filename);
            }
        }       
    }
}else{
    $mail->AddAttachment(false);
}

希望对遇到我同样问题的人有所帮助。

【讨论】:

  • 很高兴它成功了,但这种文件处理方式并不安全 - 按照发送上传示例使用的方式进行 - 首先调用 move_uploaded_file,并且不要信任提交的文件名。
  • Synchro,我按照您的建议修改了答案中的代码,现在是否正确?如果我使用链接示例中显示的 "tempnam(sys_get_temp_dir(), sha1($_FILES['userfile']['name']))" 行脚本,则无法发送有效文件。文件已正确发送,但已损坏。为什么需要在 move_uploaded_file 方法中处理文件?为了防止我的邮件列表被视为垃圾邮件?
  • 所做的只是生成一个安全、唯一的临时文件名。它不发送任何内容或移动任何文件。下一行是重要的。如果您想知道为什么需要这样做,请阅读也从该示例链接的 PHP 文件上传文档。
猜你喜欢
  • 1970-01-01
  • 2015-12-02
  • 2015-11-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-13
  • 2015-12-06
  • 1970-01-01
相关资源
最近更新 更多