Вставить изображение в почтовый питон через запрос на мыло - PullRequest
1 голос
/ 01 апреля 2019

Я пытаюсь использовать suds, чтобы сделать soap запрос и отправить электронное письмо с python. Для этого письма требуется встраивание изображения.

Импорт:

import uuid
import cgi
import os
from suds.client import Client
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage

Тело электронной почты:

img = '/path/img.jpg'
msg = MIMEMultipart('related')
img = dict(title=u'Some text…', path=u'/path/img.jpg', cid=str(uuid.uuid4()))

msg_alternative = MIMEMultipart('alternative')
msg.attach(msg_alternative)

msg_text = MIMEText(u'[image: {title}]'.format(**img), 'plain', 'utf-8')
msg_alternative.attach(msg_text)

msg_html = MIMEText(u'<div dir="ltr">'
                     '<img src="cid:{cid}" alt="{alt}"><br></div>'
                    .format(alt='Bla', **img),
                    'html', 'utf-8')
msg_alternative.attach(msg_html)

with open(img['path'], 'rb') as file:
    msg_image = MIMEImage(file.read(), name=os.path.basename(img['path']))
    msg.attach(msg_image)
msg_image.add_header('Content-ID', '<{}>'.format(img['cid']))

SOAP:

url = "http://XX.X.XX.XX/path/SendEmail.asmx?WSDL"
client = Client(url)

client.set_options(port="ID_NAMEServiceSoap")
client.service.SendEmail("sender@bla.com", 
                     "receive@bla.com", 
                     u"This is a test", 
                     msg.as_string(),
                     None,
                     " ",
                     "secHash")

Ответ:

Content-Type: multipart/related; boundary="===============4991678697102201166==" MIME-Version: 1.0 --===============4991678697102201166== Content-Type: multipart/alternative; boundary="===============7267000742066247724==" MIME-Version: 1.0 --===============7267000742066247724== Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: base64 W2ltYWdlOiBQaWN0dXJlIHJlcG9ydOKApl0= --===============7267000742066247724== Content-Type: text/html; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: base64 PGRpdiBkaXI9Imx0ciI+PGltZyBzcmM9ImNpZDo0ZTA0NjM4MS0yMjkzLTQwODktYTA5OC0wMmJl MjM5OTQwMzQiIGFsdD0iQmxhIj48YnI+PC9kaXY+ --===============7267000742066247724==-- --===============4991678697102201166== Content-Type: image/jpeg; name="G01.jpg" MIME-Version: 1.0 Content-Transfer-Encoding: base64 Content-ID: <4e046381-2293-4089-a098-02be23994034> /9j/4AAQSkZJRgABAQEAlQCVAAD/2wBDAAcEBAQFBAcFBQcKBwUHCgwJBwcJDA0LCwwLCw0RDQ0N DQ0NEQ0PEBEQDw0UFBYWFBQeHR0dHiIiIiIiIiIiIiL/2wBDAQgHBw0MDRgQEBgaFREVGiAgICAg ICAgICAgICAhICAgICAgISEhICAgISEhISEhISEiIiIiIiIiIiIiIiIiIiL/wAARCANMBdwDAREA AhEBAxEB/8QAHAABAQEBAQEBAQEAAAAAAAAAAAECAwQFBgcI/8QATRAAAgEDAgMDBwkFBwMDAgcB AAECAwQREiEFMUEGE1EiMkJhcYGxBxQjM1JikaHBFSRy0eE0Q1NjgqKykvDxJXPCFiYXNUR0g6PS 4v/EABsBAQEBAQEBAQEAAAAAAAAAAAABAgUEAwYH/... P//Z --===============4991678697102201166==--
...