简体   繁体   English

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

[英]Sending Email with Codeigniter Taking So much time

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. 每封邮件需要5分钟或更长时间才能到达用户电子邮件。 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. 如果我要发送具有相同功能的电子邮件sendEmail()而不是仅带有简单文本的验证码。 Now it takes 1 minute or 1 n half minutes to reach the user email. 现在需要1分钟或1 n半分钟才能到达用户电子邮件。

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. 我不知道如何使用SMTP发送电子邮件。 I found some examples where they add their domain name to the smtp_host, email, password, which email is created with the domain. 我找到了一些示例,他们在其中将域名添加到smtp_host,电子邮件,密码,该电子邮件是使用域创建的。 I did the same but nothing happens with my email sending. 我做了同样的事情,但是我的电子邮件发送没有任何反应。 It almost same with this also whether I am using SMTP or not. 无论我是否使用SMTP,都与此几乎相同。

This is my function name sendEmail( ) which I have created the model to sending emails. 这是我创建的用于发送电子邮件的模型的函数名称sendEmail( )。 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,\n\n\nPlease click on Given below URL  to verify your Email Address ".$verification_link." \n\n 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...!\n";
            $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' => "\r\n",
    'newline' => "\r\n",
    '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";
    }
}

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. 不会是速度较慢的类,它将是您尝试连接到的SMTP邮件服务器,该服务器发送造成页面滞后的电子邮件。 here are some of my suggestions. 这是我的一些建议。

First of all, create a custom config file email.ph p inside application/config Please make sure this config is autoloaded.Open your Autoload.php inside application/config and write $autoload['config'] = array('email'); 首先,在application/config内创建一个自定义配置文件email.ph p。请确保此配置已自动Autoload.phpapplication/config内打开您的Autoload.php并写入$autoload['config'] = array('email');

In my case I am sending email via webmail id, so here is my email.php 就我而言,我是通过Webmail ID发送电子邮件,所以这是我的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. 如果您遵循此过程,则可能可以节省几秒钟。

try like this in your controller.... 在您的控制器中尝试这样。

public function sendResetEmail($params) { 公共函数sendResetEmail($ params){

        $params['body'] = 'emails/password_reset';
        $params['title'] = 'Forgot Password';
        $params['subject'] = 'Mail From Admin - Reset Password ';
        $params['reset_url'] = base_url() . 'login/reset/?key=' . $params['reset_key'] . '&email=' . $params['email_user'];
        $params['mailtype'] = 'html';
        $this->email->set_mailtype("html");
        $this->email->from('info@example.co.in', 'admin');
        $this->email->to($params['email_user']);
        $this->email->subject($params['subject']);
        $this->email->message($this->load->view('emails/main', $params, true)); 
        $this->email->send();
}

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

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