2017-07-25 143 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); 
}