我正在创建邮件列表服务。 我需要动态地将一个或多个文件附加到电子邮件中,使用php,phpmailer和只有一个" slot"用于以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";
?>
我想我需要将文件存储在一个数组中,然后为每个文件创建相对附件。不幸的是我无法做到。有什么帮助吗?
答案 0 :(得分:0)
首先,我使用了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文件中添加了一些字符串来收集帖子信息和sendig电子邮件,使其能够携带附件(正如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);
}
希望对有同样问题的人有所帮助。