Question

I'm trying to send an email using smtplib using the low-level commands such as MAIL, RCPT and DATA.

This allows me to disguise the To field while still delivering to the proper inbox (required). My issue is that I have not found a way to send file attachments this way, and I have not found a way to 'spoof' the To field when using smtp_server.sendmail(). Here's the 'raw' method:

    smtp_server = smtplib.SMTP('smtp.mail.server.com', 587)
    smtp_server.ehlo()
    smtp_server.starttls()
    smtp_server.ehlo()
    smtp_server.login('user','pass')

    smtp_server.mail(sender)
    smtp_server.rcpt(inbox)
    data = """From: {0}
To: {1}

{2}"""
    data = data.format(sender, recipient, message)
    smtp_server.data(data)
    smtp_server.close()

Thanks for any assistance.

Was it helpful?

Solution

This was actually pretty simple, create the MIMEMultipart message object, and simply call

smtp_server.data(message.as_string())
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top