vim: improve the way messages are sent

We want the proper encoding and content-type to be set when sending the
mail, but human-readable plain-text for composing. So split the code in
two parts: the presentation and the transport conversion.

This fixes an issue while sending non-ascii mails to strict servers; the
mail needs to be encoded.

Signed-off-by: Felipe Contreras <felipe.contreras@gmail.com>
This commit is contained in:
Felipe Contreras 2014-05-01 17:57:54 -05:00 committed by David Bremner
parent 468deca60c
commit cc8c213bbc

View file

@ -86,17 +86,22 @@ endfunction
function! s:compose_send() function! s:compose_send()
let b:compose_done = 1 let b:compose_done = 1
let fname = expand('%') let fname = expand('%')
let lines = getline(5, '$')
" remove headers ruby << EOF
0,4d # Generate proper mail to send
write text = VIM::evaluate('lines').join("\n")
fname = VIM::evaluate('fname')
transport = Mail.new(text)
transport.message_id = generate_message_id
transport.charset = 'utf-8'
File.write(fname, transport.to_s)
EOF
let cmdtxt = g:notmuch_sendmail . ' -t -f ' . s:reply_from . ' < ' . fname let cmdtxt = g:notmuch_sendmail . ' -t -f ' . s:reply_from . ' < ' . fname
let out = system(cmdtxt) let out = system(cmdtxt)
let err = v:shell_error let err = v:shell_error
if err if err
undo
write
echohl Error echohl Error
echo 'Eeek! unable to send mail' echo 'Eeek! unable to send mail'
echo out echo out
@ -572,9 +577,7 @@ ruby << EOF
end end
m.cc = orig[:cc] m.cc = orig[:cc]
m.from = $email m.from = $email
m.message_id = generate_message_id
m.charset = 'utf-8' m.charset = 'utf-8'
m.content_transfer_encoding = '7bit'
end end
lines = [] lines = []
@ -600,7 +603,7 @@ ruby << EOF
reply.body = body_lines.join("\n") reply.body = body_lines.join("\n")
lines += reply.to_s.lines.map { |e| e.chomp } lines += reply.present.lines.map { |e| e.chomp }
lines << "" lines << ""
cur = lines.count - 1 cur = lines.count - 1
@ -611,18 +614,13 @@ ruby << EOF
def open_compose() def open_compose()
lines = [] lines = []
lines << "Date: #{Time.now().strftime('%a, %-d %b %Y %T %z')}"
lines << "From: #{$email}" lines << "From: #{$email}"
lines << "To: " lines << "To: "
cur = lines.count cur = lines.count
lines << "Cc: " lines << "Cc: "
lines << "Bcc: " lines << "Bcc: "
lines << "Message-Id: #{generate_message_id}"
lines << "Subject: " lines << "Subject: "
lines << "Mime-Version: 1.0"
lines << "Content-Type: text/plain; charset=utf-8"
lines << "Content-Transfer-Encoding: 7bit"
lines << "" lines << ""
lines << "" lines << ""
lines << "" lines << ""
@ -928,6 +926,16 @@ ruby << EOF
end end
text text
end end
def present
buffer = ''
header.fields.each do |f|
buffer << "%s: %s\r\n" % [f.name, f.to_s]
end
buffer << "\r\n"
buffer << body.to_s
buffer
end
end end
end end