0

How can I send a welcome email with the registration system?

function register()
    {
        if(isset($_POST['register'])){

            $this->form_validation->set_rules('username','Username','required|is_unique[accounts.Username]');
            $this->form_validation->set_rules('email','Email','required|is_unique[accounts.Email]');
            $this->form_validation->set_rules('password','Password','required');
         // 
            if($this->form_validation->run () == true){
                echo 'Form Validate';

                $data = array(
                    'username'=>$_POST['username'],
                    'email'=>$_POST['email'],
                    'password'=>strtoupper(hash('whirlpool',$_POST['password']))

                    );

                $this->db->insert('accounts',$data);

                $this->session->set_flashdata('success_msg', 'Sua conta foi criada com sucesso.');

                redirect("painel/register");
            }
        }

How can I send a welcome email with the registration system?

Pedro HB
  • 15
  • 1
  • 6
  • you need to insert your send email code into `if($this->form_validation->run () == true){` before create the flashdata – elddenmedio Jul 25 '17 at 16:16
  • yes, it's possible. – Muhammad Usman Jul 25 '17 at 16:16
  • 1
    `Is it possible to send codeigniter in welcome email?` Yes. Welcome to Stack Overflow! You are expected to try to **write the code yourself**. After [doing research](https://meta.stackoverflow.com/questions/261592), and **post what you've tried** with a clear explanation of what **isn't working** and providing a [Minimal, Complete, and Verifiable](https://stackoverflow.com/help/mcve) example. I suggest reading [How to Ask a Good Question](https://stackoverflow.com/questions/how-to-ask). Also, be sure to take the [tour](https://stackoverflow.com/tour). – GrumpyCrouton Jul 25 '17 at 16:16
  • You should use [`password_hash()`](http://us3.php.net/manual/en/function.password-hash.php) and [`password_verify()`](http://us3.php.net/manual/en/function.password-verify.php) instead of unsalted whirlpool. General use hashes are designed to be fast, which is not a feature you want when you're trying to make brute-forcing impossible. – Alex Howansky Jul 25 '17 at 16:20
  • (The `function register()`-line belongs to the code block, too: please indent it by four spaces. Your post ends with a colon: something missing there?) – greybeard Jul 25 '17 at 17:17
  • put function in code brackets – Tony Tannous Jul 26 '17 at 05:36

3 Answers3

0

After Insert successful do this

 try { 
       $this->send_email($email, $subject, $message)
      } catch (Exception $e) {
         $this->set_error($e->getMessage());
        }

My send_mail function :

public function send_mail($to, $subject, $body, $from = NULL, $from_name = NULL, $attachment = NULL, $cc = NULL, $bcc = NULL) {

       try {
            $mail = new PHPMailer;
            $mail->isSMTP();
//             $mail->SMTPDebug = 1;
            $mail->Host = "smtp.gmail.com";
            $mail->SMTPAuth = true;
            $mail->Username = $emailusername;
            $mail->Password = $emailpassword;
            $mail->Port = 465;

            if ($from && $from_name) {
                $mail->setFrom($from, $from_name);
                $mail->setaddReplyToFrom($from, $from_name);
            } elseif ($from) {
                $mail->setFrom($from, $this->site_name);
                $mail->addReplyTo($from, $this->site_name);
            } else {
                $mail->setFrom($this->default_email, $this->site_name);
                $mail->addReplyTo($this->default_email, $this->site_name);
            }

            $mail->addAddress($to);
            if ($cc) { $mail->addCC($cc); }
            if ($bcc) { $mail->addBCC($bcc); }
            $mail->Subject = $subject;
            $mail->isHTML(true);
            $mail->Body = $body;
            if ($attachment) {
                if (is_array($attachment)) {
                    foreach ($attachment as $attach) {
                        $mail->addAttachment($attach);
                    }
                } else {
                    $mail->addAttachment($attachment);
                }
            }

            if (!$mail->send()) {
                throw new Exception($mail->ErrorInfo);
                return FALSE;
            }
            return TRUE;
        } catch (phpmailerException $e) {
            throw new Exception($e->errorMessage());
        } catch (Exception $e) {
            throw new Exception($e->getMessage());
        }
    }
Arun pandian M
  • 862
  • 10
  • 17
0

Simply Create a function in autoload.php

<?php 
function sendMailToUser($toMail, $fromMail, $subject, $message) {
    $config['protocol'] = 'smtp';
    $config['smtp_host'] = 'smtp.gmail.com';
    $config['smtp_port'] = '25';
    $config['smtp_user'] = 'abcs@xyz.com';
    $config['smtp_pass'] = '12345';
    $config['mailtype'] = 'html';
    $config['charset'] = 'iso-8859-1';
    $config['wordwrap'] = TRUE;

    $CI = & get_instance();
    $CI->load->library('email', $config);
    // Sender email address
    $CI->email->from($fromMail, 'wxyz@abc.com');
    $CI->email->to($toMail);
    $CI->email->subject($subject);
    $CI->email->message($message);
    if ($CI->email->send()) {
        return true;
    } else {
        return false;
    }
}
?>

Use this function within your code, Like...

<?php 
     if($this->form_validation->run () == true){
          $mailSend = sendMailToUser($toMail, $fromMail, $subject, $message); // Returns 1 if Mail will be sent Successfully
     }
?>

Now, You can use sendMailToUser() anywhere in the Project. Thank You.

D D Parmar
  • 127
  • 2
  • 11
0

After the insertion you have to send the mail, so change here and add the mail script.

$this->db->insert('accounts',$data);
$insert_id = $this->db->insert_id();

if($insert_id){
    $to = 'nobody@example.com';
    $subject = 'the subject';
    $message = 'hello';
    $headers = 'From: webmaster@example.com' . "\r\n" .
    'Reply-To: webmaster@example.com' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

    mail($to, $subject, $message, $headers);
}
Anand Pandey
  • 2,025
  • 3
  • 20
  • 39