开发者

How to solve the send mail slow in codeIgniter?

I am using the default codeIgniter mail class, which is very slow for me to send the email, it takes me up to 3 seconds to from start to finish. Can I do something to make the user think it is faster. I know that sending email takes time, but can I show the result to the user开发者_如何学运维 first, and send to later or something other workaround? Thanks.


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.

Assuming it's a form of some kind you could submit it using AJAX and have the receiving script send the email. Alternatively you could try using PHP mail rather than SMTP.


I've never seen a performance problem reported in relation to php's mail() function which was not a problem in the MTA (and hence nothing to do with PHP).

An SMTP system (MTA) is all about managing email queues. If it's not managing to enqueue messages promptly then it's failing in its primary purpose.

But you've provided no information about what MTA you are using, how it is configured, what OS the PHP is running on, nor how the mail interface is configured in PHP.

Add to that the fact that the standard codeigniter mail class can use the PHP mail() function, or a direct OS call to a SMTP mailer or a network call to the SMTP port/server, without knowing what the code is doing its rather difficult to understand what the problem is.

Check your webserver can resolve the SMTP host (promptly) if you're using an SMTP port connection from your code. Otherwise it's a problem with the MTA.


First of all create a custom config file

email.php inside application/config

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 yours
    'smtp_pass' => 'SMTP_PASSWORD', // change it to yours
    'mailtype' => 'html',
    'charset' => 'iso-8859-1',
    'wordwrap' => TRUE
);

Then make sure this config is autoloaded.Open your Autoload.php inside application/config and write

$autoload['config'] = array('email');

Now whenever you create a controller that has many methods using email library.use parent contruct

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

}

And then you can mails easily just be

$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();

This reduces 2-3 seconds when sending mails via CI email library. Thanks

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜