Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

Is this a good way to send a signed email message? It seems to me that a public key is a little too large to send as a email header. Should I be including the signature and key as a header field, or should I be sending it within the message body?

What are some ways that I could improve this script to make its usage more practical for sending email?

import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText

from Crypto.Signature import PKCS1_v1_5
from Crypto.Hash import SHA256
from Crypto.PublicKey import RSA

FROMADDR = "[email protected]"
TOADDRS = ["[email protected]"]
LOGIN = ""
PASSWORD = ""
body = "hello world"

key = RSA.importKey(open('/mnt/external_sd/keys/private.asc').read())
h = SHA256.new(body)
signer = PKCS1_v1_5.new(key)
signature = signer.sign(h).encode("base64")

msg = MIMEMultipart()
msg["X-signature"]=signature
msg["X-public-key"]=open("/mnt/external_sd/keys/public.asc").read().encode("base64")
msg['From'] = "[email protected]"
msg['To'] = "[email protected]"
msg['Subject'] = 'sign test'
msg.attach(MIMEText(body, 'plain'))

server = smtplib.SMTP_SSL('smtp.example.net', 465)
server.set_debuglevel(1)
server.ehlo()
server.login(LOGIN, PASSWORD)
server.sendmail(FROMADDR, TOADDRS, msg.as_string())
server.quit()
share|improve this question

closed as off-topic by Gareth Rees, syb0rg, Michael Urman, Malachi, rolfl Jan 5 at 20:49

This question appears to be off-topic. The users who voted to close gave these specific reasons:

  • "Questions asking for code to be written to solve a specific problem are off-topic here as there is no code to review." – Michael Urman, Malachi
  • "Your question must contain working code for us to review it here. For questions regarding specific problems encountered while coding, try Stack Overflow. After getting your code to work, you may edit this question seeking a review of your working code." – Gareth Rees, syb0rg
If this question can be reworded to fit the rules in the help center, please edit the question.

    
Have you done some research in to how other tools send signed mail? PGPMail, outlook, etc.? –  rolfl Jan 4 at 23:03
    
This code does not work for me: my public key is not stored in /mnt/external_sd/... and I can't connect to the SMTP server at smtp.example.net. We only review working code here, so I have voted to close. –  Gareth Rees Jan 5 at 12:48
1  
@GarethRees I personally don't thing of those things as reasons for closing. Public key location and SMTP server is data, not code. I'm voting to leave open. –  Simon André Forsberg Jan 5 at 12:55
    
If it's data, then there should be some way to change it (for example, a configuration file, or command-line arguments). No-one should have to edit source code to make a program work. –  Gareth Rees Jan 5 at 13:03
    
@GarethRees If this question had been written with functions, say a def send_mail(server, port): ... with the last 6 lines, never called, and something similar for the private key, your complaint would not apply. However the question seems to be either a question about how email works, or about how to structure the code that hasn't been written yet. So I agree that it is off-topic. –  Michael Urman Jan 5 at 17:04
show 4 more comments

Browse other questions tagged or ask your own question.