def protect_message(basemsg, gpg, do_sign=True, boundary=None, sign_digest_algo='SHA512', passphrase=None): msg = email.message.Message() _msg = MIMEMultipart(_subtype="signed", micalg="pgp-%s" % sign_digest_algo.lower(), protocol="application/pgp-signature") for hdr in COPY_HEADERS: msg[hdr] = basemsg[hdr] for hdr in _msg.keys(): msg[hdr] = _msg[hdr] if boundary: msg.set_boundary(boundary()) wrapper = _wrap_with_header(basemsg, boundary=boundary) msg.attach(wrapper) if do_sign: payload = wrapper.as_string().replace('\n', '\r\n') sig = gpg.sign(payload, detach=True, digest_algo=sign_digest_algo, clearsign=False, passphrase=passphrase) signed_msg = _make_signature_subpart(sig) msg.attach(signed_msg) return msg
def protect_message(basemsg, gpg, do_sign=True, boundary=None, sign_digest_algo='SHA512', passphrase=None): msg = email.message.Message() _msg = MIMEMultipart( _subtype="signed", micalg="pgp-%s" % sign_digest_algo.lower(), protocol="application/pgp-signature") for hdr in COPY_HEADERS: msg[hdr] = basemsg[hdr] for hdr in _msg.keys(): msg[hdr] = _msg[hdr] if boundary: msg.set_boundary(boundary()) wrapper = _wrap_with_header(basemsg, boundary=boundary) msg.attach(wrapper) if do_sign: payload = wrapper.as_string().replace('\n', '\r\n') sig = gpg.sign( payload, detach=True, digest_algo=sign_digest_algo, clearsign=False, passphrase=passphrase) signed_msg = _make_signature_subpart(sig) msg.attach(signed_msg) return msg
else: non_mail_files.append(filepath) # Bundle attachements to their mails if len(non_mail_files): if len(mail_files) == 1: # Attach all non-mail files to the only mail found here print "Add to %s these attachements: %r" % (mail_files[0][0], non_mail_files) mail = mail_files[0][1] # Transform our simple mail to a multipart one if not mail.is_multipart(): new_mail = MIMEMultipart() new_mail.attach(MIMEText(mail.get_payload())) # Transfer all headers headers_to_transfer = set([h.lower() for h in mail.keys()]) - set([h.lower() for h in new_mail.keys()]) for h in headers_to_transfer: h_value = mail.get_all(h) print repr(h) print repr(h_value) if type(h_value) == type([]): for v in h_value: new_mail.add_header(h, v) else: new_mail.add_header(h, h_value) mail = new_mail for f in non_mail_files: part = MIMEBase('application', "octet-stream") part.set_payload(open(f, "rb").read()) Encoders.encode_base64(part)
# Bundle attachements to their mails if len(non_mail_files): if len(mail_files) == 1: # Attach all non-mail files to the only mail found here print "Add to %s these attachements: %r" % (mail_files[0][0], non_mail_files) mail = mail_files[0][1] # Transform our simple mail to a multipart one if not mail.is_multipart(): new_mail = MIMEMultipart() new_mail.attach(MIMEText(mail.get_payload())) # Transfer all headers headers_to_transfer = set([ h.lower() for h in mail.keys() ]) - set([h.lower() for h in new_mail.keys()]) for h in headers_to_transfer: h_value = mail.get_all(h) print repr(h) print repr(h_value) if type(h_value) == type([]): for v in h_value: new_mail.add_header(h, v) else: new_mail.add_header(h, h_value) mail = new_mail for f in non_mail_files: part = MIMEBase('application', "octet-stream") part.set_payload(open(f, "rb").read()) Encoders.encode_base64(part)