1.引入第三方模組
2.安裝方式:
pip install yagmail-0.10.212-py2.py3-none-any.whl
說明:存在中文亂碼的問題
3.設定郵箱
1)啟動pop3/smtp服務
2)設定授權碼
4.編寫指令碼,如下圖所示
5.方法解析
1)mail = yagmail.smtp() #實際就是類的例項化
類的建構函式如下所示,詳細的函式,請自行匯入模組進行檢視
class smtp():2)mail.send () #例項化物件呼叫send方法,通過此方法傳送郵件``smtplib``'s smtp connection, and allows messages to be sent."""
def
__init__(self
, user=none, password=none, host='smtp.gmail.com'
, port=none,
smtp_starttls=none, smtp_ssl=true, smtp_set_debuglevel=0
,smtp_skip_login=false, encoding="utf-8"
, oauth2_file=none,
soft_email_validation=true, **kwargs):
self.log = get_logger()
self.set_logging()
if smtp_skip_login and user is none:
user = ''
elif user is none:
user = self._find_user_home_path()
self.user,
self.useralias = self._make_addr_alias_user(user)
self.soft_email_validation = soft_email_validation
if soft_email_validation:
validate_email_with_regex(self.user)
self.is_closed = none
self.host = host
self.port = str(port) if port is not none else
'465'
if smtp_ssl else
'587'
self.smtp_starttls = smtp_starttls
self.ssl = smtp_ssl
self.smtp_skip_login = smtp_skip_login
self.debuglevel = smtp_set_debuglevel
self.encoding = encoding
self.kwargs = kwargs
if oauth2_file is not none:
self.login_oauth2(oauth2_file)
else:
self.login(password)
self.cache = {}
self.unsent =
self.log.info('connected to smtp @ %s:%s as %s'
, self.host,
self.port,
self.user)
self.num_mail_sent = 0
send()函式如下所示:
defsend(self
, to=none, subject=none, contents=none, attachments=none, cc=none, bcc=none,
preview_only=false, headers=none):
""" use this to send an email with gmail"""
addresses = self._resolve_addresses(to, cc, bcc)
if not addresses['recipients']:
return {}
msg = self._prepare_message(addresses, subject, contents, attachments, headers)
if preview_only:
return addresses, msg.as_string()
return
self._attempt_send(addresses['recipients'], msg.as_string())
Python實現自動傳送郵件功能
簡單郵件傳輸協議 smtp 是一種協議,用於在郵件伺服器之間傳送電子郵件和路由電子郵件。python提供smtplib模組,該模組定義了乙個smtp客戶端會話物件,可用於使用smtp或esmtp偵聽器守護程式向任何網際網路機器傳送郵件。smtp通訊的基本流程可以概括為以下幾點 1.連線smtp伺服器...
Python實現傳送郵件的功能(高階)
上篇文章已經介紹了利用python傳送文字訊息的用法,也在文末遺留了如何傳送和附件的問題,本章主要來回答這兩個問題。如何傳送 如何傳送普通附件 def attach picture self,picture path,msg try with open picture path,rb as f im...
python2 7實現郵件傳送功能
要想實現乙個能夠傳送帶有文字 附件的python程式,首先要熟悉兩大模組 email以及smtplib 然後對於mime 郵件擴充套件 要有一定認知,因為有了擴充套件才能傳送附件以及這些 或者非文字資訊 最後乙個比較細節的方法就是mimemultipart,要理解其用法以及對應引數所實現的功能區別 ...