Ganesh T
Ganesh T

Reputation: 81

Delay due to email send PHP CodeIgniter

First of all, I would like to thank StackOVerflow and its users for helping me solve a number of technical problems. I created this account today since I could not see similar problem being addressed.

The problem - I am stuck with a problem with sending emails in PHP CodeIgniter. The function $this->email->send() is taking around 3-4 seconds each time to execute.

We have a social platform where users come and post things. Every time the user uploads a new update we want to send an email to him/her. The problem is the send email function takes around 3-4 seconds and we would love to bring it down under 1 second.

IS there any way wherein we can execute the send email process in parallel ? Say a python code which runs continuously to fetch new updates and send emails to those users. Any better method than that ?

Technology stack - PHP CI, MySQL, Apache, Windows

This is the email code that gets called on every new update -

$config = Array(
        'protocol'  => 'smtp',
        'smtp_host' => 'smtp.gmail.com',
        'smtp_port' => 587,
        'smtp_user' => '[email protected]',
        'smtp_pass' => 'password',
        'mailtype'  => 'html',
        'charset'   => 'utf-8',
        'smtp_crypto' => "tls"
        );

$this->load->library('email', $config);
$this->email->set_newline("\r\n");
$this->email->set_mailtype("html");
$this->email->from('[email protected]', 'Support');
$this->email->to($this->session->userdata['email']);
$this->email->subject('You have posted a new update');
$this->email->message('Please login to check your new update. Do not reply to this email. For any issues reach out to [email protected]');

if (!$this->email->send())
{
    show_error($this->email->print_debugger());
}
else
{
    echo true;
}

Upvotes: 5

Views: 2196

Answers (2)

Vijay Sharma
Vijay Sharma

Reputation: 831

It's my suggestion to store the emails in a database and send the emails using a cron job.
The uploading user should not wait to send emails one at a time .

If you need code for a cron job or an smtp mailer, add a comment to this answer.

Upvotes: 1

Imran Abdur Rahim
Imran Abdur Rahim

Reputation: 417

If your function is working faster but email sending is slower, it must be problem for your smtp connection. Either your server is not much faster to connect SMTP quickly or the SMTP you are using bit slower to response. In that case, try with a gmail SMTP and see the result.

Upvotes: 1

Related Questions