<?php
//
// Variáveis que vem da chamada à função
//
$arr_destinatarios_email = array();
$arr_destinatarios_nome = array();
$arr_destinatarios_email[] = "vladhe@gmail.com";
$arr_destinatarios_email[] = "vlad_emir@yahoo.com.br";
$arr_destinatarios_email[] = "vlademir@hernandes.teo.br";
$arr_destinatarios_nome[] = "Vlademir do Gmail";
$arr_destinatarios_nome[] = "Vlademir do Yahoo";
$arr_destinatarios_nome[] = "Vlademir do Hernandes";
$assunto = "Assunto do Email a ser Enviado por php via curl";
$mensagem_do_email = "<p>Mensagem HTML a ser enviada por php cia curls</p>";
$usuario_login = "vlademir";
$reply_to = "financeiro@igrejafonte.org.br";
$reply_to_name = "Igreja Batista Fonte - Financeiro";
//
//
//
$sender_email = "admin@igrejafonte.com.br";
$sender_name = "Igreja Batista Fonte";
$msge = "<p>***</p>";
$cc = ', "cc": [';
$virgula = '';
foreach ($arr_destinatarios_email as $i => $email) // pega email e celular do destinatario
{
$nome = $arr_destinatarios_nome[$i];
if ($i == 0)
{
$to = '"to": [{"email": "' . $email . '", "name": "' . $nome . '"}]';
}
else
{
$cc .= $virgula . '{"email":"' . $email . '","name":"' . $nome . '"}';
$virgula = ', ';
}
}
$cc .= ']';
if($cc == ', "cc": []') // se não tiver cc, limpa para não dar problema
{
$cc = '';
}
//
$curl = curl_init();
//
// Usar o PHP para gerar o Json para não dar pau com erro Json inválido
//
$arr = array('html' => $mensagem_do_email);
$htmlcontent = json_encode($arr);
$htmlcontent = substr($htmlcontent,1); // elimina primeira chave {
$htmlcontent = substr($htmlcontent,0,strlen($htmlcontent)-1); // elimina última chave }
//
/*
'{"from": {"email": "admin@igrejafonte.com.br" , "name": "Igreja Batista Fonte"}, "to": [{"email": "vladhe@gmail.com" , "name": "Vlademir - gmail"}], "cc": [{"email": "vlad_emir@yahoo.com.br" , "name": "Vlademir - yahoo"} , {"email": "vlademir@hernandes.teo.br" , "name": "Vlademir - hernandes"}] , "reply_to": {"email": "financeiro@igrejafonte.org.br" , "name": "Igreja Batista Fonte - Financeiro"} , "subject": "Assunto do email de teste com Reply To email e name!" , "html": "<p>do email de teste com Reply To email e name</p>"}'
*/
// function comum_envia_email($arr_destinatarios_email, $arr_destinatarios_nome, $assunto, $mensagem_do_email, $usuario_login, $reply_to, $reply_to_name)
$postfields = '{"from": {"email": "'. $sender_email .'" , "name": "' . $sender_name . '"},' . $to . ', ' . $cc . ' , "reply_to": {"email": "' . $reply_to . '" , "name": "'. $reply_to_name . '"} , "subject": "' . $assunto . '" , "html": "' . $htmlcontent . '"}';
//
curl_setopt_array($curl, [
CURLOPT_URL => 'https://api.mailersend.com/v1/email',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => $postfields,
CURLOPT_HTTPHEADER => [
'accept: application/json',
'Authorization: Bearer mlsn.fbd6485c1627448d955df50ff8313fd39f851f52adcafcaf048b11c771544f1d',
'content-type: application/json'
],
]);
$response = curl_exec($curl);
// echo "<p>Postfields = $postfields </p>";
$err = curl_error($curl);
curl_close($curl);
if ($err)
{
$msge .= "<p>***** Não foi possível enviar o email para os destinatários" . $to . $cc . " Mensagem de Erro: " . $err . " *****</p>";
// echo $msge;
}
if($usuario_login == "vlademir" || $usuario_login == "vlad_emir")
{
$msge .= "<p><br>MailerSend - COMANDOS E RESPOSTAS:<br></p>";
$msge .= "<p>Comando MailerSend= $postfields<br></p>";
$msge .= "<p>Resposta MailerSend= $response ;<br>Erro do Curl se houver: $err </p>";
}
//
//
//
echo $msge;