Problem sending multiple emails

Asked

Viewed 47 times

0

I got a problem, I made a script to send multiple emails, the problem is that it only sends to the first result of the select,and out of the loop without sending to the missing ones, below is the code. If someone knows what it might be that would help a lot:

<?php
include('../../config.php');

$evento = $_POST['evento'];
$assunto = $_POST['assunto'];
$msg = $_POST['corpo'];

    $select_ins = "SELECT inscrito, evento FROM inscricoes WHERE status = 'Aprovado' AND evento =".$evento." ";

$insc = mysql_query($select_ins) or die(mysql_error());

while($ins = mysql_fetch_array($insc)){
    $select_part = "SELECT nome, email, telefone FROM inscrito WHERE id=".$ins['inscrito'];
             $parts = mysql_query($select_part) or die(mysql_error());
             $part = mysql_fetch_array($parts);

             $select_eve= "SELECT nome FROM eventos WHERE id=".$ins['evento'];
             $eves = mysql_query($select_eve) or die(mysql_error());
             $eve = mysql_fetch_array($eves);

    $email = $part['email'];
    $msg = str_replace('#nome', $part['nome'], $msg);
    $msg = str_replace('#telefone', $part['telefone'], $msg);
             $msg = str_replace('#evento', $eve['nome'], $msg);

    date_default_timezone_set ('America/Sao_Paulo');
             $corpo = "Email de teste para usuarios";

                require ('../../../aws/aws-autoloader.php');

                // Replace [email protected] with your "From" address.
                // This address must be verified with Amazon SES.
                define('SENDER', 'Eventos<[email protected]>');

                // Replace [email protected] with a "To" address. If your account
                // is still in the sandbox, this address must be verified.
                define('RECIPIENT', $email);

                // Replace us-west-2 with the AWS region you're using for Amazon SES.
                define('REGION','us-east-1');

                define('SUBJECT', $assunto); //Assunto digitado pelo Administrador
                define('BODY', $corpo);


                $client = Aws\Ses\SesClient::factory(array(
                    'version'=> 'latest',
                    'region' => REGION,
                    'credentials' => array(
                        'key' => 'AJSIDSAJIDSAJIDSAID',
                        'secret' => 'SOKAODSAKDOSADSA86SD6SA2',
                        )
                ));

                $request = array();
                $request['Source'] = SENDER;
                $request['Destination']['ToAddresses'] = array(RECIPIENT);
                $request['Message']['Subject']['Data'] = SUBJECT;
                $request['Message']['Body']['Html']['Data'] = BODY;

                try {
                     $result = $client->sendEmail($request);
                     $messageId = $result->get('MessageId');
                     echo("Emails enviados com sucesso! ");
                } catch (Exception $e) {
                     echo("Erro ao enviar, confira se o email informado esta correto.");
                     echo($e->getMessage()."\n");
                }



}

  • 1

    pq vc define the constants within while? they are constants pq ... their value does not change ...

  • Really, but I took the counting from inside but it continues with the same problem, sends to the first and to the other not

  • Any error message appears? put this at the beginning, ini_set('display_errors', true); error_reporting(E_ALL);

  • No mistakes, but I realized that he is doing the number of correct loops, so much so that more than one email arrives in the inbox, the first result, I do not know why the foreach is not bringing the other data

1 answer

0


The solution was to replace the counters with variables, thus taking them out of the loop. The code looked like this:

<?php
$ano = date('Y');

//verificação de evento
$evento = $_POST['evento'];
$assunto = $_POST['assunto'];
$msg = $_POST['corpo'];

require ('../../../aws/aws-autoloader.php');
define('SENDER', '[email protected]');
define('REGION','us-east-1');
define('SUBJECT', $assunto);

$client = Aws\Ses\SesClient::factory(array(
    'version'=> 'latest',
    'region' => REGION,
    'credentials' => array(
        'key' => 'key',
        'secret' => 'secret',
        )
));

if($evento == 0){
	$select_ins = "SELECT DISTINCT inscrito, evento FROM inscricoes WHERE status = 'Aprovado' GROUP BY inscrito";
}else{
	$select_ins = "SELECT inscrito, evento FROM inscricoes WHERE status = 'Aprovado' AND evento =".$evento;
}

$insc = mysql_query($select_ins, $conexao) or die(mysql_error());

while($ins = mysql_fetch_array($insc)){
             $msg = $_POST['corpo'];
	$select_part = "SELECT nome, email, telefone FROM inscrito WHERE id=".$ins['inscrito']." ";
             $parts = mysql_query($select_part) or die(mysql_error());
             $part = mysql_fetch_array($parts);

             $select_eve= "SELECT nome FROM eventos WHERE id=".$ins['evento'];
             $eves = mysql_query($select_eve) or die(mysql_error());
             $eve = mysql_fetch_array($eves);

	$email = $part['email'];
	$msg = str_replace('#nome', $part['nome'], $msg);
	$msg = str_replace('#telefone', $part['telefone'], $msg);
             $msg = str_replace('#evento', $eve['nome'], $msg);

	date_default_timezone_set ('America/Sao_Paulo');
             $corpo = "Mensage Aqui";

                $request = array();
                $request['Source'] = SENDER;
                $request['Destination']['ToAddresses'] = array($email);
                $request['Message']['Subject']['Data'] = SUBJECT;
                $request['Message']['Body']['Html']['Data'] = $corpo;

                $result = $client->sendEmail($request);

                sleep(1);
}
                try {
                     $messageId = $result->get('MessageId');
                     echo("Emails enviados com sucesso! ");
                } catch (Exception $e) {
                     echo("Erro ao enviar, confira se o email informado esta correto.");
                     echo($e->getMessage()."\n");
                }

Browser other questions tagged

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