anchor.diff:  % Timeout value for the connection of the spamassassin daemon
   -define(SPAMD_TIMEOUT, 10000).
   
  +% Max number of e-mails being sent at the same time
  +-define(EMAIL_MAX_SENDING, 30).
  +
  +
   -record(state, {smtp_relay, smtp_relay_opts, smtp_no_mx_lookups,
                   smtp_verp_as_from, smtp_bcc, override, 
                   smtp_spamd_ip, smtp_spamd_port, sending=[],
                   delete_sent_after}).
  --record(email_queue, {id, retry_on=inc_timestamp(now(), 10), retry=0, 
  +-record(email_queue, {id, retry_on=inc_timestamp(now(), 1), retry=0, 
                         recipient, email, created=now(), sent, 
                         pickled_context}).
   
  
positive.code:     QEmail = #email_queue{id=Id,
                            recipient=Recipient,
                            email=Email,
                            pickled_context=z_context:pickle(Context)},
      QEmailTransFun = fun() -> mnesia:write(QEmail) end,
      {atomic, ok} = mnesia:transaction(QEmailTransFun),        
      case Email#email.queue of
          false -> spawn_send(Id, Recipient, Email, Context, State);
          true -> State
      end.
  
  
  spawn_send(Id, Recipient, Email, Context, State) ->
      F = fun() ->
              MessageId = message_id(Id, Context),
              VERP = "<"++bounce_email(MessageId, Context)++">",
  
negative.code:     [ z_notifier:first(#email_failed{message_nr=Id, recipient=Recipient}, z_context:depickle(PickledContext)) 
       || {Id, Recipient, PickledContext} <- NotifyList2 ],
  
      MaxListSize = ?EMAIL_MAX_SENDING - length(State#state.sending), 
      case MaxListSize > 0 of
          true ->
              %% fetch a batch of messages for sending
              FetchTransFun =
                  fun() ->
                          Q = qlc:q([QEmail || QEmail <- mnesia:table(email_queue),
                                     %% Should not already have been sent
                                     QEmail#email_queue.sent == undefined,
                                     %% Should not be currently sending
                                     proplists:get_value(QEmail#email_queue.id, State#state.sending) == undefined,
                                     %% Eligible for retry
                                     timer:now_diff(QEmail#email_queue.retry_on, Now) < 0]),
                          QCursor = qlc:cursor(Q),
                          QFound = qlc:next_answers(QCursor, MaxListSize),
                          ok = qlc:delete_cursor(QCursor),
                          QFound
                  end,
              {atomic, Ms} = mnesia:transaction(FetchTransFun),
              %% send the fetched messages
              case Ms of
                  [] ->
                      State;
                  _  ->
                      State2 = update_config(State),
                      lists:foldl(
                        fun(QEmail, St) ->
                            update_retry(QEmail),
                            spawn_send(QEmail#email_queue.id, 
                                       QEmail#email_queue.recipient,
                                       QEmail#email_queue.email,
                                       z_context:depickle(QEmail#email_queue.pickled_context), 
                                       St)
                        end,
                        State2, Ms)
              end;
          false ->
              State
      end.
  
  
  