pythonemailsmtplibmime-mail

Sending an email with Python from a Protonmail account, SMTP library


I am using apple mail app with protonmail-I have the bridge app. (MacOS and Windows install here; linux here.)

After activating the bridge app, I tried to send an email with python by using smtp library and it does not work. Here is the code which I tried to run and failed me:

import smtplib

server = smtplib.SMTP("127.0.0.1", portnumber)
server.login("mymail@protonmail.com", "my password")
server.sendmail(
    "mymail@protonmail.com",
    "receiver@protonmail.com",
    "hello")
server.quit()

The error message I receive:

smtplib.SMTPDataError: (554, b'Error: transaction failed, blame it on the weather: malformed MIME header line: 00')


Solution

  • This might help..

    import smtplib 
    from email.MIMEMultipart import MIMEMultipart 
    from email.MIMEText import MIMEText
    
    port_number =1234
    msg = MIMEMultipart()
    msg['From'] = 'sender@protonmail.com'
    msg['To'] = 'receiver@protonmail.com'
    msg['Subject'] = 'My Test Mail '
    message = 'This is the body of the mail'
    msg.attach(MIMEText(message))
    mailserver = smtplib.SMTP('localhost',port_number)
    mailserver.login("sender@protonmail.com", "mypassword")
    mailserver.sendmail('sender@protonmail.com','receiver@protonmail.com',msg.as_string())
    mailserver.quit()