Form registration confirmation email with codeigniter

Asked

Viewed 296 times

0

Eai personal. I have the following scenario...

I wanted to send an email to the user of my site. For example: - Welcome to the Fulano company, wait for us to contact you soon.

This is just an example, but in my real form I have other fields(type: name, email, address, city, state, chosen plan and others).

So the goal would be that when he filled out the form in full, it would be sent to the email that he provided at the time of registration, the message that I said most about. (Welcome home.....)

Obs:

  • controller Contact: I have two functions, mail() and checkEmail()
  • envieEmail(): it works, the form data arrives in my server email.

  • the error is in checkEmail() on that line $address = $this->sendEmail($email);

Error says $email variable is not set.

If someone has another idea to carry out the same process, all help is welcome!

<?php

defined('BASEPATH') OR Exit('No direct script access allowed');

class Contact extends Ci_controller {

public function __construct(){
    parent::__construct();
    $this->load->library('email');
    $this->load->library('session');
    $this->load->helper('url');
    $this->load->library('form_validation');
}

public function index(){
    $this->load->view('contato');    
}

// funcao: envia o email informando para o usuario que o registro foi recebido
public function checkEmail(){

// config do servidor de email
    $config = array();
    $config ['charset'] = 'utf8';
    $config ['mailtype'] = 'html';
    $config ['protocol'] = 'smtp';
    $config ['smtp_host'] = '';
    $config ['smtp_user'] = '[email protected]';
    $config ['smtp_pass'] = '';
    $config ['smtp_port'] = 25;

    $this->email->initialize($config);

    $this->email->set_newline("\r\n");

    $address = $this->enviaEmail($email);

// formatacao do corpo de email
    $this->email->from('[email protected]');
    $this->email->to($address);
    $this->email->subject('Confirmação de recebimento!');       
    $this->email->message('
        <html>
        <head>

        <style>
          .card {
        border: 1px solid #ededed;
        background-color: #fff;
        padding: 43px 40px;
        border-bottom: 1px solid #FFA100;
          }

          .page-footer{padding: 43px 40px;}

          body{background-color:#D9EBFF;}

          h2 {color: #002040; font-size: 16px;}

        </style>
        </head>
        <body>

         <div class="">
    <div class="card">
      <h2>Olá, seja Bem Vindo! Nome Empresa agradecece pela sua escolha!</h2><br>

      <p>O seu email foi recebido pelo nosso atendimento, aguarde que entraremos em contato para mais informações</p>

    </div>
     </div>


     <div class="page-footer">
        <p>Sua Empresa | Todos os direitos reservados</p>
     </div>

        </body>
        </html>

    '); 

    if ($this->email->send()){
        echo "email enviado";
    }
    else{
        echo "erro ao enviar";

    }
}

//funcao que envia o email pro usuario que fez o registro no formulario
public function enviaEmail(){

   //validacao do formulario
    $this->form_validation->set_rules('nome','nome', 'required|min_length[5]|max_length[30]');
$this->form_validation->set_rules('email','email', 'required');
$this->form_validation->set_rules('mensagem','mensagem', 'required|min_length[5]|max_length[144]');
$this->form_validation->set_rules('item','item', 'required');

    $data = $this->input->post();

// config servidor de email
    $config = array();
    $config ['charset'] = 'utf8';
    $config['mailtype'] = 'html';
    $config ['protocol'] = 'smtp';
    $config ['smtp_host'] = '';
    $config ['smtp_user'] = '[email protected]';
    $config ['smtp_pass'] = '';
    $config ['smtp_port'] = 25;

    // recebe os valores dos campos preenchidos pelo usuario
    $nome = $this->input->post('nome', TRUE);
    $email = $this->input->post('email', TRUE);
    $emailTo = $this->input->post('email', TRUE);
    $item = $this->input->post('item', TRUE);
    $mensagem = $this->input->post('mensagem', TRUE);


    $this->email->initialize($config);

    $this->email->set_newline("\r\n");

// formatacao do corpo de email
    $this->email->from($data['email']);
    $this->email->to('[email protected]');
    $this->email->subject('Entre em Contato');      
    $this->email->message('
        <html>
        <head>

        <style>
          .card {
        border: 1px solid #ededed;
        background-color: #fff;
        padding: 43px 40px;
        border-bottom: 1px solid #FFA100;
          }

          .page-footer{padding: 43px 40px;}

          body{background-color:#D9EBFF;}

          h2 {color: #002040; font-size: 16px;}

        </style>
        </head>
        <body>

         <div class="">
    <div class="card">
      <h2>Nome:</h2>'.$nome.' <br>
          <h2>Email:</h2> '.$email.' <br>
              <h2>Tipo de Item:</h2> '.$item.' <br>
          <h2>Mensagem:</h2> '.$mensagem.' <br>
    </div>
     </div>


     <div class="page-footer">
        <p>Sua Empresa | Todos os direitos reservados</p>
        <p>Este formulário foi enviado direto do site suaempresa.com.br </p>
     </div>

        </body>
        </html>

    '); 

    $this->form_validation->set_error_delimiters('<div class="alert alert-danger">', '</div>');


    if ($this->email->send()){ 
        $this->checkEmail();
    }
    else{

        $this->load->view('errorEmail');      
    }
}

}

  • check the answer you solved using the blue V on the side of the corresponding score

2 answers

0


Good night!

In this line you are passing an attribute in a method that does not expect any attribute

$address = $this->enviaEmail($email);

Try adding one or more attributes to process the data without repeating the inputs, something like:

public function enviaEmail($email){

Also check that when calling another method in Codeigniter it can fetch the POST data, if it fails, send them as parameter to this function.

And be careful when calling the same function inside another, it can generate an unnecessary upload loop!

Hug

0

Good night! , thanks a lot for the tip, it worked here. I’ll put the changes I made here!

// in the checkEmail function()

I removed that line $address = $this->enviaEmail($email);

of

public function checkEmail()

I switched to

public function checkEmail($email)

of

$this->email->to($address);

I switched to

$this->email->to($email);

// in the mail() function, I made the following changes

added this excerpt $emailTo = $this->checkEmail($email);

and in IF, instead of executing a method (which was generating the loops). I passed a view, stating that the email was successfully sent!

if($this->email->send())
    {
        $this->load->view('formEnviado');          
    }
    else
    {
         $this->load->view('errorEmail');
    }

Browser other questions tagged

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