Vishal Kumar
Vishal Kumar

Reputation: 262

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. 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,\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' => '[email protected]', // 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('[email protected]', 'Company Name');
    $this->email->to($useremail);   


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

Upvotes: 2

Views: 3152

Answers (2)

always-a-learner
always-a-learner

Reputation: 3794

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('[email protected]', 'Account');
$this->email->to('[email protected]');
$this->email->cc('[email protected]');
$this->email->bcc('[email protected]');
$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.

Upvotes: 1

Mahesh
Mahesh

Reputation: 93

try like this in your controller....

public function 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('[email protected]', '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();
}

Upvotes: 1

Related Questions