Django 邮件发送

https://docs.djangoproject.com/en/1.10/topics/email/#django.core.mail.EmailMessage

 

注意:

1.

163邮箱,开启了客户端授权,好像不开启这个客户端授权也能发送。开启了这个客户端授权后,代码中的密码要使用这个客户端授权密码,而不是登录邮箱的密码。 

2.

https://help.aliyun.com/knowledge_detail/36687.html?spm=a2c4g.11186623.4.1.aef5ced48esTU0

企业云邮箱默认 SMTP 发信功能已经开启,使用企业邮箱使用与 SMTP 程序进行发信时,涉及的邮箱服务器和设置方法如下: 

1、 SMTP 服务器名称smtp.mxhichina.com,或者smtp.您的邮箱域名;

2、 SMTP 服务器端口:25(SSL加密端口为465);

3、 SMTP 身份验证:必须勾选

4、 SMTP 认证登录用户名(发信邮箱地址):正确、完整的阿里云企业邮箱账号名称,如postmaster@abc.com。

setting.py 配置
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'

EMAIL_USE_SSL = True

EMAIL_HOST = 'smtp.qq.com'  # 如果是 163 改成 smtp.163.com

EMAIL_PORT = 465

EMAIL_HOST_USER = 'xxx@qq.com' # 帐号

EMAIL_HOST_PASSWORD = 'p@ssw0rd'  # 密码

DEFAULT_FROM_EMAIL = EMAIL_HOST_USER

第一种方式

1,

def send_mail(subject, message, from_email, recipient_list,
              fail_silently=False, auth_user=None, auth_password=None,
              connection=None, html_message=None):
send_mail(
    'Subject',
    'Message.',
    'from@example.com',
    ['john@example.com', 'jane@example.com'],
)

2,

datatuple = (
    ('Subject', 'Message.', 'from@example.com', ['john@example.com']),
    ('Subject', 'Message.', 'from@example.com', ['jane@example.com']),
)
send_mass_mail(datatuple)

第二种方式

from django.core.mail import EmailMessage

email = EmailMessage(
    'Hello',
    'Body goes here',
    'from@example.com',
    ['to1@example.com', 'to2@example.com'],
    ['bcc@example.com'],
    reply_to=['another@example.com'],
    headers={'Message-ID': 'foo'},
)
#添加图片
email.attach('design.png', img_data, 'image/png')
#添加附件
email.attach_file('/images/weather_map.png')
#发送 html 格式,设置格式
msg = EmailMessage(subject, html_content, from_email, [to])
msg.content_subtype = "html"  # Main content is now text/html
msg.send()
第三种方式:
from django.core import mail

with mail.get_connection() as connection:
    mail.EmailMessage(
        subject1, body1, from1, [to1],
        connection=connection,
    ).send()
    mail.EmailMessage(
        subject2, body2, from2, [to2],
        connection=connection,
    ).send()
#email=mail.EmailMessage()获取emailmessage instance
#添加附件图片
email.attach('design.png', img_data, 'image/png')
email.attach_file('/images/weather_map.png')

#发送 html 格式,设置格式
email.content_subtype = "html" # Main content is now text/html

msg.send()

第4种方式:携带替代内容类型

from django.core.mail import EmailMultiAlternatives#emailmessage 的子类

subject, from_email, to = 'hello', 'from@example.com', 'to@example.com'
text_content = 'This is an important message.'
html_content = '<p>This is an <strong>important</strong> message.</p>'
msg = EmailMultiAlternatives(subject, text_content, from_email, [to])
msg.attach_alternative(html_content, "text/html")
#添加附件图片
msg.attach('design.png', img_data, 'image/png')
msg.attach_file('/images/weather_map.png')
msg.send()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值