HTML Image Base64 in emails

Asked

Viewed 352 times

0

I have a python API that sends emails. The problem is that I mount an HTML and tag <img> and I put a clothesline from Base64 which is the src. However 90% of email services do not accept Base64. Is there any other way out ?
Follows the Code:

def show_logotipo():
    from PIL import Image
    import StringIO
    import base64
    logotipo = db(Parametros_Gerais.id == 1).select().first().logotipo
    pic = Image.open(os.path.join(request.folder,'uploads',logotipo))
    buf= StringIO.StringIO()
    pic.save(buf, format='PNG')
    buf.seek(0)
    buf_s = buf.read()
    logo_final = base64.b64encode(buf_s)
    return logo_final

logo_final = show_logotipo()
            mail_body = "<html>"\
                            "<header>"\
                                "<style>"\
                                "span{"\
                                    "font-family: serif;"\
                                    "font-weight:bold;"\
                                    "font-style: 18px;"\
                                "}"\
                                "p{"\
                                    "font-family: serif;"\
                                    "font-style: 18px;"\
                                "}"\
                                "</style>"\
                            "</header>"\
                            "<body>"\
                                "<h1>Você foi convidado a uma nova sala de conferencia</h1>"\
                                "<p><span>Número da conferencia:</span> "+str(busca_conf[0]['confno'])+"</p>"\
                                "<p><span>Senha de acesso:</span> "+str(user_pw)+"</p>"\
                                "<p><span>Horario:</span> "+str(busca_conf[0]['starttime'].strftime('%Y-%m-%d %H:%M'))+"</p>"\
                                "<p><span>Qualquer dúvida sobre essa conferencia, envie um e-mail para:</span> "+email_admin+"</p>"\
                                "<p>--</p>"\
                                "<img src='data:image/png;base64,"+logo_final+"'>"\
                            "</body>"\
                        "</html>"
            print mail_body
mail.send(
                to=item_form['email'],
                subject='Convite para uma nova sala de Conferencia',
                message=mail_body,
                    attachments=mail.Attachment('/var/www/audio_conf.ics')
)
  • I think it will not solve your problem, but sometimes it gives you a light... https://answall.com/questions/286570/imagem-em-e-mail-html?noredirect=1#comment583592_286570

1 answer

1

Send Multipart email as follows (python3):

from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

def enviaEmail(strFrom, strTo, assunto, msgText=None, msgHTML=None, img1=None, img2=None):
    msgRoot = MIMEMultipart('related')
    msgRoot['Subject'] = assunto
    msgRoot['From'] = strFrom
    msgRoot['To'] = strTo
    msgRoot.preamble = 'This is a multi-part message in MIME format.'

    msgAlternative = MIMEMultipart('alternative')
    msgRoot.attach(msgAlternative)

    if msgText:
       msgText = MIMEText(msgText)
       msgAlternative.attach(msgText)

    if msgHTML:
        #  Imagem no html: <img src="cid:imagem1">
        msgText = MIMEText(msgHTML, 'html')
        msgAlternative.attach(msgText)

    if img1:
        fp = open(img1, 'rb')
        msgImage = MIMEImage(fp.read())
        fp.close()
        msgImage.add_header('Content-ID', '<imagem1>')
        msgRoot.attach(msgImage)

    # Outra imagem....
    if img2:
        fp = open(img2, 'rb')
        msgImage = MIMEImage(fp.read())
        fp.close()
        msgImage.add_header('Content-ID', '<imagem2>')
        msgRoot.attach(msgImage)

    smtp = smtplib.SMTP()
    smtp.connect('correio.smtp.com.br')
    smtp.sendmail(strFrom, strTo, msgRoot.as_string())
    smtp.quit()


msgHTML="""
  <!DOCTYPE html>
  <html lang="en">
  <head>
    <meta charset="UTF-8">
       <title>TESTE</title>
  </head>
  <body>
   <h1>Imagem 1</h1>
   <img src="cid:imagem1"/>
   <h2>Imagem 2</h1>
   <img src="cid:imagem2"/>
   </body>
   </html>    
"""
enviaEmail("[email protected]",
           "[email protected]",
           "Assunto ",
           msgHTML=msg,
           img1="imagem/caminho1.png"
           img2="imagem/caminho_imagem2.png")
  • Vlw, you helped me a lot!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.