Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


add a 1000 ms delay after each sending email operation

Posted on 2016-10-25
Medium Priority
Last Modified: 2016-11-09

My Sendmail SMTP-relay experiencing problem as user sends email to over 70 users.  the error message on sendmail log is (Sending error 421 4.7.0 Temporary System Problem), however the return fail message is (reason: 530 5.7.0 Must issue a STARTTLS command first).  

About 60 users get the email out of 70,  I don't know what is the problem.   I browsed internet, some peole suggest to delay sending emails by 1000ms between each email.  some other indcates the problem is with (using plain autentication).  I basically don't know how to fix this:

Using following services:
SMTP-relay: installed on Linux system
user send email through SMTP-relay installed on the same server.

if you require other information please let me know.

your help is highly appreciated


Question by:Rawand Amin
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 3
  • +2
LVL 80

Expert Comment

ID: 41859794
From which system are you getting this error?
When do you get this message?
If this is within PHP, why are you using the SMTP method within PHP versus using the direct injection method?
Do not define SMTP within php.ini but use sendmail_path=/usr/sbin/sendmail -oi -t?

This will have PHP submitting the message to sendmail without the SMTP interaction.

This is similar to
echo "To: <emailaddress>
From: <sender>
Subject: direct injection message

This is the message Body(message)
" | /usr/sbin/sendmail -oi -t

the -t tells sendmail to look for the destination of the message within the data TO, Cc and Bcc (Bcc will be stripped after processing.

If the error is within the sendmail log, a 4xx error means that sendmail will retry sending this message at a later time.
Double check what your queue lifetime is within sendmail, to make sure it is at least one day which means it will attempt to transmit messages to which it gets 4xx (temporary error) for the queue lifetime before giving up and treating the temporary error as a permanent one.
LVL 111

Expert Comment

by:Ray Paseur
ID: 41859917
Are you using PHP for this?  If so, please show us the PHP scripts that are in play.  The SSCCE is an incredibly important concept when you're trying to get help with any problem.

If you want to make a PHP script pause for 1000ms, run this instruction: sleep(1);

Author Comment

by:Rawand Amin
ID: 41860071
First of all, I would like to thank you for your help.

Attached is the php file that i believe the system uses to send email.

Allow me to provide you with more information about what I am using:
Moodle service used in our university to support students and facutlies.  Moodle uses its own PHP file to send email through Sendmail (SMTP-Rela) hosted on the same network but different server.  therefore we do not touch the php.ini file.

the only configuration i make on Moodle side is to tell moodle to send email through our SMTP-Relay.
There are other files named as follow:

Error message appear on Linux SMTP-relay server (Sending error 421 4.7.0 Temporary System Problem),

Sent to web administrator automatically from Gmail (no-reply@ourdomain.) (reason: 530 5.7.0 Must issue a STARTTLS command first).
full detail:
From: Mail Delivery Subsystem <no-reply@ourdomain> [u]Gmail server[/u]
Date: Sun, Oct 16, 2016 at 9:02 PM
Subject: Returned mail: see transcript for details
To: webadmin@gmaildomain

The original message was received at Sun, 16 Oct 2016 13:30:06 +0300
from moodle.ourdomain [SERVERIP]

   ----- The following addresses had permanent fatal errors -----
    (reason: 530 5.7.0 Must issue a STARTTLS command first. qq7sm4465619wjc.30 - gsmtp)

Open in new window

eventually the message has not received to end user.
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 111

Expert Comment

by:Ray Paseur
ID: 41860131
STARTTLS is an extension to plain text communication protocols, which offers a way to upgrade a plain text connection to an encrypted (TLS or SSL) connection instead of using a separate port for encrypted communication.


You may need to add ssl:// as the protocol

Even if you're not using GroupMail, this link may show us a good idea.

This also looks like something easy to try:
LVL 59

Expert Comment

by:Julian Hansen
ID: 41860461
If you use sleep() also look at set_time_limit() to avoid your script timing out.

The rationale behind the 1000ms delay is that some ISP's have a throttle on how many emails you can send in a time period - if you exceed that they block you.

Have you considered outsourcing sending of emails to a bulk email provider. They handle all of this for you.
LVL 80

Expert Comment

ID: 41860572
The confusing thing you say you have an SMTP-relay, to which server is this SMTP-RELAY, relays?
The error you have deals with an expected mechanism.
Often, smtp-relay means that your server handles the message and then sends it through to you ISP to handle the disposition.

Your relay might be misconfigured to direct the email to a server that is not the public destination of .... and thus requires additional security features such that you would need to configure your SMTP server, sendmail to handle outgoing message.
i.e. you configured your smtp-relay to go through a specific server including an off port (not 25).......
LVL 111

Accepted Solution

Ray Paseur earned 2000 total points
ID: 41860696
I think the whole sleep() thing is a red herring.  My guess is the problem lies elsewhere, maybe in the permanent fatal error from StartTLS.  And besides, sleep() time is not added to execution time, as this script demonstrates.
<?php // sleeper.php
 * https://www.experts-exchange.com/questions/28978924/add-a-1000-ms-delay-after-each-sending-email-operation.html#a41860461
 * http://php.net/manual/en/function.sleep.php#33732
 * http://php.net/manual/en/function.set-time-limit.php
 * http://php.net/manual/en/function.getrusage.php

function resource_usage_time($ru_alpha, $ru_omega, $index="utime")
    $a = $ru_alpha["ru_$index.tv_sec"]*1000 + round($ru_alpha["ru_$index.tv_usec"]/1000);
    $z = $ru_omega["ru_$index.tv_sec"]*1000 + round($ru_omega["ru_$index.tv_usec"]/1000);
    return $z - $a;

echo '<pre>';

$counter = 60;
$seconds = ini_get('max_execution_time');

$ru_alpha = getrusage();

while($counter > 0)
    echo PHP_EOL . "$counter iterations left";
$ru_omega = getrusage();

$ms = resource_usage_time($ru_alpha, $ru_omega) ;
echo PHP_EOL . "This process used {$ms}ms out of its limit of $seconds seconds for its computations";

Open in new window

LVL 27

Expert Comment

ID: 41861991
i don't believe the sleep would help with these errors.

actually having these errors is inconsistent with the fact that some of the email manages through, except if you're doing some weird stuff such as manually relaying to gmail's submission servers rather than the regular smtp...

if you're relaying through sendmail/postfix/exim or whatever relay you installed locally, it's logs would be welcome

Author Comment

by:Rawand Amin
ID: 41862335
I have taken Ray Paseur into concentration, and re-configure SSL certification on the same server, as highlighted below.  I am waiting for someone to send bunch of and have a result.

I you know any command in linux sendmail that would enable me to send a big number of email to test the system please let me know.

define(`SMART_HOST', `smtp.gmail.com')dnl
define(`RELAY_MAILER_ARGS', `TCP $h 587')dnl
define(`ESMTP_MAILER_ARGS', `TCP $h 587')dnl

define(`CERT_DIR', `/etc/mail/certs')dnl
define(`confCACERT_PATH', `CERT_DIR')dnl
define(`confCACERT', `CERT_DIR/ca-bundle.crt')dnl
define(`confCRL', `CERT_DIR/ca-bundle.crt')dnl
define(`confSERVER_CERT', `CERT_DIR/sendmail.pem')dnl
define(`confSERVER_KEY', `CERT_DIR/sendmail.pem')dnl
define(`confCLIENT_CERT', `CERT_DIR/sendmail.pem')dnl
define(`confCLIENT_KEY', `CERT_DIR/sendmail.pem')dnl
dnl #define(`confAUTH_OPTIONS', `A p')dnl

Open in new window

Thanks all for your help
LVL 80

Expert Comment

ID: 41862386
Sendmail need not be enable to send massive amount of emails. You are configuring your sendmail as to relay through port 587 which is configured with different requirements. The additions you made enable features on your site, it does not force your sendmail client side interaction with gmail to STARTTLS during the connection as the error clearly states is the reason for the message exchange failure.

Do you have your outgoing port 25 blocked? If so, check with your ISP as to their Mail server through which you would configure your sendmail server to relay.
instead of smtp.gmail.com you would have smtp.myispprovider.com .......
LVL 27

Expert Comment

ID: 41862563
seems good. if you don't see errors any more, we can probably assume that the previous config led to errors but sendmail was able to interpret them and send the mail nevertheless. ddoes not matter anyway if you don't have them.

beware that gmail as most smtp providers applies rather strict policies regarding outgoing email and may decide to reject or discard yours very soon if you send more than a few dozens a day, no mentioning content filtering. you may consider other setups. if port 25 is not blocked and your ip range belongs to you or is configured with proper ptrs, you can use direct delivery.

as far as gmail goes, you can throttle delivery in sendmail. use rules such as do not send more than 1 mail per minute and no more than 20 per hour... note that i used arbitrary values that should work with gmail ( or would recently ) but i don't know ( and gmail does not communicate them ) the actual rules in use ; and they are subject to change without warning.

Author Closing Comment

by:Rawand Amin
ID: 41880116

Featured Post

Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

These days socially coordinated efforts have turned into a critical requirement for enterprises.
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question