error sending e-mail with phpmailer

0

I'm trying to send an email to several people within an array called $destinatario .

For this I created a function called envia_email .

But I'm having the following error:

Fatal error: Call to a member function AddBCC() on string in

What happens on the line:

$email->AddBCC($email, $name);

I do not know what it can be. Follow my code below.

        <?php

    $destinatario = array(
        '[email protected]' => 'Person One',
        '[email protected]' => 'Perqweson One',
    );

    function envia_email($destinatario,$titulo,$texto) {

        // Inclui o arquivo class.phpmailer.php localizado na pasta phpmailer
        include("Modules/phpmailer/class.phpmailer.php");

        // Instanciamos a classe
        $email = new PHPMailer();

        // Informamos que a classe ira enviar o email por SMTP
        $email->isSMTP();

        // Configuração de SMTP
        $email->Host = "xxxxxxx";
        $email->SMTPAuth = true;
        $email->SMTPDebug = false;
        $email->Port = "587";
        $email->Username = "xxxxxxx";
        $email->Password = "xxxxxxx";

        // Remetente da mensagem
        $email->From = "xxxxxxx";
        $email->FromName = "xxxxxxx";

        // Destinatário do email
        foreach($destinatario as $email => $name) {
            $email->AddBCC($email, $name);
        }

        // Iremos enviar o email no formato HTML
        $email->IsHTML(true);

        // Define a mensagem (Texto e Assunto)
        $email->Subject = "$titulo";

        $email->Body = "$texto";

        // Envia o e-mail
        $email->Send();
    }
    
asked by anonymous 21.07.2016 / 18:45

1 answer

2

You are overwriting the variable $email inside the foreach loop just change the name of one of them that should work.

function envia_email ($ recipient, $ title, $ text) {

    // Inclui o arquivo class.phpmailer.php localizado na pasta phpmailer
    include("Modules/phpmailer/class.phpmailer.php");

    // Instanciamos a classe
    $email = new PHPMailer();

    // Informamos que a classe ira enviar o email por SMTP
    $email->isSMTP();

    // Configuração de SMTP
    $email->Host = "xxxxxxx";
    $email->SMTPAuth = true;
    $email->SMTPDebug = false;
    $email->Port = "587";
    $email->Username = "xxxxxxx";
    $email->Password = "xxxxxxx";

    // Remetente da mensagem
    $email->From = "xxxxxxx";
    $email->FromName = "xxxxxxx";

    // Destinatário do email
    foreach($destinatario as $emailToSend => $name) {
        $email->AddBCC($emailToSend, $name);
    }

    // Iremos enviar o email no formato HTML
    $email->IsHTML(true);

    // Define a mensagem (Texto e Assunto)
    $email->Subject = "$titulo";

    $email->Body = "$texto";

    // Envia o e-mail
    $email->Send();
}
    
21.07.2016 / 18:48