-
Notifications
You must be signed in to change notification settings - Fork 3
/
custom-sendmail-routine
100 lines (77 loc) · 2.69 KB
/
custom-sendmail-routine
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<chapter id="DefineCustomSendmailRoutine">
<chapterinfo>
<title>Define Custom Sendmail Routine</title>
<titleabbrev>customsendmail</titleabbrev>
<keywordset>
<keyword>custom</keyword>
<keyword>send</keyword>
<keyword>mail</keyword>
<keyword>routine</keyword>
<keyword>delay</keyword>
</keywordset>
<authorgroup>
<author>
<firstname>Mike</firstname>
<surname>Heins</surname>
<affiliation>
<email>mheins@perusion.net</email>
</affiliation>
</author>
</authorgroup>
</chapterinfo>
<sect1 id='customsendmail_introduction'>
<title>Introduction</title>
<para>
HOW-TO version: $Id: custom-sendmail-routine,v 1.3 2004-11-12 22:26:26 docelic Exp $
</para>
<para>
Someone <ulink url="http://www.icdevgroup.org/pipermail/interchange-users/2004-July/039811.html">was wondering</ulink> how to optimize the order processing on a busy site. It was observed that about once in every ten times, the <filename>etc/mail_receipt</filename> takes a few <emphasis>extra</emphasis> seconds before it's mailed out.
</para>
<para>
The delay occured because in the code used, the <option>SendMailProgram</option> ran in foreground so the ordering process "stalled" for a few seconds (or at least that's how the average customer saw it). The critical section was reduced to:
</para>
<screen>
<![CDATA[
[email to="[scratch to_email], __MAIL_RECEIPT_CC__"
subject="__COMPANY__ Order #[value mv_order_number]: [scratch subject_end]"
from=|"__COMPANY__ Order Confirmation" <orders at company.com>| ]
... email contents ...
[/email]
]]>
</screen>
</sect1>
<sect1 id='customsendmail_solution'>
<title>Solution</title>
<para>
To eliminate this "delay", you could use a custom script that accepts the message on standard input (STDIN), and then calls <application>Sendmail</application> in the background.
</para> <para>
Save the script with the following contents to <filename>/usr/local/bin/sendmail-bg</filename>:
</para>
<programlisting>
<![CDATA[
#!/usr/bin/perl
#use strict;
#use warnings;
use File::Temp;
my $basedir = '/tmp/sendmail';
my $sendmail = '/usr/sbin/sendmail -t';
umask 2;
mkdir $basedir unless -d $basedir;
my $tmp = File::Temp->new( DIR => $basedir );
my $tmpnam = $tmp->filename;
open OUT, "> $tmpnam" or die "Cannot create $tmpnam: $!\n";
my $cmdline = join " ", $sendmail, '<', $tmpnam, '&';
while(<>) { print OUT $_; }
close OUT;
system($cmdline);
if($?) { die "Failed to fork sendmail: $!\n" }
]]>
</programlisting>
<para>
And of course, don't forget to add/modify the <option>SendMailProgram</option> directive:
</para>
<programlisting>
SendMailProgram /usr/local/bin/sendmail-bg
</programlisting>
</sect1>
</chapter>