2

I am trying SwiftMailer to fetch the email addresses to an array() I have the original example and I know what I want to put in it but I can't see the forest from the trees... any one out there with proper eyes please:

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

// Send the message
$failedRecipients = array();
$numSent = 0;

Here is where I am struggling:

 foreach($groups as $value)
    { 
             echo "<h3>".$value."</h3>"; //this is working

    // get the email groups
    $sql="SELECT * FROM emails WHERE sendesstat=1 AND deleted=0 AND classhoz=\"".$value."\"";
    $query=mysql_query($sql);

    // fetch the emails from the group
    while($data=mysql_fetch_array($query))
                {
                  $emil="'".$data["email"]."' => '".$data["firstname"]." ".$data["surname"]."'";
                  echo $emil;

this echo is working but I can't put in the $to[] array... (sorry) and this is how it should appear:

$to = array('[email protected]', '[email protected]' => 'A name');

the issue is the given name => 'A name' I am being sick and I can't find the syntax !!!

                  $to[ ]= $emil;            
                }
    }

foreach ($to as $address => $name)
{
  if (is_int($address)) {
   $message->setTo($name);
  } else {
   $message->setTo(array($address => $name));
}

 $numSent += $mailer->send($message, $failedRecipients);
}

printf("Sent %d messages\n", $numSent);

please please everything else is working now only this bit is missing. Thank you

1 Answer 1

1

You are defining the $to array the wrong way.

Use this to add an item on the array

$to[$data["email"]] = $data["firstname"]." ".$data["surname"];
//  ^ Index           ^ Value

Later On, when you use foreach loop it will well differentiated into $address and $name, just the way you are doing.

foreach ($to as $address => $name) {
    echo $address;
    echo $name; 
}
Sign up to request clarification or add additional context in comments.

1 Comment

thanks very much I am going to put in my code :)))) thanks a lot again

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.