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


Python Message.attachments方法代码示例

本文整理汇总了Python中pyramid_mailer.message.Message.attachments方法的典型用法代码示例。如果您正苦于以下问题:Python Message.attachments方法的具体用法?Python Message.attachments怎么用?Python Message.attachments使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在pyramid_mailer.message.Message的用法示例。


在下文中一共展示了Message.attachments方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: send_mail

# 需要导入模块: from pyramid_mailer.message import Message [as 别名]
# 或者: from pyramid_mailer.message.Message import attachments [as 别名]
def send_mail(to, etype, request, data=None):
    add_cc = ["mentoring_request", "mentoring_request_intro"]
    if data is None:
        data = {}
    mi = emails.get(etype, "data").copy()

    if mi.get("mailer"):
        mailer = mailer_factory_from_settings(mi.get("mailer"), "")
    else:
        mailer = get_mailer(request)

    if "user" not in data and request.user:
        data["user"] = request.user

    if request and request.authenticated_userid and data["user"].id != request.authenticated_userid:
        data["auth_user"] = request.user

    for k, v in mi.iteritems():
        if callable(v):
            v = v(data, request)
        if ".jinja2" in v:
            v = render(v, data, request)
        elif isinstance(v, basestring):
            if "{{" in v and "}}" in v:
                v = Template(v).render(**data)
            else:
                v = v.format(**data)
        mi[k] = v

    if "attachments" in data:
        attachments = data["attachments"]
    elif "attachments" in mi and mi["attachments"] and mi["attachments"] != "None":
        with open(os.path.dirname(os.path.dirname(__file__)) + mi["attachments"]) as fo:
            a_file = fo.read()
        attachments = [Attachment(os.path.basename(mi["attachments"]), "application/" + mi["attachments"][-3:], a_file)]
    else:
        attachments = None
    if not isinstance(to, list):
        to = to.split(",")

    from alchemist.models.settings import Settings

    send_from = mi.get("from")
    bcc = [bc for bc in data.get("bcc", mi.get("bcc", "").split(",")) if bc]
    sender = Settings.get("admin_mail", "[email protected]")
    sender = sender.split(",")
    main_admin_mail = sender[0].strip()
    if main_admin_mail:
        bcc.append(main_admin_mail)
    if not send_from:
        send_from = main_admin_mail or "[email protected]"
    cc = mi.get("cc") if mi.get("cc") else []
    if "@" not in cc and not isinstance(cc, list):
        cc = Settings.get(cc, cc).split(",")
    if not cc and etype in add_cc:
        cc = [email.strip() for email in Settings.get("cc_emails", "").split(",")]
    if debug and not set(Settings.get("allowed_outgoing", "").split(",")).issuperset(bcc + cc + to):
        print "Wanted to send email %s to %s, didn't" % (etype, bcc + cc + to)
        return None
    mail = Message(mi["subject"], to, mi["text"], mi["html"], sender=send_from, bcc=bcc, cc=cc)
    if attachments:
        mail.attachments = attachments
    mailer.send(mail)
开发者ID:RaHus,项目名称:portal,代码行数:65,代码来源:emails.py

示例2: send_mail

# 需要导入模块: from pyramid_mailer.message import Message [as 别名]
# 或者: from pyramid_mailer.message.Message import attachments [as 别名]
def send_mail(to, etype, request, data=None):
    add_cc = ['mentoring_request', 'mentoring_request_intro']
    if data is None:
        data = {}
    mi = emails.get(etype, 'data').copy()

    if mi.get('mailer'):
        mailer = mailer_factory_from_settings(mi.get('mailer'), '')
    else:
        mailer = get_mailer(request)

    if 'user' not in data:
        from alchemist.models import User
        data['user'] = User.fr(request)

    if request.authenticated_userid and data['user'].id != request.authenticated_userid:
        from alchemist.models import User
        data['auth_user'] = User.fr(request)

    for k, v in mi.iteritems():
        if callable(v):
            v = v(data, request)
        if '/templates/' in v:
            v = render(v, data, request)
        elif isinstance(v, basestring):
            if '{{' in v and '}}' in v:
                v = Template(v).render(**data)
            else:
                v = v.format(**data)
        mi[k] = v

    if 'attachments' in data:
        attachments = data['attachments']
    elif 'attachments' in mi and mi['attachments'] and mi['attachments'] != 'None':
        with open(os.path.dirname(__file__) + mi['attachments']) as fo:
            a_file = fo.read()
        attachments = [Attachment(os.path.basename(mi['attachments']),
                                  'application/' + mi['attachments'][-3:],
                                  a_file)]
    else:
        attachments = None
    if not isinstance(to, list):
        to = to.split(',')

    from alchemist.models.settings import Settings

    send_from = mi.get('from')
    bcc = [bc for bc in mi.get('bcc', '').split(',') if bc]
    sender = Settings.get('admin_mail', '[email protected]')
    sender = sender.split(',')
    main_admin_mail = sender[0].strip()
    if main_admin_mail:
        bcc.append(main_admin_mail)
    if not send_from:
        send_from = main_admin_mail or '[email protected]'
    cc = mi.get('cc') if mi.get('cc') else []
    if '@' not in cc and not isinstance(cc, list):
        cc = Settings.get(cc, cc).split(',')
    if not cc and etype in add_cc:
        cc = [email.strip() for email in Settings.get('cc_emails', '').split(',')]
    mail = Message(mi['subject'], to, mi['text'], mi['html'],
                   sender=send_from,
                   bcc=bcc, cc=cc)
    if attachments:
        mail.attachments = attachments
    mailer.send(mail)
开发者ID:umar93132,项目名称:portal,代码行数:68,代码来源:misc.py


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