简体   繁体   English

使用codeigniter发送电子邮件

[英]sending email using codeigniter

Am trying to send an email in codeigniter. 我正在尝试通过codeigniter发送电子邮件。 I first created an independent function in the model and it's corespondent in the controller and it worked very well. 我首先在模型中创建了一个独立的函数,它是控制器的核心响应者,并且运行良好。 The email was sent and received in both yahoo and gmail. 该电子邮件已通过yahoo和gmail发送和接收。 However when I tried to use the same code in another function where am selection the email address (recipient) from the database, it gives me a message that email successfully sent but it actually doesn't deliver to yahoo or gmail. 但是,当我尝试在从数据库中选择电子邮件地址(收件人)的另一个函数中使用相同的代码时,它给我一条消息,表明电子邮件已成功发送,但实际上并未传递给yahoo或gmail。 what could be the problem? 可能是什么问题呢? It' not even delivering into the spam/bulk mail. 它甚至没有发送到垃圾邮件/大量邮件中。

the codes for the one that worked and that that failed are bellow. 下面是工作正常和失败的代码。

the model function is 模型函数是

function sendMail()
{
   $to = "mymail@yahoo.com"; 
       $subject = "My Sublect"; 
       $messagetext= "stop distubbing me and work!";
       $config=array(
      'protocol'=>'smtp',
      'smtp_host'=>'mail.mydomail.co.ug',
      'smtp_port'=>25,
      'smtp_user'=>'myname@mydomail.co.ug',
      'smtp_pass'=>'mypass'
    );
    $this->load->library("email",$config);
    $this->email->set_newline("\r\n");
    $this->email->from("myname@mydomail.co.ug","Joyce");
    $this->email->to($to); 
    $this->email->subject($subject); 
    $this->email->message($messagetext); 
    if($this->email->send())
    {
        echo "Mail send successfully!";
    }
    else
    {
        show_error($this->email->print_debugger());
    }
}

the controller fcn 控制器fcn

function sendmail()
{
    $this->load->model('customer_model');
    $this->customer_model->sendMail();
}

However the one that failed to work is bellow 然而,失败的是波纹管

function customer()
{
    $this->load->library('email');
    $this->load->database();
    $data = array(
              'name'=>$this->input->post('name'),
              'contact'=>$this->input->post('contact'),
              'entity'=>$this->input->post('entity'),
              'sector'=>$this->input->post('sector'),
              'inquiry'=>$this->input->post('inquiry'),
         'nature_of_inquiry'=>$this->input->post('nature_of_inquiry'),
          'status'=>$this->input->post('status'),

                );
    $this->db->insert('customers',$data);
    $id = $this->db->insert_id();
    $refno = date('d/m/Y').'-C'.str_pad($id, 4, "0", STR_PAD_LEFT);

$this->db->query("UPDATE customers set refno = '".$refno."' WHERE id = '".$id."'"); 

$query=$this->db->query("select entity,name,status,contact from customers where id ='".$id."'");
foreach ($query->result() as $row)
{
    $entity = $row->entity;
    $phone = $row->contact;
    $name = $row->name;
    $status = $row->status;
}   

$query1=$this->db->query("select phone, email from services where entity = '".$entity."'");
     foreach ($query1->result() as $row)
{

    $to = $row->email;
}   
    $sms ="Dear $name, your request/compaint has been $status";
    //$emailtext ="yo company has been refferenced by servicecops.";
   //$this->sendSMS(test,$phone,$sms);
   //$subject = "Servicecops Reminder";
   $config=array(
      'protocol'=>'smtp',
      'smtp_host'=>'mail.mydomain.co.ug',
      'smtp_port'=>25,
      'smtp_user'=>'myname@mydomain.co.ug',
      'smtp_pass'=>'mypass',
      'mailtype'=>'html'
    );
    $this->load->library("email",$config);
    $this->email->set_newline("\r\n");
    $this->email->from("myname@mydomain.co.ug","Joyce");
    $this->email->to($to); 
    $this->email->subject("my subject"); 
    $this->email->message("yo company has been refferenced by me"); 
    if($this->email->send())
    {
        echo "Mail send successfully!";
        echo $to;
    }
    else
    {
        show_error($this->email->print_debugger());
    }

    return $this;
  }

First try to check if you are fetching the emails well from database. 首先尝试检查您是否从数据库中很好地获取了电子邮件。 If yes then check in the spam. 如果是,则检查垃圾邮件。 That would solve the problem I guess. 我猜那可以解决问题。

Guys i have discovered the problem. 伙计们,我发现了问题。 I had defined the email library twice in the same function that's why messages where not reaching yahoo and gmail inbox. 我在同一功能中两次定义了电子邮件库,这就是为什么邮件无法到达yahoo和gmail收件箱的原因。 at the begining of the function, i had put. 在功能开始时,我已经提出了。 $this->load->library('email'); $这个 - >负载>库( '电子邮件'); and this in the midle. 这在中部。 $this->load->library("email",$config); $这个 - >负载>库( “电子邮件”,$配置);

Most of the mails getting spam if you use IP address while running with the URL(ex: 123.117.116.27/test/email/) instead of domain name. 如果您使用IP地址(而不是域名)运行URL(例如123.117.116.27/test/email/),则大多数邮件都会收到垃圾邮件。 So check with spam folder. 因此,请检查垃圾邮件文件夹。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM