2

I am trying to send emails from an AWS server using python and sendmail. (SES not available in my region). Using mail at the command line to send a basic email works fine. Now I am trying to do this from Python. I have the code below. It seems to run without error but no mail turns up in recipient email. Note that I am not sending attachments at this stage.

Maillog entries appear for the mail sent via python and via command line. Entries seem quite similar except "Message accepted for delivery" appears after the entries sent via command line.

What could be going wrong here? Where else might I look to find out what is going wrong? Is there an easier way to send email from Python / Linux?

import smtplib
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.utils import COMMASPACE, formatdate
from subprocess import Popen, PIPE

def send_mail(send_from, send_to, subject, text, files=None,
          server="127.0.0.1"):

msg = MIMEMultipart()
#msg['From'] = send_from
msg['To'] = send_to
msg['Date'] = formatdate(localtime=True)
msg['Subject'] = subject

msg.attach(MIMEText(text))

# files should be a dictionary of filenames & base64 content 
for fname in files or {}:
    part = MIMEBase('image', 'jpeg')
    part.set_payload(files[fname])
    part.add_header('Content-Transfer-Encoding', 'base64')
    part['Content-Disposition'] = 'attachment; filename="%s"' % fname
    msg.attach(part)

print (msg.as_string())
p = Popen(["/usr/sbin/sendmail", "-t", "-oi"], stdin=PIPE)
p.communicate(msg.as_string())
Lee Melbourne
  • 407
  • 5
  • 20
  • 1
    have you tried this https://stackoverflow.com/questions/3362600/how-to-send-email-attachments?rq=1 – Jithu R Jacob Sep 20 '17 at 10:11
  • Actually that looks like where i originally adapted my code from. I actually got it working by paring things right back. And building it up again. I will post the solution later on. – Lee Melbourne Sep 21 '17 at 21:55
  • FYI, i think this post is missing the import for `MIMEBase`; something like this: `from email.MIMEBase import MIMEBase` or this `from email.mime.base import MIMEBase` – joefromct Jul 26 '19 at 19:05

2 Answers2

4

So I am not sure where things went wrong. But I was on the right track. My final function is as follows below. This takes a base64 encoded list of images, are sent from my client application in JSON. Each image is in its own dictionary object with {filename: base64Data}.

def send_mail(send_from, send_to, subject, text, files=None):
msg = MIMEMultipart()
msg["From"] = send_from
msg["To"] = send_to
msg["Subject"] = subject
msg.attach(MIMEText(text))

# files should be a dictionary of filenames & base64 content 
for file in files or []:
    for key in file:
        part = MIMEBase('image', 'jpeg')
        part.set_payload(file[key])
        part.add_header('Content-Transfer-Encoding', 'base64')
        part['Content-Disposition'] = 'attachment; filename="%s"' % key
        msg.attach(part)

# send the email by using send mail
p = Popen(["/usr/sbin/sendmail", "-t", "-oi"], stdin=PIPE)
p.communicate(msg.as_string())
Lee Melbourne
  • 407
  • 5
  • 20
0

In gmail is no longer supported base64 image it include outlook too. Currently only apple mail supports base64.

Another :

Base64 encoded image is not showing in gmail

  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/34860083) – user12256545 Aug 21 '23 at 13:16