[英]send email using python using Gmail
I have written a python script to send email from my gmail account to my another email.我编写了一个 python 脚本,将电子邮件从我的 gmail 帐户发送到我的另一封电子邮件。 the code is:代码是:
import smtplib
fromaddr = 'my@gmail.com'
toaddrs = 'to@gmail.com'
msg = 'my message'
username = 'my@gmail.com'
password = 'myPass'
server = smtplib.SMTP('smtp.gmail.com:587')
server.ehlo()
server.starttls()
server.login(username,password)
server.sendmail(fromaddr, toaddrs, msg)
server.quit()
print("Done")
I'm also getting the "Done" output as the email is being sent.在发送电子邮件时,我也收到了“完成”输出。 But the problem is: I can't seem to receive the email.但问题是:我似乎无法收到电子邮件。 It doesn't show up in the inbox.它不会显示在收件箱中。 I can't find the problem :( Can anyone please help?我找不到问题:(任何人都可以帮忙吗?
Thanks in advance...提前致谢...
Check this out, this works for me perfectly...看看这个,这对我来说非常有效......
def send_mail(self):
import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
gmailUser = 'myemail@gmail.com'
gmailPassword = 'P@ssw0rd'
recipient = 'sendto@gmail.com'
message='your message here '
msg = MIMEMultipart()
msg['From'] = gmailUser
msg['To'] = recipient
msg['Subject'] = "Subject of the email"
msg.attach(MIMEText(message))
mailServer = smtplib.SMTP('smtp.gmail.com', 587)
mailServer.ehlo()
mailServer.starttls()
mailServer.ehlo()
mailServer.login(gmailUser, gmailPassword)
mailServer.sendmail(gmailUser, recipient, msg.as_string())
mailServer.close()
Enable less secure apps on your gmail account and for Python3 use:在您的 Gmail 帐户和 Python3 上启用安全性较低的应用程序:
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
gmailUser = 'XXXXX@gmail.com'
gmailPassword = 'XXXXX'
recipient = 'XXXXX@gmail.com'
message = f"""
Your message here...
"""
msg = MIMEMultipart()
msg['From'] = f'"Your Name" <{gmailUser}>'
msg['To'] = recipient
msg['Subject'] = "Your Subject..."
msg.attach(MIMEText(message))
try:
mailServer = smtplib.SMTP('smtp.gmail.com', 587)
mailServer.ehlo()
mailServer.starttls()
mailServer.ehlo()
mailServer.login(gmailUser, gmailPassword)
mailServer.sendmail(gmailUser, recipient, msg.as_string())
mailServer.close()
print ('Email sent!')
except:
print ('Something went wrong...')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.