django學習-31.發送滿足【郵件正文值爲一個html頁面】的單個郵件

目錄結構

1.寫這篇博客的目的

2.完整操作流程

2.1.第一步:在【settings.py】裏新增郵箱配置信息

2.2.第二步:在【helloworld/hello/views.py】裏新增視圖函數

2.3.第三步:在【helloworld/helloworld/urls.py】裏新增url匹配規則

2.4.第四步:重啓服務

2.5.第五步:任一瀏覽器上輸入url地址【http://192.168.1.81:8000/send_email_003/】進行訪問後,查看結果

3.相關知識點

3.1.send_mail()函數的源碼簡單分析

 

 

1.寫這篇博客的目的

主要記錄如何通過django來實現這個功能:發送滿足【郵件正文值爲一個html頁面】的單個郵件

發送滿足【郵件正文值爲一個html頁面】的單個郵件,可以使用該函數:send_mail();   

完整操作流程可以看接下來的內容;

 

2.完整操作流程

2.1.第一步:在【settings.py】裏新增郵箱配置信息

# 下面的代碼都是我個人新增的,不是djano框架默認生成的;
# 1.1.配置qq郵箱信息
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'  # 值必須爲這個
EMAIL_USE_SSL = True                         # SSL加密方式,值必須爲True
EMAIL_HOST = 'smtp.qq.com'                   # 發送郵件的qq郵箱的SMTP服務器
EMAIL_PORT = 465                             # QQ郵箱對應的SMTP服務器端口
EMAIL_HOST_USER = '[email protected]'         # 發件人
EMAIL_HOST_PASSWORD = 'dwvvnvjerwcvswp'      # qq授權碼(不能使用qq密碼只能使用qq授權碼)
EMAIL_FROM = '洪景盛<[email protected]>'       # 郵件顯示的發件人

2.2.第二步:在【helloworld/hello/views.py】裏新增視圖函數

from django.core.mail import send_mail,send_mass_mail
from utils.common import Common
common = Common()

def send_email_003(request):
    '''發送郵件正文值爲一個html頁面的單個郵件'''

    html_content = '''
                    <!DOCTYPE HTML>
                    <html>
                    <head>
                        <meta charset="UTF-8">
                        <title>帶圖片的郵件</title>
                    </head>
                    <body>


                    這是第一張圖片,點擊圖片後可跳轉到我的博客首頁<br>
                    <a href="https://www.cnblogs.com/xiamen-momo/" target="_blank">
                        <p>
                        <img src="http://www.w3school.com.cn/i/eg_chinarose.jpg" height="100" width="200" />
                        </p>
                    </a>

                    <p>
                    這是第二張圖片,點擊後不會觸發任何跳轉效果<br>
                    <img src="http://www.w3school.com.cn/i/eg_chinarose.jpg" height=200 width=400 />
                    </p>

                    </body>
                    </html>
                '''

    send_mail(subject="這是洪景盛給你們發的郵件的郵件標題!(郵件正文值爲一個html頁面)",
              message="這是郵件的正文!(當入參html_message的值不爲None,那入參message的值就不會生效)",
              from_email="[email protected]",
              recipient_list=["[email protected]"], # 收件人郵箱可以是任意郵箱
              # recipient_list=["[email protected]","朋友1的[email protected]","朋友2的[email protected]","朋友3 的@qq.com"]
              html_message= html_content
              )

    now_time = common.now_time_of_y_m_d_H_M_S()
    return HttpResponse("郵件正文值爲一個html頁面的郵件發送成功!發送時間爲:%s"%now_time)

2.3.第三步:在【helloworld/helloworld/urls.py】裏新增url匹配規則

url(r"^send_email_003/$",views.send_email_003),

2.4.第四步:重啓服務

2.5.第五步:任一瀏覽器上輸入url地址【http://192.168.1.81:8000/send_email_003/】進行訪問後,查看結果

 

3.相關知識點

3.1.send_mail()函數的源碼簡單分析

def send_mail(subject, message, from_email, recipient_list,
              fail_silently=False, auth_user=None, auth_password=None,
              connection=None, html_message=None):
    """
    Easy wrapper for sending a single message to a recipient list. All members
    of the recipient list will see the other recipients in the 'To' field.

    If from_email is None, use the DEFAULT_FROM_EMAIL setting.
    If auth_user is None, use the EMAIL_HOST_USER setting.
    If auth_password is None, use the EMAIL_HOST_PASSWORD setting.

    Note: The API for this method is frozen. New code wanting to extend the
    functionality should use the EmailMessage class directly.
    """
    connection = connection or get_connection(
        username=auth_user,
        password=auth_password,
        fail_silently=fail_silently,
    )
    mail = EmailMultiAlternatives(subject, message, from_email, recipient_list, connection=connection)
    if html_message:
        mail.attach_alternative(html_message, 'text/html')

    return mail.send()

截取這部分源碼:

    mail = EmailMultiAlternatives(subject, message, from_email, recipient_list, connection=connection)
    if html_message:
        mail.attach_alternative(html_message, 'text/html')

    return mail.send()

從這部分源碼可以簡單看出來:當入參html_message值不爲空,會調用【EmailMultiAlternatives】類提供的【attach_alternative】方法,從而實現最終發送郵件正文爲一個html頁面的郵件的功能;

 

入參message值和入參html_message值的組合,主要有這兩個場景:

  1. 當函數send_mail()的入參message的值不爲空,入參html_message的值也不爲空,那麼:入參message的值不生效,入參htm_message的值生效;
  2. 當函數send_mail()的入參message的值不爲空,入參html_message的值爲默認值None,那麼:入參message的值生效,入參htm_message的值不生效;

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章