【问题标题】:PHPMailer send email with arrayPHPMailer 使用数组发送电子邮件
【发布时间】:2014-04-25 12:21:27
【问题描述】:

您好,我对 HTML 表单数组和 PHP 有疑问。例如,我有姓名和电子邮件,但问了 6 次,我想发送这封邮件。我应该编辑什么才能工作?谢谢!

HTML:

   <form  method="Post" action="send.php" onSubmit="return validate();">
    <?php
     $amount=6; //amount shows the number of data I want to repeat
     for( $i = 0; $i < $amount; $i++ ) {
        ?>
        <b>Data <?php echo $i+1 ?>º</b>
          <input type="edit" name="Name[]" size="40">
          <input type="edit" name="email[]" size="40">
          <br>
    <?php } ?>
          <input type="submit" name="Submit" value="send 6 data mail">
   </form>

send.php:

<?php
       require('phpmailer/class.phpmailer.php');
       $name= $_POST['name[]']; 
       $email= $_POST['email[]']; 
       $mail = new PHPMailer();
       ...
       $mail->Body = ' Data<br> '
    <?php
     $amount=6; //amount shows the number of data I want to repeat
     for( $i = 0; $i < $amount; $i++ ) {
        ?> '
       name: '.$name[$i].' email: '.$email[$i];
       ...
       $mail->Send();
?>

应该发送:

Data
name: nameinput1 email: emailinput1
name: nameinput2 email: emailinput2
name: nameinput3 email: emailinput3
name: nameinput4 email: emailinput4
name: nameinput5 email: emailinput5
name: nameinput6 email: emailinput6

【问题讨论】:

  • POST vars 没有括号。因此,不要将$_POST['name[]'] 称为$_POST['name']
  • 您可以简单地数其中一个并将其分配给$amount。你可以使用$_POST["name"]。请参阅我的答案以获取更多详细信息

标签: php html-email


【解决方案1】:

您可以使用以下;

 <?php
    require('phpmailer/class.phpmailer.php');
    $name= $_POST['name']; 
    $email= $_POST['email'];

    $mail = new PHPMailer();
    ...
    $mail->Body = ' Data<br> ';
    $amount=count($name); //amount shows the number of data I want to repeat
    for( $i = 0; $i < $amount; $i++ ) {
        $mail->Body .= 'name: '.$name[$i].' email: '.$email[$i];
    } 
    ...
    $mail->Send();
?>

【讨论】:

  • no 不要给出任何错误 junt 保持空白(如果我只做一项工作)
  • 您的表单名称在前端是Name,您能否更新一下并尝试一下?而且你在 for 循环上缺少括号。查看我的更新答案
  • 我使用你的更新保持不变:\ 我插入 $mail->SMTPDebug = 2;并且不显示错误
  • $mail-&gt;Send(); 的结果是什么?您需要提供更多详细信息。一定有一些错误
  • $mail = new PHPMailer(); $mail->SMTPDebug = 2; $mail->IsSMTP();
【解决方案2】:

你可以试试下面的:

$mail->Body = ' Data<br> ';
 $amount=6; //amount shows the number of data I want to repeat
 for( $i = 0; $i < $amount; $i++ ) {
   $mail->Body .= 'name: '.$name[$i].' email: '.$email[$i]."<br>";
 }
   ...
$mail->Send();
?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多