From 9075971696cae25228779e9deb9dba113b3d1b43 Mon Sep 17 00:00:00 2001 From: Martin Ashby Date: Mon, 28 Aug 2023 20:22:21 +0100 Subject: Switch to zig package manager instead of using submodules Add smtp library Send a notification on comment. --- comments/src/main.zig | 18 ++++++++++++++++++ comments/src/templates/notification.txt | 4 ++++ 2 files changed, 22 insertions(+) (limited to 'comments/src') 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; diff --git a/comments/src/templates/notification.txt b/comments/src/templates/notification.txt index ba85056..3824f52 100644 --- a/comments/src/templates/notification.txt +++ b/comments/src/templates/notification.txt @@ -1,3 +1,7 @@ +subject: New comment on {{ url }} +from: comments@mfashby.net +to: martin@mfashby.net + New comment on {{ url }}: Author: {{ author }} -- cgit v1.2.3-ZIG