diff options
Diffstat (limited to 'comments/src/main.zig')
-rw-r--r-- | comments/src/main.zig | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/comments/src/main.zig b/comments/src/main.zig index 898b689..882202d 100644 --- a/comments/src/main.zig +++ b/comments/src/main.zig @@ -2,6 +2,7 @@ const std = @import("std"); const zws = @import("zws"); const pq = @import("pq.zig"); const mustache = @import("mustache"); +const smtp = @import("smtp"); const Params = zws.Params; @@ -244,6 +245,23 @@ fn post_comment(res: *Response, ctx: Ctx, _: Params) Err!void { _ = try stmt.step(); } + // Send me an email + const rr = @embedFile("templates/notification.txt"); + const tt = comptime mustache.parseComptime(rr, .{}, .{}); + const Data = struct { url: []const u8, author: []const u8, comment: []const u8 }; + const notification = try mustache.allocRender(res.allocator, tt, Data{ + .url = form_val.url, + .author = form_val.author, + .comment = form_val.comment, + }); + const smtp_username = std.os.getenv("SMTP_USERNAME") orelse "comments@mfashby.net"; + const smtp_password = std.os.getenv("SMTP_PASSWORD") orelse "foobar"; + const notification_address = std.os.getenv("NOTIFICATION_ADDRESS") orelse "martin@mfashby.net"; + const smtp_server = std.os.getenv("SMTP_SERVER") orelse "mail.mfashby.net:587"; + smtp.send_mail(res.allocator, smtp_server, .{ .user = smtp_username, .pass = smtp_password }, smtp_username, &[_][]const u8{notification_address}, notification) catch |err| { + std.log.err("failed to send notification email {}", .{err}); + }; + // And redirect! res.transfer_encoding = .none; res.status = .found; |