2016-04-26 3 views
0

Я пытаюсь автоматизировать рассылку новостей с помощью codeigniter.Response Email Won`t Sent

 public function response() 
    { 
    $status    = $this->uri->segment(3);   
    $id     = $this->uri->segment(4); 
    $id_user   = $this->uri->segment(5); 

    $data['id']   = $id; 
    $data['status']  = $status ; 

    $this->messages_model->pending($data); <<<<update database 

    $this->load->library('email'); 

    $config['protocol'] = 'smtp';   
    $config['smtp_host'] = 'host.org';    
    $config['smtp_port'] = 'secretport';   
    $config['smtp_user'] = '[email protected]';   
    $config['smtp_pass'] = 'examplepassword';   
    $config['smtp_timeout'] = '100';   
    $config['mailtype']  = 'html';   
    $config['charset']  = 'utf-8';    
    $config['newline']  = "\r\n";  

    if($status == "approved") { 
    $message='your form is approved'; 
    redirect("/response/approved"); 
      }else{ 
    $message='your form is approved'; 
    redirect("/response/rejected"); 
     }   
     $to_email = $this->messages_model->reply($id_user); 

     $this->email->initialize($config); 
     $this->email->from('[email protected]', 'NAME'); 
     $this->email->to($to_email); 
     $this->email->subject('Confirmation Note'); 
     $this->email->message($message); 
    } 

Это обновление базы данных успешно, но нет электронной почты в моем почтовом ящике.

Что-то не в порядке с этим кодом?

ответ

1

Как указано в CI документации для функции переадресации:

Эта функция прекращает выполнение скрипта

Ссылка: Redirect function

Итак, сначала переместите Перенаправление конец функции.

Во-вторых, после того, как вы установили все свойства электронной почты, пользователь $ this-> email-> send() отправит электронное письмо.

Ссылка: Email library

В-третьих, ваше сообщение об отказе неправильно :)

Update на вопрос в комментарии:

$to_email = $this->messages_model->reply($id_user); 
$this->email->initialize($config); 
$this->email->from('[email protected]', 'NAME'); 
$this->email->to($to_email); 
$this->email->subject('Confirmation Note'); 


if ($status == "approved") { 
    $message='your form is approved'; 
    $this->email->message($message); 
    $this->email->send(); 
    redirect("/response/approved"); 
} else { 
    $message='your form is rejected'; 
    $this->email->message($message); 
    $this->email->send(); 
    redirect("/response/rejected"); 
}   
+0

, так что я должен инициализировать два раза внутри функции IF? –

+0

Нет, вы можете инициализировать один раз, перед IF, и только поместите сообщение $ this-> email-> ($ message) и $ this-> email-> send() внутри IF. –

+0

Добавлен пример в ответ –

2

Необходимо поместить перенаправленный код после электронной почты.

 $to_email = $this->messages_model->reply($id_user); 

     $this->email->initialize($config); 
     $this->email->from('[email protected]', 'NAME'); 
     $this->email->to($to_email); 
     $this->email->subject('Confirmation Note'); 
     $this->email->message($message); 
     $this->email->send(); 
     if($status == "approved") { 
       $message='your form is approved'; 
       redirect("/response/approved"); 
     }else{ 
       $message='your form is approved'; 
       redirect("/response/rejected"); 
     } 
+0

Нах, $ сообщение = 'блаблабла'; должны запускаться до сообщения $ this-> email-> ($ message); –

Смежные вопросы