2017-07-25 138 views
0

如何向注册系统发送欢迎电子邮件?Codeigniter在注册时自动发送欢迎电子邮件

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

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

+0

你需要插入您发送电子邮件的代码到'如果($这个 - > form_validation->的run()==真){'之前创建flashdata – elddenmedio

+0

是的,这是可能的。 –

+1

'是否可以在欢迎电子邮件中发送codeigniter?'是的。 欢迎来到Stack Overflow!预计你会尝试**自己编写代码**。在[做研究]之后(https://meta.stackoverflow.com/questions/261592),以及**发布你已经尝试过的**,并明确说明**不工作**并提供[最小,完整和可验证](https://stackoverflow.com/help/mcve)示例。我建议阅读[如何提出一个好问题](https://stackoverflow.com/questions/how-to-ask)。另外,一定要参加[tour](https://stackoverflow.com/tour)。 – GrumpyCrouton

回答

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

这是错误,但我不能在这里发布代码:( –

+0

不推荐'$ mail-> SMTPDebug = 1;'并且发布输出请 –

+0

好吧,我不太明白。 –

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'] = '[email protected]'; 
    $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, '[email protected]'); 
    $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 
    } 
?> 

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

+0

我在哪个部分放? –

0

插入后你必须发送邮件,所以在这里改变并添加邮件脚本。

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

if($insert_id){ 
    $to = '[email protected]'; 
    $subject = 'the subject'; 
    $message = 'hello'; 
    $headers = 'From: [email protected]' . "\r\n" . 
    'Reply-To: [email protected]' . "\r\n" . 
    'X-Mailer: PHP/' . phpversion(); 

    mail($to, $subject, $message, $headers); 
}