phpmailer多个收件人错误代码?

时间:2017-05-29 13:51:30

标签: javascript php html forms phpmailer

我一直在尝试向多个收件人发送电子邮件,这是我目前为止的代码。电子邮件工作,但只适用于我手动设置的电子邮件,而不是通过表单(添加电子邮件)

有人可以帮我吗?
提前谢谢!

<table>
               <fieldset id="" width= "600" class="w3-main"> <b>registratie bij afwijking</b></fieldset>
               <tr><td width="100"><b>Controleur:</b></td><td><input type = "text" name="controleur" class="w3-card-2 w3-row" value="<?php echo $_SESSION["username"]; ?>" readonly></td></tr>
               <tr>
                    <td>onderwerp</td><td><input type= "text" name="onderwerp"></td>
               </tr>
                <tr>
                    <td>betreft/object</td><td><textarea name="message"></textarea></td>
                </tr>
                <tr>
                    <td><button type="button" onclick="emailNext();">nog een ontvanger.</button>
                    <div id="addEmail"></div></td>
                </tr>
                <tr>
                    <td><input type="submit" name="emails" value="email verzenden"></td>
                </tr
               </table>
   </form>
  <script>
function emailNext() {
var nextEmail, inside_where;
nextEmail = document.createElement('input');
nextEmail.type = 'text';
nextEmail.name = 'emails[]';
nextEmail.className = 'class_for_styling';
nextEmail.style.display = 'block';
nextEmail.placeholder  = 'Enter E-mail Here';
inside_where = document.getElementById('addEmail');
inside_where.appendChild(nextEmail);
return false;
}   
</script>              
<?php
if(isset($_POST['emails'])) {
foreach ($_POST[emails] AS $postEmail){
    if ($postEmail){$mail->AddAddress($postEmail);}
}
$msg = $_POST['message'];
$controle = $_POST['controleur'];
$subj = $_POST['onderwerp'];

require 'phpMailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 0;
$mail->Debugoutput = 'html';
$mail->Host = "******";
$mail->SMTPSecure = "TSL";
$mail->SMTPAutoTLS = false;
$mail->Port       = 25;
$mail->SMTPAuth = false;
$mail->setFrom('*****', ($controle));
$mail->addAddress('****', 'Test mail');
$mail->Subject = ($subj);
$mail->Body = ($msg);

if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
 }     else {
echo "Message sent!";
}
 }

2 个答案:

答案 0 :(得分:1)

这是因为你在添加地址后覆盖数组。

从:

开始
require 'phpMailer/PHPMailerAutoload.php';
$mail = new PHPMailer;

结束然后添加地址。

答案 1 :(得分:-1)

嗯......虽然它们的文档中没有指定,但我很确定你可以使用一个电话$mail->AddAddress()以逗号分隔的多个电子邮件。

所以你可以做$mail->AddAddress(implode(', ', $_POST['emails']));

当然,必须在行<{1}} 之后完成此操作