Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for reply-to header. #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 32 additions & 2 deletions mail.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include <urweb.h>

struct headers {
uw_Basis_string from, to, cc, bcc, subject;
uw_Basis_string from, to, cc, bcc, subject, replyto;
};

typedef struct headers *uw_Mail_headers;
Expand All @@ -36,6 +36,7 @@ static uw_Mail_headers copy_headers(uw_Mail_headers h) {
h2->cc = copy_string(h->cc);
h2->bcc = copy_string(h->bcc);
h2->subject = copy_string(h->subject);
h2->replyto = copy_string(h->replyto);
return h2;
}

Expand All @@ -45,6 +46,7 @@ static void free_headers(uw_Mail_headers h) {
free_string(h->cc);
free_string(h->bcc);
free_string(h->subject);
free_string(h->replyto);
free(h);
}

Expand Down Expand Up @@ -172,6 +174,23 @@ uw_Mail_headers uw_Mail_subject(uw_context ctx, uw_Basis_string s, uw_Mail_heade
return h2;
}

uw_Mail_headers uw_Mail_replyto(uw_context ctx, uw_Basis_string s, uw_Mail_headers h) {
uw_Mail_headers h2 = uw_malloc(ctx, sizeof(struct headers));

if (h)
*h2 = *h;
else
memset(h2, 0, sizeof(*h2));

if (h2->replyto)
uw_error(ctx, FATAL, "Duplicate Reply-to header");

address(ctx, s);
h2->replyto = uw_strdup(ctx, s);

return h2;
}

typedef struct {
uw_context ctx;
uw_Mail_headers h;
Expand All @@ -187,7 +206,7 @@ static int smtp_read(uw_context ctx, int sock, char *buf, ssize_t *pos) {
ssize_t recvd;

buf[*pos] = 0;

if ((s = strchr(buf, '\n'))) {
int n;

Expand Down Expand Up @@ -390,6 +409,17 @@ static void commit(void *data) {
}
}

if (j->h->replyto) {
snprintf(out, sizeof(out), "Reply-to: %s\r\n", j->h->replyto);
out[sizeof(out)-1] = 0;

if (really_string(sock, out) < 0) {
close(sock);
uw_set_error_message(j->ctx, "Error sending Reply-to");
return;
}
}

if ((s = uw_get_global(j->ctx, "extra_mail_headers"))) {
if (really_string(sock, s) < 0) {
close(sock);
Expand Down
1 change: 1 addition & 0 deletions mail.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ uw_Mail_headers uw_Mail_to(uw_context, uw_Basis_string, uw_Mail_headers);
uw_Mail_headers uw_Mail_cc(uw_context, uw_Basis_string, uw_Mail_headers);
uw_Mail_headers uw_Mail_bcc(uw_context, uw_Basis_string, uw_Mail_headers);
uw_Mail_headers uw_Mail_subject(uw_context, uw_Basis_string, uw_Mail_headers);
uw_Mail_headers uw_Mail_replyto(uw_context, uw_Basis_string, uw_Mail_headers);

uw_unit uw_Mail_send(uw_context, uw_Mail_headers, uw_Basis_string body, uw_Basis_string xbody);
1 change: 1 addition & 0 deletions mail.urs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ val empty : headers
(* Each of the following may be used at most once in constructing a [headers]. *)
val from : string -> headers -> headers
val subject : string -> headers -> headers
val replyto : string -> headers -> headers

(* The following must be called with single valid e-mail address arguments, and
* all such addresses passed are combined into single header values. *)
Expand Down