3

I am using sSMTP with PHP for e-mail sending and it works fine.

The problem is that sSMTP performs the delivery synchronously which adds latency to my PHP scripts, letting the user waiting a little too long.

Is there a way to make it work non-blocking?

(I though of a hack on the shell, to start the sSMTP process on the background or something, and return earlier back to PHP, but I could not make it work.)

raugfer
  • 221
  • 1
  • 2
  • 5

4 Answers4

3

Yes use one of the nullmailers which supports queueing or a full MTA instead of ssmtp. While you could write a wrapper around SSMTP, it'll need to fork and setsid to isolate from the calling php process.

symcbean
  • 21,009
  • 1
  • 31
  • 52
3

This works with sendmail piping. Here is an example of what the data that gets piped looks like

To: to@to.com
Subject: Your Subject Here
X-PHP-Originating-Script: 0:MailSender.class.php
MIME-Version: 1.0
Content-type: text/html; charset=iso-8859-1
From: First Last <noreply@cool.com>

BODY OF EMAIL GOES HERE

Save this script as /usr/sbin/sendmail

#!/bin/bash
# sendmail wrapper for ssmtp to send email asynchronously

TMP=`mktemp`
stdin=$(cat)
echo "$stdin" > $TMP
body=`cat $TMP`

# Create the email and store in file
echo "ssmtp -t <<EOF " > $TMP
echo "$body" >> $TMP
echo "EOF" >> $TMP
echo "rm -f $TMP" >> $TMP

# Send the email
sh $TMP &
masegaloeh
  • 18,236
  • 10
  • 57
  • 106
Mike
  • 31
  • 1
1

You can use gearman or other similar techniques do make blocking things asynchronously. See http://www.phpclasses.org/blog/post/108-Distributing-PHP-processing-with-Gearman.html

DukeLion
  • 3,259
  • 1
  • 18
  • 19
0

Ok, I figured it out! Using the script command we can write a wrapper:

#!/bin/sh
script -q -c "/usr/sbin/ssmtp $*" /dev/null
raugfer
  • 221
  • 1
  • 2
  • 5