Problem sending multiple emails

0

I have a problem, I made a script to send multiple emails, the problem is that it only sends to the first result of select , and already leaves loop without sending to those that are missing, below the code. If anyone knows what that could be, it 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");
                }



}
    
asked by anonymous 10.10.2016 / 20:15

1 answer

0

The solution was to replace the variables with variables, thus taking them out of the loop. The code looks 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");
                }
    
07.11.2016 / 15:29