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 ability to send attachments #7

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Prev Previous commit
feat(send): add ability to add attachments for composing mail
Use `<ctrl>g<ctrg>a` to open attachment window and add one full path to
attachment per line
  • Loading branch information
simonhughxyz committed Feb 11, 2025

Verified

This commit was signed with the committer’s verified signature.
austinhuang0131 Austin Huang
commit 74cf8126dee0167b550a626866acef15085dcf03
1 change: 1 addition & 0 deletions lua/notmuch/config.lua
Original file line number Diff line number Diff line change
@@ -12,6 +12,7 @@ C.defaults = function()
open_cmd = 'xdg-open',
keymaps = { -- This should capture all notmuch.nvim related keymappings
sendmail = '<C-g><C-g>',
attachment_window = '<C-g><C-a>',
},
}
return defaults
68 changes: 64 additions & 4 deletions lua/notmuch/send.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
local s = {}
local u = require('notmuch.util')
local m = require('notmuch.mime')
local v = vim.api

local config = require('notmuch.config')
@@ -17,19 +18,62 @@ local config = require('notmuch.config')
-- vim.keymap.set('n', '<C-c><C-c>', function()
-- confirm_sendmail(reply_filename)
-- end, { buffer = true })
local confirm_sendmail = function(filename)
local confirm_sendmail = function()
local choice = v.nvim_call_function('confirm', {
'Send email?',
'&Yes\n&No',
2 -- Default to no
})

if choice == 1 then
vim.cmd.write()
s.sendmail(filename)
return true
else
return false
end
end

-- Builds mime msg from contents of main msg buffer and attachment buffer
local build_mime_msg = function(buf, buf_attach, compose_filename)
local attach_lines = vim.api.nvim_buf_get_lines(buf_attach, 0, -1, false)
local main_lines = vim.api.nvim_buf_get_lines(buf, 0, -1, false)


-- capture attributes and remove from main message buffer
local attributes, msg = m.get_msg_attributes(main_lines)
v.nvim_buf_set_lines(buf, 0, -1, false, msg)
vim.cmd.write({bang = true})



-- get attachments and build mime table
local attachments = m.create_mime_attachments(attach_lines)
local mimes = {{
file = compose_filename,
type = "text/plain; charset=utf-8",
}}

for _, v in ipairs(attachments) do
table.insert(mimes, v)
end



local mime_table = {
version = "Mime-Version: 1.0",
type = "multipart/mixed", -- or multipart/alternative
encoding = "8 bit",
attributes = attributes,
mime = mimes,
}


local mime_msg = m.make_mime_msg(mime_table)
v.nvim_buf_set_lines(buf, 0, -1, false, mime_msg)


vim.cmd.write({bang = true})
end

-- Send a completed message
--
-- This function takes a file containing a completed message and send it to the
@@ -111,9 +155,25 @@ s.compose = function()
-- Populate with header fields (date, to, subject)
v.nvim_buf_set_lines(buf, 0, -1, false, headers)

local buf_attach = v.nvim_create_buf(true, true)

-- Keymap for showing attachment_window
vim.keymap.set('n', config.options.keymaps.attachment_window, function()
vim.api.nvim_open_win(buf_attach, true, {
split = 'left',
win = 0
})

end, { buffer = true })

-- Keymap for sending the email
vim.keymap.set('n', config.options.keymaps.sendmail, function()
confirm_sendmail(compose_filename)
if confirm_sendmail() then
build_mime_msg(buf, buf_attach, compose_filename)

s.sendmail(compose_filename)
end

end, { buffer = true })
end