aboutsummaryrefslogtreecommitdiff
path: root/comments/src/main.zig
diff options
context:
space:
mode:
authorMartin Ashby <martin@ashbysoft.com>2023-08-28 20:22:21 +0100
committerMartin Ashby <martin@ashbysoft.com>2023-08-28 20:22:21 +0100
commit9075971696cae25228779e9deb9dba113b3d1b43 (patch)
tree57fc964986570483f624269f31ac6476e8c9749b /comments/src/main.zig
parent570d9a2503cfd06303c96f40253f80d8cee84766 (diff)
downloadmfashby.net-9075971696cae25228779e9deb9dba113b3d1b43.tar.gz
mfashby.net-9075971696cae25228779e9deb9dba113b3d1b43.tar.bz2
mfashby.net-9075971696cae25228779e9deb9dba113b3d1b43.tar.xz
mfashby.net-9075971696cae25228779e9deb9dba113b3d1b43.zip
Switch to zig package manager instead of using submodules
Add smtp library Send a notification on comment.
Diffstat (limited to 'comments/src/main.zig')
-rw-r--r--comments/src/main.zig18
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;