2016-06-12 8 views
0

私は基本的な電子メールを次のコードとともにpythonで送信しようとしていましたが、ブラウザを使用してサインインするとエラーが発生します。私はそれをやったが、同じエラーを吐き出す。 コード:Pythonで電子メールを送信する簡単なスクリプト

import smtplib 
server = smtplib.SMTP('smtp.gmail.com', 587) 
server.starttls() 
server.login("[email protected]", "redacted") 

msg = "Your security system has detected an intrusion of some kind please refere to http://projetotig6.orgfree.com/ for an image of the current situation" 
server.sendmail("[email protected]", "[email protected]", msg) 
server.quit() 

そして、私はそれが

[email protected]:~/Desktop$ python3 tent.py 
Traceback (most recent call last): 
    File "tent.py", line 5, in <module> 
    server.login("[email protected]", "redacted") 
    File "/usr/lib/python3.4/smtplib.py", line 639, in login 
    raise SMTPAuthenticationError(code, resp) 
smtplib.SMTPAuthenticationError: (534, b'5.7.14 <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbtT\n5.7.14 NJKUGchcZYhLMqcl9utRyvWa6jwkzOCuhBtdP3URW63HNIVrSlue8To8yKxxbDIwvlnO2g\n5.7.14 V2GooN21jsn0X8_d6W_CxGwuOXmBrkoDzFCqqbB72xz3MbY0Kj3Z7ZzdXlQCc8sjdavwes\n5.7.14 bUlIhA8GIqRKJAyBbildXtsSqF8Fh_7AYPI0W3SKlidhoUOK7o4hI0IIUmgVdqOCpFpxyU\n5.7.14 fvShqxoTn6W_bAWqXfS5akND40-gQ> Please log in via your web browser and\n5.7.14 then try again.\n5.7.14 Learn more at\n5.7.14 https://support.google.com/mail/answer/78754 c142sm10923092wme.18 - gsmtp') 
+1

https://support.google.com/mail/answer/78775?hl=en – danidee

+0

は、私はすでにそのページ上のすべてのものを踏襲言及し忘れました。私はPOPとIMAPを有効にしました。添付ファイルを送信しないSMTPサーバーに接続できます。エラーを引き起こす可能性があるのはssl/TSLのことだけですが、私はそれを理解していないのでここに投稿しました。 – grenskul

+0

あなたはパブリックであなたのパスワードを表示していることに気づきます... – danidee

答えて

0

まず、あなたは「安全性の低いアプリはGmailに認証できるように、」確認して実行すると、それは私にこれを与える:https://support.google.com/accounts/answer/6010255?hl=en

の場合それは問題ではない、私はPythonで電子メールを送信するために書いたこのラッパーを使用してみてください。 https://github.com/krystofl/krystof-utils/blob/master/python/krystof_email_wrapper.py

をそうのようにそれを使用してください:あなたはまた、それがどのように動作するかを見つけるために、ソースコードを見ることができます

emailer = Krystof_email_wrapper() 

email_body = 'Hello, <br /><br />' \ 
      'This is the body of the email.' 

emailer.create_email('[email protected]', 'Sender Name', 
        ['[email protected]'], 
        email_body, 
        'Email subject!', 
        attachments = ['filename.txt']) 

cred = { 'email': '[email protected]ample.com', 'password': 'VERYSECURE' } 

emailer.send_email(login_dict = cred, force_send = True) 

それは超簡単電子メールを送信することができます。 関連ビット:

import email 
import smtplib # sending of the emails 
from email.mime.multipart import MIMEMultipart 
from email.mime.text  import MIMEText 

self.msg = MIMEMultipart() 

self.msg.preamble = "This is a multi-part message in MIME format." 

# set the sender 
author = email.utils.formataddr((from_name, from_email)) 
self.msg['From'] = author 

# set recipients (from list of email address strings) 
self.msg['To' ] = ', '.join(to_emails) 
self.msg['Cc' ] = ', '.join(cc_emails) 
self.msg['Bcc'] = ', '.join(bcc_emails) 

# set the subject 
self.msg['Subject'] = subject 

# set the body 
msg_text = MIMEText(body.encode('utf-8'), 'html', _charset='utf-8') 
self.msg.attach(msg_text) 

# send the email 
session = smtplib.SMTP('smtp.gmail.com', 587) 
session.ehlo() 
session.starttls() 
session.login(login_dict['email'], login_dict['password']) 
session.send_message(self.msg) 
session.quit() 
関連する問題