X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~mdw/git/disorder/blobdiff_plain/bb6ae3fb80fff36cd342d6f6bff3dfabd7dd243e..05b75f8d50b83e943af3be4071449304d82dbdcd:/lib/sendmail.c diff --git a/lib/sendmail.c b/lib/sendmail.c index 3ecfc40..8f75343 100644 --- a/lib/sendmail.c +++ b/lib/sendmail.c @@ -1,6 +1,6 @@ /* * This file is part of DisOrder - * Copyright (C) 2007 Richard Kettlewell + * Copyright (C) 2007-2008 Richard Kettlewell * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -18,16 +18,16 @@ * USA */ -#include -#include "types.h" +#include "common.h" -#include #include #include #include #include #include #include +#include +#include #include "syscalls.h" #include "log.h" @@ -101,7 +101,7 @@ static int sendcommand(const char *tag, FILE *out, const char *fmt, ...) { * @param recipient Recipient address * @param subject Subject string * @param encoding Body encoding - * @param body_type Content-type of body + * @param content_type Content-type of body * @param body Text of body (encoded, but \n for newline) * @return 0 on success, non-0 on error */ @@ -117,7 +117,13 @@ static int sendmailfp(const char *tag, FILE *in, FILE *out, const char *ptr; uint8_t idbuf[20]; char *id; + struct tm ut; + time_t now; + char date[128]; + time(&now); + gmtime_r(&now, &ut); + strftime(date, sizeof date, "%a, %d %b %Y %H:%M:%S +0000", &ut); gcry_create_nonce(idbuf, sizeof idbuf); id = mime_to_base64(idbuf, sizeof idbuf); if((rc = getresponse(tag, in)) / 100 != 2) @@ -145,6 +151,7 @@ static int sendmailfp(const char *tag, FILE *in, FILE *out, || fprintf(out, "MIME-Version: 1.0\r\n") < 0 || fprintf(out, "Content-Type: %s\r\n", content_type) < 0 || fprintf(out, "Content-Transfer-Encoding: %s\r\n", encoding) < 0 + || fprintf(out, "Date: %s\r\n", date) < 0 || fprintf(out, "\r\n") < 0) { write_error: error(errno, "%s: write error", tag); @@ -201,14 +208,10 @@ int sendmail(const char *sender, FILE *in, *out; static const struct addrinfo pref = { - 0, - PF_INET, - SOCK_STREAM, - IPPROTO_TCP, - 0, - 0, - 0, - 0 + .ai_flags = 0, + .ai_family = PF_INET, + .ai_socktype = SOCK_STREAM, + .ai_protocol = IPPROTO_TCP, }; /* Find the SMTP server */ @@ -237,6 +240,37 @@ int sendmail(const char *sender, return rc; } +/** @brief Start a subproces to send a mail message + * @param sender Sender address (can be "") + * @param pubsender Visible sender address (must not be "") + * @param recipient Recipient address + * @param subject Subject string + * @param encoding Body encoding + * @param content_type Content-type of body + * @param body Text of body (encoded, but \n for newline) + * @return Subprocess PID on success, -1 on error + */ +pid_t sendmail_subprocess(const char *sender, + const char *pubsender, + const char *recipient, + const char *subject, + const char *encoding, + const char *content_type, + const char *body) { + pid_t pid; + + if(!(pid = fork())) { + exitfn = _exit; + if(sendmail(sender, pubsender, recipient, subject, + encoding, content_type, body)) + _exit(1); + _exit(0); + } + if(pid < 0) + error(errno, "error calling fork"); + return pid; +} + /* Local Variables: c-basic-offset:2