2016年1月19日 星期二

write gmail by using python on raspberry


如果當raspberry 偵測到警報時(有煙霧時)....可以透過gmail馬上通知你.....

可以參考下面的python code



=========================================================

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Send email via smtp_host."""
import smtplib
from email.mime.text import MIMEText
from email.header    import Header

####smtp_host = 'smtp.live.com'        # microsoft
smtp_host = 'smtp.gmail.com'       # google
####smtp_host = 'smtp.mail.yahoo.com'  # yahoo
login = "your_gmail_account@gmail.com"
#password = raw_input(">>> Type your gmail password:")
password = "your_password"
recipients_emails = [login]

msg = MIMEText('body…', 'plain', 'utf-8')
msg['Subject'] = Header('subject…', 'utf-8')
msg['From'] = login
msg['To'] = ", ".join(recipients_emails)

s = smtplib.SMTP(smtp_host, 587, timeout=10)
s.set_debuglevel(1)
try:
    s.ehlo()
    s.starttls()
    s.ehlo()
    s.login(login, password)
    s.sendmail(msg['From'], recipients_emails, msg.as_string())
finally:
    s.quit()

=====================================================

你需要改三個地方.....

1 :  login = "your_gmail_account@gmail.com"
2 :  password = "your_password"
3 :  msg = MIMEText('body…', 'plain', 'utf-8')

把  你的帳號和密碼改上去後....然後把你要傳的message 換上去('body...').....

執行之後....大概就會得到下面的error message 



大概是說你被gmail 擋了下來....

把關鍵字再去google

可以看到這一篇


有位帥哥回文說 :

Google blocks sign-in attempts from apps which do not use modern security standards (mentioned on their support page). You can however, turn on/off this safety feature by going to the link below:



選擇打開之後.....再執行一次就可以順利傳送gmail

1 則留言:

  1. If an app or website doesn’t meet Goole's security standards, Google might block anyone who’s trying to sign in to your account from it. So, SMTPAuthenticationError exception thrown when you attempt to login to Gmail via Python smtp library. Less secure apps can make it easier for hackers to get in to your account, so blocking sign-ins from these apps helps keep your account safe.

    Go to this link and select Turn On
    https://www.google.com/settings/security/lesssecureapps

    Moreover google block an ip when you try to send a email since a unusual location, so, you can unblock in the next link
    https://support.google.com/accounts/answer/6009563

    and clicked in

    accounts.google.com/DisplayUnlockCaptcha .

    回覆刪除