phpmailler error while sending email

Asked

Viewed 998 times

2

how can I send more than one email with 'phpmailler' it always gives this error 'Mail error: You must provide at least one recipient email address. 'I am developing a newsletter system that will select all registered emails but it always gives it. detail I take the emails for a 'variable' and pass it to the sending of who will receive the emails.

my code:

$nome       = "leonardo";   // Pega o valor do campo Nome
$assunto    = "sadasdsad";  // Pega o valor do campo Telefone
$email      = "[email protected]";   // Pega o valor do campo Email
$mensagem   = "asdasdsadsadsadsad"; // Pega os valores do campo Mensagem

// Variável que junta os valores acima e monta o corpo do email

$Vai        = "Nome: $nome\n\nE-mail: $email\n\nAssunto: $assunto\n\nMensagem: $mensagem\n";


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'];
}

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

function smtpmailer($para, $de, $de_nome, $assunto, $corpo) { 
    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->SetFrom($de, $de_nome);
    $mail->Subject ="gmail - Contato";
    $mail->Body = $corpo;
    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;
    }
}
var_dump($emails);
// 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, '$email', '$nome', '$assunto', $Vai)) {


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

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

2 answers

2


To set your function, first hit the array of emails, the code below will store only the first email from the database.

$query = "SELECT * FROM newsletter";
$result = $mysqli->query($query);
$row = $result->fetch_array();
$mail = $row['mail'];

Change the code to:

$query = "SELECT * FROM newsletter";
$result = $mysqli->query($query);
$emails = $result->fetch_all();

or:

$query = "SELECT * FROM newsletter";
$result = $mysqli->query($query);
$emails = array();
while($row = $result->fetch_array()){
   $emails[] = $row['mail'];
}

To send the same email to several people you need to do a foreach iterating $emails and inside the call loop AddAddress

change:

$mail->AddAddress($para);

for:

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

and the call should look like this:

if (smtpmailer('$para', '$email', '$nome', '$assunto', $Vai)) {

The line $to = array($mail); is no longer needed.

  • i made the way Voce spoke and gave error Parse error: syntax error, Unexpected 'Else' (T_ELSE) in C: xampp htdocs new enviar_email.php on line 38 I will edit the Perg with the code that Voce described

  • fixed Else Voce error but still continues error Mail error: You must provide at least one recipient email address.

  • solved the mistakes and worked out was I will edit the Perg and leave the answer to those who need to use

  • @Leonardocosta, now that I’ve seen the comments, the keys are really wrong, I’ll correct that.

0

Do not pass email as array, pass as CSV, comma-separated values:

[email protected], [email protected]

To transform the array $to in CSV use function implode

$to = array($mail);
$to = implode($to, ",");
  • 1

    but the emails come registered from the bank as I will get all registered without being with array?

  • they come in what database format? gives an example.

  • If you have an array implode($to, ",");

  • ah to insert , in them would be this?

  • I edited the answer from a look

  • I tried to do this but still in error

  • I gave a var dump on it and look what it brings me I have 2 email in the string database(29) "[email protected]" Mail error: You must provide at least one recipient email address.

Show 2 more comments

Browser other questions tagged

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