Please whitelist cPanel in your adblocker so that you’re able to see our version release promotions, thanks!

The Community Forums

Interact with an entire community of cPanel & WHM users!
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Email sent via PHP mail() is not sent immediately

Discussion in 'E-mail Discussions' started by Reado, Jun 7, 2017.

  1. Reado

    Reado Well-Known Member

    Joined:
    Sep 8, 2009
    Messages:
    184
    Likes Received:
    7
    Trophy Points:
    68
    Location:
    United Kingdom
    cPanel Access Level:
    DataCenter Provider
    My server has a number of PHP-based services that operate using systemd.

    Some of these services send emails using mail() but doing so does not send the email immediately. Instead, the email is queued by Exim. To workaround this, I also have to call shell_exec("/usr/sbin/exim -q -v") after mail() to send the email immediately.

    Recently something must have changed as calling the Exim command above either sends the email straight away or produces an error from Exim stating "Spool file is locked (another process is handling this message)".

    I'm puzzled as to why PHP mail() can't just send email immediately, but even more so now the Exim command has started to play up as well.

    Any ideas what could be causing the problem?

    The code is as follows:

    /etc/systemd/system/EmailTest.service
    Code:
    [Unit]
    Description=Email Test
    After=network.target
    Requires=mysql.service
    
    [Service]
    PIDFile=/var/run/EmailTest.pid
    ExecStart=/usr/local/bin/php -f /home/myuser/emailtest.php
    RestartSec=1m
    Restart=always
    
    [Install]
    WantedBy=multi-user.target

    /home/myuser/emailtest.php
    PHP:
    <?
    $path '/var/run/EmailTest.pid';

    if (!
    file_exists($path))
        
    touch($path);

    $file fopen($path'w');
    fwrite($filegetmypid());
    fclose($file);

    $from    'sender@mydomain';
    $to      'recipient@mydomain';
    $subject 'Test Subject';
    $body    'Test Body';

    $header  'From: Email Test <'.$from.'>'."\r\n";
    $header .= 'Reply-To: Email Test <'.$from.'>'."\r\n";
    $header .= 'X-Priority: 1'."\r\n";
    $header .= 'X-Mailer: Emailer Test'."\r\n";
    $header .= 'MIME-Version: 1.0'."\r\n";
    $header .= 'Content-Type: text/plain; charset=utf-8';

    mail($to$subject$body$header'-f '.$from);
    echo 
    shell_exec('/usr/sbin/exim -q -v');
     
  2. Reado

    Reado Well-Known Member

    Joined:
    Sep 8, 2009
    Messages:
    184
    Likes Received:
    7
    Trophy Points:
    68
    Location:
    United Kingdom
    cPanel Access Level:
    DataCenter Provider
    Sorted! Had to add sleep(1) between mail() and shell_exec(). Now I'm no longer getting the spooler file locked error, but would still like to know what could be causing mail not to send immediately regardless without the additional shell_exec() call, if anyone can suggest anything? Thanks.
     
  3. cPanelMichael

    cPanelMichael Forums Analyst
    Staff Member

    Joined:
    Apr 11, 2011
    Messages:
    38,658
    Likes Received:
    1,425
    Trophy Points:
    363
    cPanel Access Level:
    Root Administrator
    Hello,

    Do you notice any output to /var/log/exim_mainlog when the message is queued?

    Thank you.
     
Loading...

Share This Page