简体   繁体   English

如何在数组PHPMailer中发送电子邮件

[英]How to sent email in array PHPMailer

I'm setting up an email notification in my website to notify the user to approve a claim. 我正在网站上设置电子邮件通知,以通知用户批准索赔。 The email will be send to more than one user which that are allow to approve a claim. 该电子邮件将发送给多个可以批准索赔的用户。 The problem is i want to put the email address in the button approve to know who approve the claim. 问题是我想在批准的按钮中放入电子邮件地址,以了解谁批准了索赔。

I already try to put in array but the email only can be send to the first email. 我已经尝试放入数组,但是只能将电子邮件发送到第一封电子邮件。

Below is my example code to sent multiple email address 以下是我发送多个电子邮件地址的示例代码

do{ 
$to = $Rows_RecQuery1['email'];
$mail->AddAddress($email);
}while($Rows_RecQuery1 = mysqli_fetch_array($RecQuery1));

Below is the button i want to put the email 下面是我要放入电子邮件的按钮

<a style="border-style:solid; border-color:#F00; background-color:#F00; color:#FFF; text-decoration:none"
                             href="http://www.crm.icherrycloud.com/report/approval_claim_process_email.php?<?php echo $uid ?>&process=0&claim_id=<?php echo $_GET['claim_id']?>&modified=<?php echo date('Y-m-dH:i:s', strtotime($row_RecEdit['modified']));?>">Cancel</a>

<a style="border-style:solid; border-color:#0CC; background-color:#0CC; color:#FFF;text-decoration:none"     
                          href="http://www.crm.icherrycloud.com/report/approval_claim_process_email.php?<?php echo $uid ?>&$emailprocess=3&claim_id=<?php echo $_GET['claim_id']?>&modified=<?php echo date('Y-m-dH:i:s', strtotime($row_RecEdit['modified'])); ?>">Reject</a>

<a style="border-style:solid; border-color:#00F; background-color:#00F; color:#FFF;text-decoration:none"  
                                 href="http://www.crm.icherrycloud.com/report/approval_claim_process_email.php?<?php echo $uid ?>&$emailprocess=2&claim_id=<?php echo $_GET['claim_id']?>&modified=<?php echo date('Y-m-dH:i:s', strtotime($row_RecEdit['modified'])); ?>">Approve</a>

Here is a test script I use. 这是我使用的测试脚本。 Swiftmailer is installed using a package manager, so I include the autoloader, not the library directly. Swiftmailer是使用软件包管理器安装的,因此我包含了自动加载器,而不是直接包含库。 Also, I left the logging on, in case everything breaks you can check the logs produced. 另外,我保留了登录信息,以防万一一切都中断了,您可以检查生成的日志。

<?php
$hostname = '';
$port     = 465; // or anyhting else that works with your server
$username = '';
$password = '';
$sender = ['test@mail.com' => 'John Doe'];
$receivers = ['test2@mail.com'];

echo date(DATE_RFC2822)."<br/>";
include ('vendor/autoload.php');

// Create the Transport
$transport = (new \Swift_SmtpTransport($hostname, $port))
    ->setUsername($username)
    ->setPassword($password);

//$transport->setAuthMode('login'); 
//$transport->start();

// Create the Mailer using your created Transport
$mailer = new \Swift_Mailer($transport);

// To use the ArrayLogger
//$logger = new Swift_Plugins_Loggers_ArrayLogger();
//$mailer->registerPlugin(new Swift_Plugins_LoggerPlugin($logger));

// Or to use the Echo Logger
$logger = new \Swift_Plugins_Loggers_EchoLogger();
$mailer->registerPlugin(new \Swift_Plugins_LoggerPlugin($logger));

// Create a message
$message = (new \Swift_Message('Wonderful Subject'))
    ->setFrom($sender)
    ->setTo($receivers)
    ->setBody('Here is the message itself');

// Send the message
$result = $mailer->send($message);

So for your case I'd do something like that 所以对于您的情况,我会做类似的事情

$Rows_RecQuery1 = mysqli_fetch_array($RecQuery1);
foreach ($Rows_RecQuery1 as $k=>$v) {
    $receivers[] = $v['email'];
}
....rest of the script.....

PS: In case the script doesn't works as is, it is morning, no coffee, too lazy to come back in an hour or two :) . PS:如果脚本不能按原样工作,那就是早上,没有咖啡,懒得在一两个小时后回来:)。

i think the problem is in the mysqli_fetch_array() ...it will get the result of the whole query as an array rather that fetch one row at a time as it iterates. 我认为问题出在mysqli_fetch_array() ...它将以数组的形式获取整个查询的结果,而不是每次迭代都获取一行。 so i'm guessing it only iterates once since the condition will fail the second time. 所以我猜它只会迭代一次,因为条件将第二次失败。

try using mysqli_fetch_row() instead... 尝试使用mysqli_fetch_row()代替...

https://www.w3schools.com/php/func_mysqli_fetch_array.asp https://www.w3schools.com/php/func_mysqli_fetch_array.asp

https://www.w3schools.com/php/func_mysqli_fetch_row.asp https://www.w3schools.com/php/func_mysqli_fetch_row.asp

You have a problem with mixed up variables. 您对混合变量有疑问。 In AddAdress you have put $email variable and in your loop you are fetching $Rows_RecQuery1['email'] . 在AddAdress中,您放置了$email变量,并在循环中获取$Rows_RecQuery1['email'] Just put $to instead of $email and it should work. 只需放入$to而不是$email ,它应该可以工作。 I guess you are declaring $email variable somewhere and it holds one email adress, that's why is sending only one email. 我猜您在某处声明了$email变量,并且该变量包含一个电子邮件地址,这就是为什么仅发送一封电子邮件的原因。

do{ 
  $to = $Rows_RecQuery1['email'];
  $mail->AddAddress($to);
}while($Rows_RecQuery1 = mysqli_fetch_array($RecQuery1));

There are few issues with your code 您的代码很少有问题

  1. First you are doing do..while() cycle when you should be doing while() , because you don't have data available until second cycle at least 首先,您应该在执行while() do..while()周期时执行do..while()周期,因为至少直到第二个周期才有可用数据
  2. Then you are fetching array instead of associative array, that's fine if you access data at a desired index not by name. 然后,您将获取数组而不是关联数组,如果您按所需索引而不是按名称访问数据,那很好。

Try this: 尝试这个:

while ($contact = mysqli_fetch_assoc($RecQuery1));
{
   $mail->AddAddress($contact['email']);
}

Also if you show us your query, we might be able to better help you 另外,如果您向我们显示您的查询,我们也许可以更好地帮助您

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM