Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;
}
like image 459
Ganesh T Avatar asked Oct 29 '22 01:10

Ganesh T


2 Answers

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.

like image 65
Imran Abdur Rahim Avatar answered Nov 13 '22 17:11

Imran Abdur Rahim


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.

like image 22
Vijay Sharma Avatar answered Nov 13 '22 17:11

Vijay Sharma