Python脚本发送邮件

时间:2018-06-04 06:14:27

标签: python email smtp send

import smtplib

server = smtplib.SMTP('smtp.gmail.com', 587)

server.ehlo()

server.starttls()

server.login("******@gmail.com", "*******")

msg = "Hello!"

server.sendmail("rajesh.debugs@gmail.com", "rjucsm@gmail.com", msg)

输出:

C:\Users\Admin\PycharmProjects\Gabbar\venv\Scripts\python.exe C:/Users/Admin/PycharmProjects/Gabbar/MadhuBhai/t3.py
Traceback (most recent call last):
  File "C:/Users/Admin/PycharmProjects/Gabbar/MadhuBhai/t3.py", line 5, in <module>
    server.login("******@gmail.com", "******")
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python36-32\lib\smtplib.py", line 730, in login
    raise last_exception
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python36-32\lib\smtplib.py", line 721, in login
    initial_response_ok=initial_response_ok)
  File "C:\Users\Admin\AppData\Local\Programs\Python\Python36-32\lib\smtplib.py", line 642, in auth
    raise SMTPAuthenticationError(code, resp)
smtplib.SMTPAuthenticationError: (534, b'5.7.14 <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbtX\n5.7.14 HDzghr7H0UegF2rvoxWT6p9FwK8ct-IgZQXTa09qiineo743EE4PjOLOukbW-7fN2_FfIx\n5.7.14 qBwOghPCGmq1zlaUP3231EHWXgeut6dhRtiEjEVKAd-VKglbnUqvCyPMLKlADKhWt56L_5\n5.7.14 afzoYLGapj8SmZxp_W6VMrkj10aK9xthTsrmUerV9bkqgILAnKh9SWOO2n-7WsHO43reIf\n5.7.14 MQqmW0G2lyXQWbYt-8LxUHRt3ATI8> 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 v5-v6sm6337091pfd.1 - gsmtp')

Process finished with exit code 1

3 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

您需要email.mimesmtp一起使用。请尝试以下

from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText 
from email.mime.image import MIMEImage
import smtplib

strFrom = 'xyz.uvw@gmail.com'
strTo = ['abc.efg@gmail.com','hij.lmn@gmail.com]     
attachment = '<path to attachment if any>'

msgRoot = MIMEMultipart('related')
msgRoot['Subject'] = 'TEST'
msgRoot['From'] = strFrom
msgRoot['To'] = ", ".join(strTo)
msgRoot.preamble = 'This is a multi-part message in MIME format.'

msgAlternative = MIMEMultipart('alternative')
msgRoot.attach(msgAlternative)

msgText = MIMEText('This is the alternative plain text message.')
msgAlternative.attach(msgText)

msgText = MIMEText('<b>Summary</b>, 'html')
msgAlternative.attach(msgText)

fp = open(attachment, 'rb')  
msgImage = MIMEImage(fp.read())
fp.close()

msgImage.add_header('Content-ID', '<image1>')
msgRoot.attach(msgImage)

smtp = smtplib.SMTP("YOUREMAILHOST", 25, timeout=120)
smtp.sendmail(strFrom, strTo, msgRoot.as_string())
smtp.close()

答案 2 :(得分:0)

如果您未使用smtplib,我会建议SendGrid发送电子邮件(如果您每天发送<100封电子邮件,则免费)。

import sendgrid
from sendgrid.helpers.mail import *

sg = sendgrid.SendGridAPIClient(apikey=sendgrid_api_key)
from_email = Email("test@example.com")
to_email = Email("test@example.com")
subject = "Sending with SendGrid is Fun"
content = Content("text/plain", "content")
mail = Mail(from_email, subject, to_email, content)
response = sg.client.mail.send.post(request_body=mail.get())

print(response.status_code)