How to send an email after a new mysql registration

Asked

Viewed 964 times

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 inserted in mysql, an email would be sent automatically to the email from the buyer_email table

  • Who is inserting the record into mysql? PHP?

  • What you’ve already done ?

  • Yes, the data is entered into the system administration panel in php.

  • @Zoom so far I have not done anything to send the email after there is a new registration in mysq.

2 answers

2


You have to take the script that makes the insertion and at the end of it check. Do an if to confirm that the record was inserted correctly. If yes, enter the email script, using the e-mail that came in the registration.

Example (no code, only logic):

  • send.php - Contains the data to be entered with a POST to the receive page.php

  • receives.php - Picks up the data, inserts it into the database and if the insertion was done sends the e-mail.

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
}

Checks the mail to send function.

0

The above friend already explained basically everything. And if you are wondering 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 preference.

Browser other questions tagged

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