Send to different email according to time of day

Asked

Viewed 37 times

0

How do I send an email form to two different emails according to the time of day.

Example: From 00:00 until 11:00AM the e-mail is sent to a certain e-mail and from 12:01PM until 23:59PM the e-mail would be sent to another e-mail.

   <?php

date_default_timezone_set('America/Sao_Paulo');
$date = date('Y-m-d H:i');

if (isset($_POST['BTEnvia'])) {

 //Variaveis de POST, Alterar somente se necessário 
 //====================================================
 $nome = $_POST['nome'];
 $email = $_POST['email'];
 $telefone = $_POST['telefone']; 
 $mensagem = $_POST['mensagem'];
 //====================================================

 //REMETENTE --> ESTE EMAIL TEM QUE SER VALIDO DO DOMINIO
 //==================================================== 
 $email_remetente = "[email protected]"; // deve ser uma conta de email do seu dominio 
 //====================================================

 //Configurações do email, ajustar conforme necessidade
 //==================================================== 
 $email_destinatario = "[email protected]"; // pode ser qualquer email que receberá as mensagens
 $email_reply = "$email"; 
 $email_assunto = "Contato formmail"; // Este será o assunto da mensagem
 //====================================================

 //Monta o Corpo da Mensagem
 //====================================================
 $email_conteudo = "Nome = $nome \n"; 
 $email_conteudo .= "Email = $email \n";
 $email_conteudo .= "Telefone = $telefone \n"; 
 $email_conteudo .= "Mensagem = $mensagem \n"; 
 //====================================================

 //Seta os Headers (Alterar somente caso necessario) 
 //==================================================== 
 $email_headers = implode ( "\n",array ( "From: $email_remetente", "Reply-To: $email_reply", "Return-Path: $email_remetente","MIME-Version: 1.0","X-Priority: 3","Content-Type: text/html; charset=UTF-8" ) );
 //====================================================

 //Enviando o email 
 //==================================================== 
 if (mail ($email_destinatario, $email_assunto, nl2br($email_conteudo), $email_headers)){ 
 echo "</b>E-Mail enviado com sucesso!</b>"; 
 } 
 else{ 
 echo "</b>Falha no envio do E-Mail!</b>"; } 
 //====================================================
} 
?>

1 answer

4


You can check the date based on Datetime, setting the time for noon, then checking if the current date is after noon. If yes, change the email that should receive the shot.

<?php

$date = new \DateTime();

$date->setTime(12,00,00);

$dateNow = new \DateTime();

$email = '[email protected]';

if($dateNow > $date) {
    $email = '[email protected]';
}

....

Browser other questions tagged

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