【问题标题】:Send multiple email address using phpmailer function使用 phpmailer 功能发送多个电子邮件地址
【发布时间】:2021-09-13 06:18:31
【问题描述】:

我在发送多个电子邮件地址功能时遇到问题。如果我发送一个电子邮件地址,我可以工作。如果我向收件人发送多封电子邮件,他们将无法收到我的消息。我正在使用 PHPMailer 函数来使用 XAMPP 执行电子邮件功能。我正在使用PHP数组函数将接收者的地址放入数组中,并使用while函数循环接收者地址发送。

下面是我的代码:

$address = array('st9overfindsolution@gmail','st7overfindsolution@gmail');
require 'class/class.phpmailer.php';
$mail = new PHPMailer(true);
$mail->IsSMTP();                                
$mail->Host = 'smtp.gmail.com';     
// $mail->SMTPDebug = 1; 
$mail->Port = 465;                              
$mail->SMTPAuth = true;                         
$mail->Username = 'example@gmail.com';              
$mail->Password = '1233aqqq';                   
$mail->SMTPSecure = 'ssl';                      
$mail->From = $_POST["email"];                  
$mail->FromName = $_POST["name"];               

$mail->AddCC($_POST["email"], $_POST["name"]);  
$mail->WordWrap = 50;                           
$mail->IsHTML(true);                                            
$mail->Subject = $_POST["subject"];             
$mail->Body = $_POST["message"];                

while(list ($key, $val) = each($address)){
$mail->AddAddress($val);        
}

What I've tried?

1.我把所有接收方邮箱地址都放在数组$address = array('st9overfindsolution@gmail','st7overfindsolution@gmail');中,下面使用while功能码,但是不能用。

2.如果不使用while功能发送单个电子邮件地址,也可以工作,如下编码:

希望有人能指导我如何解决这个问题。谢谢。

【问题讨论】:

    标签: php email while-loop smtp


    【解决方案1】:

    我创建了一个名为“sendmyemail”的新函数,并在这个函数中插入了代码,所以你可以通过使用电子邮件地址调用这个函数来发送多封电子邮件。

    <?php
    
    /**
     * This example shows sending a message using a local sendmail binary.
     */
    
    //Import the PHPMailer class into the global namespace
    use PHPMailer\PHPMailer\PHPMailer;
    
    require '../vendor/autoload.php';
    
    function sendmyemail($whotoEmail){
      //Create a new PHPMailer instance
      $mail = new PHPMailer();
      //Set PHPMailer to use the sendmail transport
      $mail->isSendmail();
      //Set who the message is to be sent from
      $mail->setFrom('from@example.com', 'First Last');
      //Set who the message is to be sent to
      $mail->addAddress($whotoEmail);
      //Set the subject line
      $mail->Subject = 'PHPMailer sendmail test';
      //Read an HTML message body from an external file, convert referenced images to           
      embedded,
      //convert HTML into a basic plain-text alternative body
      $mail->msgHTML(file_get_contents('contents.html'), __DIR__);
      //Replace the plain text body with one created manually
      $mail->AltBody = 'This is a plain-text message body';
      //Attach an image file
      $mail->addAttachment('images/phpmailer_mini.png');
    
      //send the message, check for errors
      if (!$mail->send()) {
          echo 'Mailer Error: ' . $mail->ErrorInfo;
      } else {
          echo 'Message sent to '.$whotoEmail.'!';
      }
    }
    sendmyemail('abcduser@gmail.com');
    sendmyemail('efguser@gmail.com');
    sendmyemail('hijkuser@gmail.com');
    sendmyemail('lmnouser@gmail.com');
    
    

    【讨论】:

    • 所以你的函数不需要放入数组中吧?
    • 是的,不需要数组
    • $address = ""'st9overfindsolution@gmail','st7overfindsolution@gmail'"";发送我的电子邮件($地址);可以这样吗?
    • 因为$address变量数据是从数据库中抓取的。
    猜你喜欢
    • 1970-01-01
    • 2014-06-27
    • 2013-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多