Codeigniter注册时自动欢迎电子邮件 - heelp

时间:2017-07-25 16:14:29

标签: php mysql codeigniter email

如何使用注册系统发送欢迎电子邮件?

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");
            }
        }

如何使用注册系统发送欢迎电子邮件?

3 个答案:

答案 0 :(得分:0)

插入成功后执行此操作

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

我的send_mail功能:

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());
        }
    }

答案 1 :(得分:0)

只需在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;
    }
}
?>

在您的代码中使用此功能,例如......

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

现在,您可以在Project中的任何位置使用sendMailToUser()。 谢谢。

答案 2 :(得分:0)

插入后您必须发送邮件,因此请在此处更改并添加邮件脚本。

$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);
}