Farahatiqah34
Farahatiqah34

Reputation: 15

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>

Upvotes: 0

Views: 202

Answers (4)

GeorgeKaf
GeorgeKaf

Reputation: 613

Here is a test script I use. Swiftmailer is installed using a package manager, so I include the autoloader, not the library directly. 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 = ['[email protected]' => 'John Doe'];
$receivers = ['[email protected]'];

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 :) .

Upvotes: 1

George G
George G

Reputation: 7695

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
  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

Upvotes: 0

Budimir Skrtic
Budimir Skrtic

Reputation: 419

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']. Just put $to instead of $email and it should work. I guess you are declaring $email variable somewhere and it holds one email adress, that's why is sending only one email.

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

Upvotes: 0

bloo
bloo

Reputation: 1560

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. so i'm guessing it only iterates once since the condition will fail the second time.

try using mysqli_fetch_row() instead...

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

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

Upvotes: 0

Related Questions