File: plusemail.cpp

package info (click to toggle)
tinymux 2.10.1.14-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 6,212 kB
  • ctags: 8,535
  • sloc: cpp: 111,587; sh: 5,867; ansic: 141; makefile: 139
file content (732 lines) | stat: -rw-r--r-- 19,517 bytes parent folder | download | duplicates (2)
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
/*! \file plusemail.cpp
 * \brief Quicky module implementing Firan-like \@email function.
 *
 * $Id: plusemail.cpp 5538 2012-03-17 17:32:28Z brazilofmux $
 *
 * \author Rachel Blackman <sparks@noderunner.net>
 *
 * \verbatim
 * Config options:
 *     mail_server               SMTP server to use (mail.bar.com)
 *     mail_ehlo                 ehlo server (my.baz.com)
 *     mail_sendaddr             return address (foo@bar.com)
 *     mail_sendname             RFC822 'name' (FooMUSH)
 *     mail_subject              Defaults subject link (FooMUSH Mail)
 * \endverbatim
 */

#include "autoconf.h"
#include "config.h"
#include "externs.h"
#include "interface.h"
#include "mathutil.h"

#include "_build.h"

// Encode mail body such that CRLF.CRLF only appears at the end of the
// message.  Here are some example transforms.
//
//   <CR><LF>.<anytext><CR><LF> --> <CR><LF>..<CR><LF>
//   <BOM>.<anytext><CR><LF>    --> <BOM>..<anytext><CR><LF>
//   <anytext><EOM>             --> <anytext><CR><LF>.<CR><LF>
//   <anytext><CR><LF><EOM>     --> <anytext><CR><LF>.<CR><LF>
//
// <CR> and <LF> are only allowed to occur as the <CR><LF> pair -- never alone.
//
// Code 0 - Any byte.
// Code 1 - NUL  (0x00)
// Code 2 - LF   (0x0A)
// Code 3 - CR   (0x0D)
// Code 4 - '.'  (0x2E)
// Code 5 - non-ASCII
//
static const UTF8 BodyClasses[256] =
{
//  0  1  2  3  4  5  6  7  8  9  A  B  C  D  E  F
//
    1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 3, 0, 0, // 0
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 1
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0, // 2
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 3
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 4
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 5
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 6
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 7
    5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, // 8
    5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, // 9
    5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, // A
    5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, // B
    5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, // C
    5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, // D
    5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, // E
    5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5  // F
};

#define STATE_NOTHING          0
#define STATE_BOM              1
#define STATE_HAVE_CR          2
#define STATE_HAVE_CRLF        3
#define STATE_HAVE_CRLF_DOT    4
#define STATE_HAVE_CRLF_DOT_CR 5
#define STATE_HAVE_DOT         6
#define STATE_HAVE_DOT_CR      7

// Action  0 - Remain in current state.
// Action  1 - Emit ch and remain in current state.
// Action  2 - Emit ch and move to STATE_NOTHING.
// Action  3 - Emit CRLF.CRLF and terminate.
// Action  4 - Move to STATE_HAVE_CR.
// Action  5 - Move to STATE_HAVE_DOT.
// Action  6 - Move to STATE_HAVE_CRLF.
// Action  7 - Emit CRLF+ch and move to STATE_NOTHING.
// Action  8 - Emit CRLF and move to STATE_HAVE_CR.
// Action  9 - Move to STATE_HAVE_CRLF_DOT.
// Action 10 - Emit CRLF..+ch and move to STATE_NOTHING.
// Action 11 - Emit CRLF..CRLF.CRLF and terminate.
// Action 12 - Move to STATE_HAVE_CRLF_DOT_CR.
// Action 13 - Emit CRLF.. and move to STATE_HAVE_CRLF.
// Action 14 - Emit ..+ch and move to STATE_NOTHING.
// Action 15 - Emit ..CRLF.CRLF and terminate.
// Action 16 - Move to STATE_HAVE_DOT_CR.
// Action 17 - Emit .. and move to STATE_HAVE_CRLF.
//

static const int BodyActions[8][6] =
{
//    Any  '\0' LF   CR   '.'  Non
    {  1,   3,   0,   4,   1,   0}, // STATE_NOTHING
    {  2,   3,   0,   4,   5,   0}, // STATE_BOM
    {  2,   3,   6,   0,   5,   0}, // STATE_HAVE_CR
    {  7,   3,   0,   8,   9,   0}, // STATE_HAVE_CRLF
    { 10,  11,   0,  12,  10,   0}, // STATE_HAVE_CRLF_DOT
    { 10,  11,  13,   0,  10,   0}, // STATE_HAVE_CRLF_DOT_CR
    { 14,  15,   0,  16,  14,   0}, // STATE_HAVE_DOT
    { 14,  15,  17,   0,  14,   0}  // STATE_HAVE_DOT_CR
};

UTF8 *EncodeBody(UTF8 *pBody)
{
    static UTF8 buf[2*LBUF_SIZE];
    UTF8 *bp = buf;

    int iState = STATE_BOM;
    for (;;)
    {
        UTF8 ch = *pBody++;
        int iAction = BodyActions[iState][BodyClasses[(unsigned char)ch]];
        switch (iAction)
        {
        case 0:
            // Action 0 - Remain in current state.
            //
            break;

        case 1:
            // Action 1 - Emit ch and remain in current state.
            //
            *bp++ = ch;
            break;

        case 2:
            // Action 2 - Emit ch and move to STATE_NOTHING.
            //
            *bp++ = ch;
            iState = STATE_NOTHING;
            break;

        case 3:
            // Action 3 - Emit CRLF.CRLF and terminate.
            //
            *bp++ = '\r';
            *bp++ = '\n';
            *bp++ = '.';
            *bp++ = '\r';
            *bp++ = '\n';
            *bp++ = '\0';
            return buf;
            break;

        case 4:
            // Action 4 - Move to STATE_HAVE_CR.
            //
            iState = STATE_HAVE_CR;
            break;

        case 5:
            // Action 5 - Move to STATE_HAVE_DOT.
            //
            iState = STATE_HAVE_DOT;
            break;

        case 6:
            // Action 6 - Move to STATE_HAVE_CRLF.
            //
            iState = STATE_HAVE_CRLF;
            break;

        case 7:
            // Action 7 - Emit CRLF+ch and move to STATE_NOTHING.
            //
            *bp++ = '\r';
            *bp++ = '\n';
            *bp++ = ch;
            iState = STATE_NOTHING;
            break;

        case 8:
            // Action 8 - Emit CRLF and move to STATE_HAVE_CR.
            //
            *bp++ = '\r';
            *bp++ = '\n';
            iState = STATE_HAVE_CR;
            break;

        case 9:
            // Action 9 - Move to STATE_HAVE_CRLF_DOT.
            //
            iState = STATE_HAVE_CRLF_DOT;
            break;

        case 10:
            // Action 10 - Emit CRLF..+ch and move to STATE_NOTHING.
            //
            *bp++ = '\r';
            *bp++ = '\n';
            *bp++ = '.';
            *bp++ = '.';
            *bp++ = ch;
            iState = STATE_NOTHING;
            break;

        case 11:
            // Action 11 - Emit CRLF..CRLF.CRLF and terminate.
            //
            *bp++ = '\r';
            *bp++ = '\n';
            *bp++ = '.';
            *bp++ = '.';
            *bp++ = '\r';
            *bp++ = '\n';
            *bp++ = '.';
            *bp++ = '\r';
            *bp++ = '\n';
            *bp++ = '\0';
            return buf;
            break;

        case 12:
            // Action 12 - Move to STATE_HAVE_CRLF_DOT_CR.
            //
            iState = STATE_HAVE_CRLF_DOT_CR;
            break;

        case 13:
            // Action 13 - Emit CRLF.. and move to STATE_HAVE_CRLF.
            //
            *bp++ = '\r';
            *bp++ = '\n';
            *bp++ = '.';
            *bp++ = '.';
            iState = STATE_HAVE_CRLF;
            break;

        case 14:
            // Action 14 - Emit ..+ch and move to STATE_NOTHING.
            //
            *bp++ = '.';
            *bp++ = '.';
            *bp++ = ch;
            iState = STATE_NOTHING;
            break;

        case 15:
            // Action 15 - Emit ..CRLF.CRLF and terminate.
            //
            *bp++ = '.';
            *bp++ = '.';
            *bp++ = '\r';
            *bp++ = '\n';
            *bp++ = '.';
            *bp++ = '\r';
            *bp++ = '\n';
            *bp++ = '\0';
            return buf;
            break;

        case 16:
            // Action 16 - Move to STATE_HAVE_DOT_CR.
            //
            iState = STATE_HAVE_DOT_CR;
            break;

        case 17:
            // Action 17 - Emit .. and move to STATE_HAVE_CRLF.
            //
            *bp++ = '.';
            *bp++ = '.';
            iState = STATE_HAVE_CRLF;
            break;
        }
    }
}

// Transform CRLF runs to a space.
//
UTF8 *ConvertCRLFtoSpace(const UTF8 *pString)
{
    static UTF8 buf[LBUF_SIZE];
    UTF8 *bp = buf;

    // Skip any leading CRLF as well as non-ASCII.
    //
    while (  '\r' == *pString
          || '\n' == *pString
          || (0x80 & *pString) == 0x80)
    {
        pString++;
    }

    bool bFirst = true;
    while (*pString)
    {
        if (!bFirst)
        {
            safe_chr(' ', buf, &bp);
        }
        else
        {
            bFirst = false;
        }

        while (  *pString
              && '\r' != *pString
              && '\n' != *pString
              && (0x80 & *pString) == 0x00)
        {
            safe_chr(*pString, buf, &bp);
            pString++;
        }

        // Skip any CRLF.
        //
        while (  '\r' == *pString
              || '\n' == *pString
              || 0x80 == (0x80 & *pString))
        {
            pString++;
        }
    }
    *bp = '\0';
    return buf;
}

// Write a formatted string to a socket.
//
static int DCL_CDECL mod_email_sock_printf(SOCKET sock, const UTF8 *format, ...)
{
    va_list vargs;
    UTF8 mybuf[2*LBUF_SIZE];

    if (IS_INVALID_SOCKET(sock))
    {
        return 0;
    }

    va_start(vargs, format);
    mux_vsnprintf(mybuf, sizeof(mybuf), format, vargs);
    va_end(vargs);

    return SOCKET_WRITE(sock, (char *)&mybuf[0], strlen((char *)mybuf), 0);
}

// Read a line of input from the socket.
//
static int mod_email_sock_readline(SOCKET sock, UTF8 *buffer, int maxlen)
{
    buffer[0] = '\0';

    if (IS_INVALID_SOCKET(sock))
    {
        return 0;
    }

    fd_set read_fds;
    FD_ZERO(&read_fds);
    FD_SET(sock, &read_fds);

    // Wait up to 1 second.
    //
    struct timeval tv;
    tv.tv_sec  = 1;
    tv.tv_usec = 0;

    // Check for data before giving up.
    //
    if (IS_SOCKET_ERROR(select(static_cast<int>(sock+1), &read_fds, NULL, NULL, &tv)))
    {
        return 0;
    }

    if (!FD_ISSET(sock, &read_fds))
    {
        return 0;
    }

    bool done = false;
    bool possible_close = false;
    int  pos = 0;

    while (  !done
          && pos < maxlen)
    {
        UTF8 getme[2];

        int numread = SOCKET_READ(sock, (char *)&getme[0], 1, 0);
        if (  IS_SOCKET_ERROR(numread)
           || 0 == numread)
        {
            if (possible_close)
            {
                done = true;
            }
            else
            {
                FD_ZERO(&read_fds);
                FD_SET(sock, &read_fds);

                // Wait up to 1 second.
                //
                tv.tv_sec  = 1;
                tv.tv_usec = 0;

                // Check for data before giving up.
                //
                if (IS_SOCKET_ERROR(select(static_cast<int>(sock+1), &read_fds, NULL, NULL, &tv)))
                {
                    done = true;
                }

                if (FD_ISSET(sock, &read_fds))
                {
                    possible_close = true;
                }
            }
        }
        else
        {
            possible_close = false;
            if (getme[0] != '\n')
            {
                buffer[pos++] = getme[0];
            }
            else
            {
                done = true;
            }
        }
    }
    buffer[pos] = '\0';

    return pos;
}

// Open a socket to a specific host/port.
//
static int mod_email_sock_open(const UTF8 *conhostname, u_short port, SOCKET *sock)
{
    int cc = -1;

    // Let getaddrinfo() fill out the sockaddr structure for us.
    //
    MUX_ADDRINFO hints;
    memset(&hints, 0, sizeof(hints));
    hints.ai_family   = AF_UNSPEC;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_protocol = IPPROTO_TCP;
    hints.ai_flags = AI_ADDRCONFIG;

    UTF8 sPort[SBUF_SIZE];
    mux_ltoa(port, sPort);

    MUX_ADDRINFO *servinfo;
    if (0 == mux_getaddrinfo(conhostname, sPort, &hints, &servinfo))
    {
        for (MUX_ADDRINFO *p = servinfo; NULL != p; p = p->ai_next)
        {
            cc = -2;
            SOCKET mysock = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
            if (0 == connect(mysock, p->ai_addr, p->ai_addrlen))
            {
                *sock = mysock;
                cc = 0;
                break;
            }
            SOCKET_CLOSE(mysock);
        }
        mux_freeaddrinfo(servinfo);
    }
    return cc;
}

static int mod_email_sock_close(SOCKET sock)
{
    return SOCKET_CLOSE(sock);
}

void do_plusemail(dbref executor, dbref cause, dbref enactor, int eval, int key,
                 int nargs, UTF8 *arg1, UTF8 *arg2, const UTF8 *cargs[], int ncargs)
{
    UNUSED_PARAMETER(cause);
    UNUSED_PARAMETER(enactor);
    UNUSED_PARAMETER(eval);
    UNUSED_PARAMETER(key);
    UNUSED_PARAMETER(nargs);
    UNUSED_PARAMETER(cargs);
    UNUSED_PARAMETER(ncargs);

    UTF8 inputline[LBUF_SIZE];

    if ('\0' == mudconf.mail_server[0])
    {
        notify(executor, T("@email: Not configured"));
        return;
    }

    if (!arg1 || !*arg1)
    {
        notify(executor, T("@email: I don\xE2\x80\x99t know who you want to e-mail!"));
        return;
    }

    if (!arg2 || !*arg2)
    {
        notify(executor, T("@email: Not sending an empty e-mail!"));
        return;
    }

    UTF8 *addy = alloc_lbuf("mod_email_do_email.headers");
    UTF8 *bp = addy;
    safe_str(arg1, addy, &bp);
    *bp = '\0';

    UTF8 *subject = (UTF8 *)strchr((char *)addy, '/');
    if (subject)
    {
        *subject = '\0';
        subject++;
    }
    else
    {
        subject = mudconf.mail_subject;
    }

    UTF8 *pMailServer = ConvertCRLFtoSpace(mudconf.mail_server);
    SOCKET mailsock = INVALID_SOCKET;
    int result = mod_email_sock_open(pMailServer, 25, &mailsock);

    if (-1 == result)
    {
        notify(executor, tprintf(T("@email: Unable to resolve hostname %s!"),
            pMailServer));
        free_lbuf(addy);
        return;
    }
    else if (-2 == result)
    {
        // Periodically, we get a failed connect, for reasons which elude me.
        // In almost every case, an immediate retry works.  Therefore, we give
        // it one more shot, before we give up.
        //
        result = mod_email_sock_open(pMailServer, 25, &mailsock);
        if (0 != result)
        {
            notify(executor, T("@email: Unable to connect to mailserver, aborting!"));
            free_lbuf(addy);
            return;
        }
    }

    UTF8 *body = alloc_lbuf("mod_email_do_email.body");
    UTF8 *bodyptr = body;
    mux_exec(arg2, LBUF_SIZE-1, body, &bodyptr, executor, executor, executor,
        EV_TOP | EV_STRIP_CURLY | EV_FCHECK | EV_EVAL, NULL, 0);
    *bodyptr = '\0';

    do
    {
        result = mod_email_sock_readline(mailsock, inputline, LBUF_SIZE - 1);
    } while (  0 == result
            || (  3 < result
               && '-' == inputline[3]));

    if (-1 == result)
    {
        mod_email_sock_close(mailsock);
        notify(executor, T("@email: Connection to mailserver lost."));
        free_lbuf(body);
        free_lbuf(addy);
        return;
    }

    if ('2' != inputline[0])
    {
        mod_email_sock_close(mailsock);
        notify(executor, tprintf(T("@email: Invalid mailserver greeting (%s)"),
            inputline));
    }

    mod_email_sock_printf(mailsock, T("EHLO %s\r\n"), ConvertCRLFtoSpace(mudconf.mail_ehlo));

    do
    {
        result = mod_email_sock_readline(mailsock, inputline, LBUF_SIZE - 1);
    } while (  0 == result
            || (  3 < result
               && '-' == inputline[3]));

    if (-1 == result)
    {
        mod_email_sock_close(mailsock);
        notify(executor, T("@email: Connection to mailserver lost."));
        free_lbuf(body);
        free_lbuf(addy);
        return;
    }

    if ('2' != inputline[0])
    {
        notify(executor, tprintf(T("@email: Error response on EHLO (%s)"),
            inputline));
    }

    mod_email_sock_printf(mailsock, T("MAIL FROM:<%s>\r\n"), ConvertCRLFtoSpace(mudconf.mail_sendaddr));

    do
    {
        result = mod_email_sock_readline(mailsock, inputline, LBUF_SIZE - 1);
    } while (  0 == result
            || (  3 < result
               && '-' == inputline[3]));

    if (-1 == result)
    {
        mod_email_sock_close(mailsock);
        notify(executor, T("@email: Connection to mailserver lost."));
        free_lbuf(body);
        free_lbuf(addy);
        return;
    }

    if ('2' != inputline[0])
    {
        notify(executor, tprintf(T("@email: Error response on MAIL FROM (%s)"),
            inputline));
    }

    mod_email_sock_printf(mailsock, T("RCPT TO:<%s>\r\n"), ConvertCRLFtoSpace(addy));

    do
    {
        result = mod_email_sock_readline(mailsock, inputline, LBUF_SIZE - 1);
    } while (  0 == result
            || (  3 < result
               && '-' == inputline[3]));

    if (-1 == result)
    {
        mod_email_sock_close(mailsock);
        notify(executor, T("@email: Connection to mailserver lost."));
        free_lbuf(body);
        free_lbuf(addy);
        return;
    }

    if ('2' != inputline[0])
    {
        notify(executor, tprintf(T("@email: Error response on RCPT TO (%s)"),
            inputline));
        free_lbuf(body);
        free_lbuf(addy);
        return;
    }

    mod_email_sock_printf(mailsock, T("DATA\r\n"));

    do
    {
        result = mod_email_sock_readline(mailsock, inputline, LBUF_SIZE - 1);
    } while (  0 == result
            || (  3 < result
               && '-' == inputline[3]));

    if (-1 == result)
    {
        mod_email_sock_close(mailsock);
        notify(executor, T("@email: Connection to mailserver lost."));
        free_lbuf(body);
        free_lbuf(addy);
        return;
    }

    if ('3' != inputline[0])
    {
        notify(executor, tprintf(T("@email: Error response on DATA (%s)"),
            inputline));
        free_lbuf(body);
        free_lbuf(addy);
        return;
    }

    UTF8 *pSendName = StringClone(ConvertCRLFtoSpace(mudconf.mail_sendname));
    mod_email_sock_printf(mailsock, T("From: %s <%s>\r\n"),  pSendName, ConvertCRLFtoSpace(mudconf.mail_sendaddr));
    MEMFREE(pSendName);

    mod_email_sock_printf(mailsock, T("To: %s\r\n"), ConvertCRLFtoSpace(addy));
    mod_email_sock_printf(mailsock, T("X-Mailer: TinyMUX %s\r\n"), mudstate.short_ver);
    mod_email_sock_printf(mailsock, T("Subject: %s\r\n\r\n"), ConvertCRLFtoSpace(subject));

    // The body is encoded to include the CRLF.CRLF at the end.
    //
    mod_email_sock_printf(mailsock, T("%s"), EncodeBody(body));

    do
    {
        result = mod_email_sock_readline(mailsock, inputline, LBUF_SIZE - 1);

        // Remove trailing CR and LF characters.
        //
        while (  0 < result
              && (  '\n' == inputline[result-1]
                 || '\r' == inputline[result-1]))
        {
            result--;
            inputline[result] = '\0';
        }
    } while (  0 == result
            || (  3 < result
               && '-' == inputline[3]));

    if (-1 == result)
    {
        mod_email_sock_close(mailsock);
        notify(executor, T("@email: Connection to mailserver lost."));
        free_lbuf(body);
        free_lbuf(addy);
        return;
    }

    if ('2' != inputline[0])
    {
        notify(executor, tprintf(T("@email: Message rejected (%s)"), inputline));
    }
    else
    {
        notify(executor, tprintf(T("@email: Mail sent to %s (%s)"), ConvertCRLFtoSpace(addy), &inputline[4]));
    }

    mod_email_sock_printf(mailsock, T("QUIT\n"));
    mod_email_sock_close(mailsock);

    free_lbuf(body);
    free_lbuf(addy);
}