Currently, I am implementing the email sending function using Cakephp2. This time we are implementing the email sending function with our own method without using CakeEmail, and we are loading the email template using render.
As a premise, this time we assume that we will deliver up to about 50,000 emails, and we can send the current 300 emails without any noticeable delay. However, up to about 300 items can be sent 5 to 7 per second, but when it reaches about 1000, it will be sent once every 3 to 5 seconds. Therefore, this time, I would like to make sure that I do not reduce the transmission speed of each mail.
Therefore, when checking the processing speed of each process, it was confirmed that the processing speed in the part executing render gradually increased, so the memory usage at the time of rendering may be a problem? I thought.
Please let me know if there is a cause or solution that increases memory usage by looping render.
function send()
{
$mails = $this->MailList->getSendMails();
foreach ($mails as $mail) {
if (!$this->saveTargetData($mail)) {
continue;
};
$limit = 300;
$offset = 0;
while ($targets = $this->MailDeliveryTarget->getTargets($mail,$limit,$offset)) {
foreach ($targets as $target) {
$result = $this->Sys->sendMailTemplate('reserved_mail',['input' => $input]);
if ($result) {
// success
} else {
// failure
}
}
unset($targets);
$offset += $limit;
}
}
}
function sendMailTemplate($tpl, $data = array())
{
$to = $this->getMailTo($tpl, $data);
$from = $this->getMailFrom($tpl, $data);
return $this->sendMail($to, $from, $this->getMailSubject($tpl, $data), $this->getMailBody($tpl, $data));
}
function getMailTo($tpl, $data)
{
return trim(convert_nl($this->_getMailTemplate($tpl."/to",$data), ""));
}
function _getMailTemplate($tpl, $data)
{
if (!is_file(dirname(dirname(dirname(__FILE__))).DS."Template".DS."mail".DS.str_replace("/", DS, $tpl).".txt")) {
return "";
}
$bk = array();
foreach (array("layout", "ext") as $v) {
$bk[$v] = $this->Controller->$v;
}
$bk_output = $this->Controller->response->body();
$this->Controller->layout = "";
$this->Controller->ext = ".txt";
$this->Controller->response->body("");
$this->Controller->set($data);
$this->Controller->mail_render = true;
$this->Controller->render("/".ltrim($tpl, "/"));
$out = $this->Controller->response->body();
$this->Controller->mail_render = false;
$this->Controller->response->body($bk_output);
foreach (array("layout", "ext") as $v) {
$this->Controller->$v = $bk[$v];
}
return $out;
}
Thank you.