第一次失败时重试发送电子邮件 - codeigniter

时间:2018-04-23 20:56:08

标签: php codeigniter email

我有这个代码可以发送电子邮件通知并且几乎一直在工作,但在某些情况下,电子邮件会失败,我想知道是否有办法在第一次失败时重试发送电子邮件:

这是系统在电子邮件失败时显示的错误:

  

SSL:握手超时

这是我的代码:

public function insertar_exportacion()

{
    $insertstatus=$this->exportaciones_model->nuevoExportacion();
    if($insertstatus)
  {
    $data['query'] = $this->exportaciones_model->obtenerDatosInsertar($insertstatus);

    //print_r($data);
    $config['mailtype'] = 'html';
    $htmlContent = $this->load->view('emailExpo_view',$data,true);

    $this->email->initialize($config);
    $this->email->set_crlf( "\r\n" );
    $this->email->to('email@email.com');
    $this->email->from('from.email@email.com');
    $this->email->cc('cc.email@email.com');
    $this->email->bcc('bcc.email@email.com');
    $this->email->subject('Actualización de Exportación');
    $this->email->attach(FCPATH . "imagenes/cari.png", "inline");
    $this->email->message($htmlContent);
    $this->email->send();

  }

}

这是codeigniter email.php中的确认:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

$config['protocol'] = "smtp";
$config['smtp_host'] = "smtp.host.com";
$config['smtp_port'] = "25";
$config['smtp_user'] = "username@mail.com";
$config['smtp_pass'] = "Password";
$config['charset'] = "utf-8";
$config['mailtype'] = "html";
$config['newline'] = "\r\n"; 

如果您知道如何在codeigniter中重新发送电子邮件失败,请告诉我,谢谢!

2 个答案:

答案 0 :(得分:0)

放入try / catch

try {
    $obj->insertar_exportacion();
} catch(\Exception $e) {
    $obj->insertar_exportacion();
}

或者更好的是,将try / catch放在你的函数中,并多次调用你想要的东西:

public function insertar_exportacion()
{
    try {
        //logic
        $insertstatus=$this->exportaciones_model->nuevoExportacion();
        ...
        $this->email->send();
        ...
    } catch(\Exception $e) {
        //logic to when an error occur
    }
}

for ($i=0; $i< $numberOfTries; $i++) {
    $obj->insertar_exportacion();
}

答案 1 :(得分:0)

根据文档,send()如果成功则返回true,如果不成功则返回false。

if (!$this->email->send()) {
  $this->email->send();
}

Link to docs