email sending error with phpmailer

Asked

Viewed 66 times

0

Guys I’m trying to send an email to several people inside an array called $destinatario.

For this I created a function called envia_email.

But I’m having the following mistake:

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

That occurs on the line:

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

I don’t 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();
    }

1 answer

2


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

Function envia_email($addressee,$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();
}
  • I did not understand very well, have to post an example in the answer?

  • of course, I put it in the back there.

  • hahhahahahahha as I did not pay attention to that hahah, vlw thank you very much

  • happens hehe :p

Browser other questions tagged

You are not signed in. Login or sign up in order to post.