当前位置: 首页>>代码示例>>Python>>正文


Python EmailMessage.attach方法代码示例

本文整理汇总了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)
开发者ID:bosondata,项目名称:prerender,代码行数:33,代码来源:mhtml.py

示例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
开发者ID:zalando-stups,项目名称:costreport,代码行数:12,代码来源:costreport.py

示例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
开发者ID:jwodder,项目名称:daemail,代码行数:17,代码来源:message.py

示例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
开发者ID:sladinji,项目名称:LabResult,代码行数:43,代码来源:mail.py

示例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
开发者ID:sladinji,项目名称:LabResult,代码行数:66,代码来源:email.py


注:本文中的email.message.EmailMessage.attach方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。