【发布时间】: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 example 和 send upload example 的组合。本质上,您只需在处理上传后为每个文件调用
addAttachment。 -
谢谢,我按照你的建议做了,也发布了答案!
-
phpmailer 不在乎你正在做多少/几个附件。您只需致电
->AddAttachment(),直到您完成添加。如果你根本不调用它,那也没关系。