简体   繁体   中英

Insert a Variable of String into an array

I'm using a Sendinblue SMTP into my PHP project and I want to send transactionals emails to a dynamic list of emails, the problem is that I have a syntax error when I'm using a variable instead a string. For example, this code works great:

    include 'Mailin.php';
    $mailin = new Mailin('senders@sender.com', 'key');
    $mailin->
    addTo(
        array(
                 'email1@email.com' => '', 'email2@email.com' => '', 'email3@email.com' => ''
            )
    )->

    setFrom('sender@sender.com', 'Test')->
    setReplyTo('sender@sender.com', 'Test')->
    setSubject('Example')->
    setText('Test')->
    setHtml($htmlContent);
    $res = $mailin->send();
    print_r($res);

But if I use a variable instead the Strings in "addTo Array" it shows syntax error, for example:

    $customers = '';
    foreach ($clientes as $customer) {

        for ($i=1; $i < 41; $i++) { 

            if ($customer['email'.$i]  != "" or $customer['email'.$i] != NULL) {

                $customers .= "'".$customer['email'.$i]. "' => '', " ; //for each customer's email add the email in " 'email@email.com' => '', " format
            }
        }
    }

    $customers = substr($customers, 0, -2); //removes last space and comma of the String

    include 'Mailin.php';
    $mailin = new Mailin('senders@sender.com', 'key');
    $mailin->
    addTo(
        array(
                 $customers
            )
    )->

    setFrom('sender@sender.com', 'Test')->
    setReplyTo('sender@sender.com', 'Test')->
    setSubject('Example')->
    setText('Test')->
    setHtml($htmlContent);
    $res = $mailin->send();
    print_r($res);

If I use the Print_r($customers) function it shows the exact string that I used in my first example, even if I use the code:

    $text = "'email1@email.com' => '', 'email2@email.com' => '', 'email3@email.com' => ''";

    if ($customers == $text) {
        print_r("Yes");
    }else{
        print_r("No");
    }

The result is "Yes", but when I use the variable in

    addTo(
        array(
                 $customers
            )
    )->

shows an error, but if I use the string directly the email is sent

    addTo(
        array(
                 'email1@email.com' => '', 'email2@email.com' => '', 'email3@email.com' => ''
            )
    )->

And I don't know why it shows error if the $customers variable has the string that is needed.

Do you know how to use the variable with the emails that I need to send?

You don't build an array by concatenating strings with => in them. To create an element in an associative array, just assign to that array index.

$customers = [];
foreach ($customers as $customer) {
    for ($i = 1; $i < 41; $i++) {
        if (!empty($customer["email" . $i])) {
            $customers[$customer["email" . $i]] = "";
        }
    }
}
include 'Mailin.php';
$mailin = new Mailin('senders@sender.com', 'key');
$mailin->
addTo($customers)->
...

Also, see Why non-equality check of one variable against many values always returns true? for why you should have used && rather than || when you were skipping empty emails (which I've simplified by using !empty() ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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