dongmao9217 2017-07-15 05:17 采纳率: 100%
浏览 191
已采纳

使用Codeigniter发送电子邮件需要花费大量时间

Here is my code in which sending an email when user signup. It takes 5 minutes or more for every mail to reaches to the user email. I do not know why email taking so much time to reach. if I am sending an email with this same function sendEmail() not with a verification code only with a simple text. Now it takes 1 minute or 1 n half minutes to reach the user email.

Sometimes it does not even send any email when I am sending the verification code at the end of the link.

I do not know how to send the email with SMTP. I found some examples where they add their domain name to the smtp_host, email, password, which email is created with the domain. I did the same but nothing happens with my email sending. It almost same with this also whether I am using SMTP or not.

This is my function name sendEmail() which I have created the model to sending emails. The reason why I have created this function in the model because I have to send emails from other controllers too. I do not if it could be a problem in sending emails

Please see this function where I am doing wrong. or if there is another way please tell me how to do this.or any type of suggestions will be very helpful for me.

Controller

function index() {
        //my validation rules are here  
        if ($this->form_validation->run() == TRUE) {

            $data = $this->fetch_data_from_post();
            $user_email = $data['email'];
            $code =  random_string('unique');
            $verification_link = base_url('Home/verify/').$code;
            $subject = "Confirmation Message";
            $message = "Dear User,


Please click on Given below URL  to verify your Email Address ".$verification_link." 

 Once you click on the above link then your account will be verified and you will get an opportunity to login. See you soon,Thank You...!
";
            $email_sent = $this->Perfect_mdl->sendEmail($user_email,$subject,$message);

            if($email_sent != 1){
                $flash = '<div class="alert alert-danger">Opppssss Somthing went Wrong...</div>';
                $this->session->set_flashdata('user_registration',$flash);
                redirect('Home/signup');

            }else{
                $this->Perfect_mdl->_insert($data);
                $flash = '<div class="alert alert-success">You are Successfully Registered... Please Check Your Email <b>'.$user_email.'</b> For Verification</div>';
                $this->session->set_flashdata('user_registration',$flash);
                redirect('Home/signup');
            }
        }

        $data['meta_title']  = "Signup";
        $data['services_name'] = $this->Perfect_mdl->getServices_home();
        $dat['flash'] = $this->session->flashdata('user_registration');
        $this->load->view('signup',$data);
    }

Model:-

function sendEmail($useremail,$subject,$message) {


     $config = Array(
    'protocol' => 'smtp',
    'smtp_host' => 'smtp.mydomainname.com',
    'smtp_port' => 25,
    'smtp_user' => 'vishal@mydomainname.com', // change it to yours
    'smtp_pass' => 'vishal123456', // change it to yours
    'mailtype' => 'html',
    'charset' => 'iso-8859-1',
    'crlf' => "
",
    'newline' => "
",
    'wordwrap' => TRUE
    );
    $this->load->library('email', $config); 
    $this->email->from('vishal@mydomainname.com', 'Company Name');
    $this->email->to($useremail);   


    $this->email->subject($subject);
    $this->email->message($message);
    if($this->email->send()){
        return "1";
    }else{
        return "0";
    }
}
  • 写回答

2条回答 默认 最新

  • doumeng1897 2017-07-16 14:11
    关注

    It won't be the class that is slow, it will be the SMTP mail server you are trying to connect to that sends the email that is making the page lag. here are some of my suggestions.

    First of all, create a custom config file email.php inside application/config Please make sure this config is autoloaded.Open your Autoload.php inside application/config and write $autoload['config'] = array('email');

    In my case I am sending email via webmail id, so here is my email.php

    $config = Array(
        'protocol' => 'smtp',
        'smtp_host' => 'SMTP_HOST_NAME',
        'smtp_port' => 25,
        'smtp_user' => 'SMTP_USER_NAME', // change it to your user name
        'smtp_pass' => 'SMTP_PASSWORD', // change it to your password
        'mailtype' => 'html',
        'charset' => 'iso-8859-1',
        'wordwrap' => TRUE
    );
    

    Use parent construct like this:

    function __construct()
    {
      parent::__construct();          
      $this->load->library('email', $config);
    }
    

    And then you can emails easily just be like this:

    $this->email->from('info@example.net', 'Account');
    $this->email->to('johndoe@example.com');
    $this->email->cc('johndoe@example.com');
    $this->email->bcc('johndoe@example.com');
    $this->email->subject('Account Confirmation');
    $message = "any message body you want to send";
    $this->email->message($message);
    $this->email->send();
    

    If you following this procedure then maybe it can save some seconds.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条