17

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')

Justapigeon
  • 560
  • 1
  • 8
  • 22
Yesim
  • 193
  • 1
  • 1
  • 7

2 Answers2

11

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()
  • 3
    It is important to note that this will only work if you have the protonmail bridge software running on the machine that is running this script. You cannot simply enter your account password for the `mailserver.login()` line; you have to check the bridge application for the hashed (or pseudo?) password that is provided for your account. – Justapigeon Dec 19 '19 at 01:41
  • 1
    Mayle small update of imports: `from email.mime.text import MIMEText` and `from email.mime.multipart import MIMEMultipart`. – lukassliacky Aug 20 '21 at 10:52
  • This doesn't seem to work anymore. It just gives me `ConnectionRefusedError: [Errno 111] Connection refused` – Cerin May 22 '22 at 01:27
2

I'm pretty new to this and was having some significant trouble.... until I just made the following tiny change:

Change the from lines to this:

import smtplib 
from email.mime.multipart import MIMEMultipart
from email.mime.text 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()