How to send an email after there is a new record in mysql

0

Hello,

I have a table in mysql basically in this format.

id  transaction_id   name     buyer_email               transaction_date        paymentstatus
1   989Y8DT8S65DS    Test     [email protected]    17/02/2016              Completed

My intention would be that when there is a new data entered in mysql, an email would automatically be sent to the email of the buyer_email table

    
asked by anonymous 17.02.2016 / 21:25

2 answers

2

You have to get the script that inserts and check it out. It does an if to confirm that the record has been inserted correctly. If yes, enter the email script, using the email that came in the register.

Example (no code, only logic):

  • envia.php - Contains the data to be inserted with a POST to the receive.php page

Example:

$email = filter_input(INPUT_POST, 'email', FILTER_SANITIZE_EMAIL);

$sql = mysqli_query("INSERT INTO tabela VALUES ('', '$email', ...)");
if($sql){
    //envia o email;
} else {
    //retorna para outra página
}

Check the mail function to send.

    
17.02.2016 / 22:45
0

The friend above explained basically everything. And if you have any questions about how to send the email, you can use this class I created:

    <?php
    class Email{
          var $remetente_nome;
          var $remetente_email;
          var $responder_para;
          var $email_destinatario;
          var $assunto;
          var $mensagem_HTML;
          var $erro;
          public function enviar(){
                if(!isset($this->remetente_nome) || !isset($this->remetente_email) || !isset($this->email_destinatario) || !isset($this->assunto) || !isset($this->mensagem_HTML)){
                    $this->erro="<b>ERRO: </b> Você deixou de especificar algum atributo, lembre-se de que o único opcional é o email para resposta, que utiliza o email do remetente se deixado em branco.";
                }else{
                    if(!isset($this->responder_para)){
                        $this->responder_para=$this->remetente_email;
                    }
                if(PATH_SEPARATOR == ";") $quebra_linha = "\r\n";
                else $quebra_linha = "\n";
                if(strstr($this->email_destinatario," ")){
                    $this->email_destinatario=str_replace(" ", "", $this->email_destinatario);
                }
                $headers = "MIME-Version: 1.1" .$quebra_linha;
                $headers .= "Content-type: text/html; charset=utf-8" .$quebra_linha;
                $headers .= "From: " . $this->responder_para.$quebra_linha;
                $headers .= "Reply-To: " . $this->remetente_email . $quebra_linha;
                if(!mail($this->email_destinatario, $this->assunto, $this->mensagem_HTML, $headers ,"-r".$this->responder_para)){
                        $headers .= "Return-Path: " . $this->responder_para . $quebra_linha;
                    mail($this->email_destinatario, $this->assunto, $this->mensagem_HTML, $headers);
                }
            }
          }   
    }
    /* USO 
    1- instancie a classe Email;
    $obj_email=new Email();

    2- Defina o valor dos atributos
    $obj_email->remetente_nome="";
    $obj_email->remetente_email="";
    $obj_email->responder_para=""; (opcional)
    $obj_email->email_destinatario="";
    $obj_email->assunto="";
    $obj_email->mensagem_HTML="";

    3- Envie o email
    $obj_email->enviar(); */

    $obj_email=new Email();
    $obj_email->remetente_nome="";
    $obj_email->remetente_email="";
    $obj_email->responder_para="";
    $obj_email->email_destinatario="";
    $obj_email->assunto="";
    $obj_email->mensagem_HTML="";
    $obj_email->enviar();

?>

Just change the values in the last lines to those of your choice.

    
22.02.2016 / 20:19