本文整理汇总了Python中email.message.EmailMessage.attach方法的典型用法代码示例。如果您正苦于以下问题:Python EmailMessage.attach方法的具体用法?Python EmailMessage.attach怎么用?Python EmailMessage.attach使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类email.message.EmailMessage
的用法示例。
在下文中一共展示了EmailMessage.attach方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: MHTML
# 需要导入模块: from email.message import EmailMessage [as 别名]
# 或者: from email.message.EmailMessage import attach [as 别名]
class MHTML(object):
def __init__(self):
self._msg = EmailMessage()
self._msg['MIME-Version'] = '1.0'
self._msg.add_header('Content-Type', 'multipart/related', type='text/html')
def add(self, location: str, content_type: str, payload: str, encoding: str = 'quoted-printable') -> None:
resource = EmailMessage()
if content_type == 'text/html':
resource.add_header('Content-Type', 'text/html', charset='utf-8')
else:
resource['Content-Type'] = content_type
if encoding == 'quoted-printable':
resource['Content-Transfer-Encoding'] = encoding
resource.set_payload(quopri.encodestring(payload.encode()))
elif encoding == 'base64':
resource['Content-Transfer-Encoding'] = encoding
resource.set_payload(base64.b64encode(payload))
elif encoding == 'base64-encoded': # Already base64 encoded
resource['Content-Transfer-Encoding'] = 'base64'
resource.set_payload(payload)
else:
raise ValueError('invalid encoding')
resource['Content-Location'] = location
self._msg.attach(resource)
def __str__(self) -> str:
return str(self._msg)
def __bytes__(self) -> bytes:
return bytes(self._msg)
示例2: combine
# 需要导入模块: from email.message import EmailMessage [as 别名]
# 或者: from email.message.EmailMessage import attach [as 别名]
def combine(body, attachments):
message = EmailMessage()
message.add_related(body, subtype='html')
for attachment in attachments:
cid = attachment['cid']
buffer = attachment['buffer']
img = MIMEImage(buffer.read(), _subtype='png')
img.add_header('Content-ID', cid)
message.attach(img)
return message
示例3: compile
# 需要导入模块: from email.message import EmailMessage [as 别名]
# 或者: from email.message.EmailMessage import attach [as 别名]
def compile(self):
if len(self.parts) == 1 and isinstance(self.parts[0], str):
msg = txt2mail(self.parts[0])
else:
msg = EmailMessage(policy=POLICY)
# This currently doesn't work <https://bugs.python.org/issue30820>:
#msg.set_content([
# txt2mail(p) if isinstance(p, str) else p for p in self.parts
#])
msg.make_mixed()
for p in self.parts:
msg.attach(txt2mail(p) if isinstance(p, str) else p)
for k,v in self.headers.items():
msg[k] = v
return msg
示例4: send
# 需要导入模块: from email.message import EmailMessage [as 别名]
# 或者: from email.message.EmailMessage import attach [as 别名]
def send(recipient):
"""
Send demo credentials to recipient
"""
# Create the base text message.
msg = EmailMessage()
msg['Subject'] = "Connexion démo LabResult "
msg['From'] = Address("", "[email protected]")
msg['To'] = (Address("", recipient),)
msg.set_content(mail_tmplt_txt.content)
logo_cid = str(uuid.uuid4())
msg.add_alternative(mail_tmplt_html.content.format(logo_cid=logo_cid),
'html', 'utf-8')
# Now add the related image to the html part.
logo = os.path.join(os.path.dirname(__file__), "data", "logo.png")
with open(logo, 'rb') as img:
msg_image = MIMEImage(img.read(), name=os.path.basename(logo),
_subtype="image/png" )
msg.attach(msg_image)
msg_image.add_header('Content-ID', '<{}>'.format(logo_cid))
msg2 = MIMEText("Envoi des identifians de démo à %s" % recipient)
msg2['Subject'] = "Connexion démo LabResult "
msg2['From'] = "[email protected]"
msg2['To'] = "[email protected]"
# Send the message via local SMTP server.
ret = False
try :
smtp_server = get_option('smtp_server', 'mail.gandi.net')
with smtplib.SMTP_SSL(smtp_server) as s:
USERNAME = get_option('smtp_login', '[email protected]')
PASSWORD = get_option('smtp_password','pacman9732')
s.login(USERNAME, PASSWORD)
s.send_message(msg)
s.send_message(msg2)
ret = True
except Exception :
labresult.app.logger.error(traceback.format_exc())
finally:
return ret
示例5: send_email_credential
# 需要导入模块: from email.message import EmailMessage [as 别名]
# 或者: from email.message.EmailMessage import attach [as 别名]
def send_email_credential(user):
"""
Send credentials to user
:param user: :class:`labresult.model.User`
"""
if not user.email :
labresult.app.logger.warning("%s has no email" % user.email)
return False
# wait ten minutes between to sending
for message in [ m for m in user.messages if m.title == "credential" and
m.channel == "email" ]:
if (now() - message.date) < timedelta(
minutes=get_option('crendential_email.tempo', 10,
gettext("Minutes avant l'envoi d'un deuxieme email"))):
labresult.app.logger.warning("Credential email already sent to %s" % user.email)
return True
# Create the base text message.
msg = EmailMessage()
msg['Subject'] = get_option("credential_email.title",
gettext("Accès à mes résultats"))
msg['From'] = Address("",
get_option('crendential_email.from_address',
"[email protected]",
gettext("Adresse email de l'envoyeur."),
)
)
msg['To'] = (Address("", user.email),)
#generate txt from html
template = get_option("credential_email.html_content",
credential.html_content,
gettext("Contenu du mail d'authentification"),
)
txt_content = re.sub("<[^>]+>",
"",
template.replace("<br>",'\n').format(code=user.credential_code,
logo_cid=""))
msg.set_content(txt_content)
logo_cid = str(uuid.uuid4())
msg.add_alternative(template.format(logo_cid=logo_cid,
code = user.credential_code),
'html', 'utf-8')
# Now add the related image to the html part.
default_logo = open(os.path.join(os.path.dirname(__file__), "data",
"logo.png"), 'rb').read()
logo = get_option("credential_email.logo", default_logo)
msg_image = MIMEImage(logo, name='logo.png',
_subtype="image/png" )
msg.attach(msg_image)
msg_image.add_header('Content-ID', '<{}>'.format(logo_cid))
# Send the message via local SMTP server.
try :
with smtplib.SMTP_SSL(get_option("smtp_server")) as s:
s.login(get_option("smtp_login"), get_option("smtp_password"))
s.send_message(msg)
log_message(user, "credential", "email", get_option("smtp_server"),
txt_content)
return True
except Exception :
error = traceback.format_exc()
labresult.app.logger.error(error)
return False