how to add phpmailer hidden copy

Asked

Viewed 8,046 times

3

How to send newsletter with hidden copy to customers, appearing only the email of the same that was sent?

code:

require_once("../phpmailer/class.phpmailer.php");
$mysqli = mysqli_connect("localhost","root", "", "projeto") or die(mysqli_connect_error());
$query = "SELECT * FROM newsletter";
$result = $mysqli->query($query);
$emails = array();
while($row = $result->fetch_array()){
   $emails[] = $row['mail'];
   $nome[] = $row['name'];
}


define('GUSER', '[email protected]'); // <-- Insira aqui o seu GMail
define('GPWD', 'test123456');       // <-- Insira aqui a senha do seu GMail

function smtpmailer($para) { 

    global $error;
    $mail = new PHPMailer();
    $mail->IsSMTP();        // Ativar SMTP
    $mail->SMTPDebug = 0;       // Debugar: 1 = erros e mensagens, 2 = mensagens apenas
    $mail->SMTPAuth = true;     // Autenticação ativada
    $mail->SMTPSecure = 'ssl';  // SSL REQUERIDO pelo GMail
    $mail->Host = 'smtp.gmail.com'; // SMTP utilizado
    $mail->Port = 465;          // A porta 587 deverá estar aberta em seu servidor
    $mail->Username = GUSER;
    $mail->Password = GPWD;
    $mail->isHTML(true);
    $mailer->AddBCC($emails, $nome);
    $mail->CharSet   = 'utf-8';

    $mail->Subject ="teste de newsletter";
    $mail->Body = 'oi';

    if(count($para) > 1){
      foreach($para as $email){
        $mail->AddAddress($email);
      }
    }else{
        $mail->AddAddress($para);
      }

    if(!$mail->Send()) {
        $error = 'Mail error: '.$mail->ErrorInfo; 
        return false;
    } else {
        $error = 'Mensagem enviada!';
        return true;
    }
}

// Insira abaixo o email que irá receber a mensagem, o email que irá enviar (o mesmo da variável GUSER), 
//o nome do email que envia a mensagem, o Assunto da mensagem e por último a variável com o corpo do email.

 if (smtpmailer($emails)) {


    echo"<script type=\"text/javascript\">alert(\"Sua Mensagem foi Enviada com Sucesso!\");
            history.go(-1);
         </script>\n";

}
if (!empty($error)) echo $error;

1 answer

4

Using Addbcc should make it work normally

$mailer->AddBCC("[email protected]", "test");
 if(!$mail->Send()) {
    $error = 'Mail error: '.$mail->ErrorInfo; 
    return false;
}

Try adding it before the send command at the end of all addresses. Note that because it is a hidden copy, neither the person who received it will see their name on the recipients.

See here too:

  • in it the first parameter that I have to add and the email address and the second and the name neh in case would be something like this based on my $Mailer->Addbcc($emails, $name); ?

  • Yes, if I’m not mistaken the second parameter is optional, you can empty it.

Browser other questions tagged

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