本文整理汇总了Python中sendgrid.Mail.add_attachment_stream方法的典型用法代码示例。如果您正苦于以下问题:Python Mail.add_attachment_stream方法的具体用法?Python Mail.add_attachment_stream怎么用?Python Mail.add_attachment_stream使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sendgrid.Mail
的用法示例。
在下文中一共展示了Mail.add_attachment_stream方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_send
# 需要导入模块: from sendgrid import Mail [as 别名]
# 或者: from sendgrid.Mail import add_attachment_stream [as 别名]
def test_send(self):
m = Mail()
m.add_to('John, Doe <[email protected]>')
m.set_subject('test')
m.set_html('WIN')
m.set_text('WIN')
m.set_from('[email protected]')
m.add_substitution('subKey', 'subValue')
m.add_section('testSection', 'sectionValue')
m.add_category('testCategory')
m.add_unique_arg('testUnique', 'uniqueValue')
m.add_filter('testFilter', 'filter', 'filterValue')
m.add_attachment_stream('testFile', 'fileValue')
self.sg.send(m)
url = self.sg._build_body(m)
url.pop('api_key', None)
url.pop('api_user', None)
url.pop('date', None)
testUrl = json.loads('''{"to[]": ["[email protected]"],
"toname[]": ["John Doe"],
"html": "WIN",
"text": "WIN",
"subject": "test",
"files[testFile]": "fileValue",
"from": "[email protected]",
"headers": "",
"fromname": "",
"replyto": ""}''')
testUrl['x-smtpapi'] = json.dumps(json.loads('''{"sub":{"subKey":["subValue"]},
"section":{"testSection":"sectionValue"},
"category":["testCategory"],
"unique_args":{"testUnique":"uniqueValue"},
"filters":{"testFilter":{"settings":{"filter":"filterValue"}}}}'''))
self.assertEqual(url, testUrl)
示例2: test_send
# 需要导入模块: from sendgrid import Mail [as 别名]
# 或者: from sendgrid.Mail import add_attachment_stream [as 别名]
def test_send(self):
m = Mail()
m.add_to('John, Doe <[email protected]>')
m.set_subject('test')
m.set_html('WIN')
m.set_text('WIN')
m.set_from('[email protected]')
m.set_asm_group_id(42)
m.add_cc('[email protected]')
m.add_bcc('[email protected]')
m.add_substitution('subKey', 'subValue')
m.add_section('testSection', 'sectionValue')
m.add_category('testCategory')
m.add_unique_arg('testUnique', 'uniqueValue')
m.add_filter('testFilter', 'filter', 'filterValue')
m.add_attachment_stream('testFile', 'fileValue')
url = self.sg._build_body(m)
url.pop('api_key', None)
url.pop('api_user', None)
url.pop('date', None)
test_url = json.loads('''
{
"to[]": ["[email protected]"],
"toname[]": ["John Doe"],
"html": "WIN",
"text": "WIN",
"subject": "test",
"files[testFile]": "fileValue",
"from": "[email protected]",
"cc[]": ["[email protected]"],
"bcc[]": ["[email protected]"]
}
''')
test_url['x-smtpapi'] = json.dumps(json.loads('''
{
"sub": {
"subKey": ["subValue"]
},
"section": {
"testSection":"sectionValue"
},
"category": ["testCategory"],
"unique_args": {
"testUnique":"uniqueValue"
},
"filters": {
"testFilter": {
"settings": {
"filter": "filterValue"
}
}
},
"asm_group_id": 42
}
'''))
try:
self.assertItemsEqual(url, test_url)
except: # Python 3+
self.assertCountEqual(url, test_url)
示例3: SGEmailClient
# 需要导入模块: from sendgrid import Mail [as 别名]
# 或者: from sendgrid.Mail import add_attachment_stream [as 别名]
class SGEmailClient(object):
def __init__(self):
self.email_config = SendGridConfiguration.objects.all()[:1].get()
self.sg = SendGridClient(self.email_config.api_key)
self.message = Mail()
self.message.set_from(self.email_config.email_from)
self.message.set_from_name(self.email_config.email_from_name)
self.message.set_subject(self.email_config.email_subject)
def send_report_email(self, email, report):
template_config = TemplateReport.objects.all()[:1].get()
user = get_object_or_404(User, pk=email)
logger.info(user)
html = unicode(template_config.html_template).format(
user_name=user.first_name,
)
self.message.add_to(user.email)
self.message.add_to_name(user.first_name)
self.message.set_html(html)
if report['report']:
self.message.add_attachment_stream(
report['name'] , report['report']
)
status, msg = self.sg.send(self.message)
logger.info('{0} - {1}'.format(status, msg))
示例4: test_send
# 需要导入模块: from sendgrid import Mail [as 别名]
# 或者: from sendgrid.Mail import add_attachment_stream [as 别名]
def test_send(self):
m = Mail()
m.add_to("John, Doe <[email protected]>")
m.set_subject("test")
m.set_html("WIN")
m.set_text("WIN")
m.set_from("[email protected]")
m.add_substitution("subKey", "subValue")
m.add_section("testSection", "sectionValue")
m.add_category("testCategory")
m.add_unique_arg("testUnique", "uniqueValue")
m.add_filter("testFilter", "filter", "filterValue")
m.add_attachment_stream("testFile", "fileValue")
url = self.sg._build_body(m)
url.pop("api_key", None)
url.pop("api_user", None)
url.pop("date", None)
test_url = json.loads(
"""
{
"to[]": ["[email protected]"],
"toname[]": ["John Doe"],
"html": "WIN",
"text": "WIN",
"subject": "test",
"files[testFile]": "fileValue",
"from": "[email protected]"
}
"""
)
test_url["x-smtpapi"] = json.dumps(
json.loads(
"""
{
"to" : ["John, Doe <[email protected]>"],
"sub": {
"subKey": ["subValue"]
},
"section": {
"testSection":"sectionValue"
},
"category": ["testCategory"],
"unique_args": {
"testUnique":"uniqueValue"
},
"filters": {
"testFilter": {
"settings": {
"filter": "filterValue"
}
}
}
}
"""
)
)
self.assertEqual(url, test_url)
示例5: EmailClient
# 需要导入模块: from sendgrid import Mail [as 别名]
# 或者: from sendgrid.Mail import add_attachment_stream [as 别名]
class EmailClient(object):
def __init__(self):
self.sg = SendGridClient(SG_API_KEY)
self.message = Mail()
self.message.set_from(SG_FROM)
self.message.set_from_name(SG_FROM_NAME)
def send_sg_email(self, correo):
# valores de envío
self.message.add_to(correo.correo)
self.message.add_to_name(correo.nombre_cliente)
self.message.set_subject(correo.asunto)
self.message.set_html(correo.html)
# valores personalizados
unique_args = {
'empresa': correo.empresa,
'rut_receptor': correo.rut_receptor,
'rut_emisor': correo.rut_emisor,
'tipo_envio': correo.tipo_envio,
'tipo_dte': correo.tipo_dte,
'numero_folio': correo.numero_folio,
'resolucion_receptor': correo.resolucion_receptor,
'resolucion_emisor': correo.resolucion_emisor,
'monto': correo.monto,
'fecha_emision': correo.fecha_emision,
'fecha_recepcion': correo.fecha_recepcion,
'estado_documento': correo.estado_documento,
'tipo_operacion': correo.tipo_operacion,
'tipo_receptor': correo.tipo_receptor,
}
self.message.set_unique_args(unique_args)
# Validacion de adjuntos
if correo.attachs:
for adjunto in correo.attachs:
adj = AttachModel.query(ancestor=adjunto).get()
self.message.add_attachment_stream(adj.nombre, adj.archivo)
# enviando el mail
status, msg = self.sg.send(self.message)
# imprimiendo respuesta
logging.info(status)
logging.info(msg)
def send_mail_to_user_attach(self, correo):
# valores de envío
self.message.add_to(correo['email'])
self.message.add_to_name(correo['user_name'])
self.message.set_subject(correo['subject'])
self.message.set_html(correo['html'])
if correo['attach']:
self.message.add_attachment_stream(correo['attach']['name'], correo['attach']['file'])
status, msg = self.sg.send(self.message)
# imprimiendo respuesta
logging.info(status)
logging.info(msg)