我正在尝试使用python发送电子邮件(Gmail),但我收到以下错误.
Traceback (most recent call last):
File "emailSend.py", line 14, in <module>
server.login(username,password)
File "/usr/lib/python2.5/smtplib.py", line 554, in login
raise SMTPException("SMTP AUTH extension not supported by server.")
smtplib.SMTPException: SMTP AUTH extension not supported by server.
Run Code Online (Sandbox Code Playgroud)
Python脚本如下.
import smtplib
fromaddr = 'user_me@gmail.com'
toaddrs = 'user_you@gmail.com'
msg = 'Why,Oh why!'
username = 'user_me@gmail.com'
password = 'pwd'
server = smtplib.SMTP('smtp.gmail.com:587')
server.starttls()
server.login(username,password)
server.sendmail(fromaddr, toaddrs, msg)
server.quit()
Run Code Online (Sandbox Code Playgroud) 如何使用Python在电子邮件中发送HTML内容?我可以发送简单的文字.
此代码可以正常工作并向我发送电子邮件:
import smtplib
#SERVER = "localhost"
FROM = 'monty@python.com'
TO = ["jon@mycompany.com"] # must be a list
SUBJECT = "Hello!"
TEXT = "This message was sent with Python's smtplib."
# Prepare actual message
message = """\
From: %s
To: %s
Subject: %s
%s
""" % (FROM, ", ".join(TO), SUBJECT, TEXT)
# Send the mail
server = smtplib.SMTP('myserver')
server.sendmail(FROM, TO, message)
server.quit()
Run Code Online (Sandbox Code Playgroud)
但是,如果我尝试将其包装在这样的函数中:
def sendMail(FROM,TO,SUBJECT,TEXT,SERVER):
import smtplib
"""this is some test documentation in the function"""
message = """\
From: %s
To: …Run Code Online (Sandbox Code Playgroud) 如何发送utf8电子邮件?
import sys
import smtplib
import email
import re
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
def sendmail(firm, fromEmail, to, template, subject, date):
with open(template, encoding="utf-8") as template_file:
message = template_file.read()
message = re.sub(r"{{\s*firm\s*}}", firm, message)
message = re.sub(r"{{\s*date\s*}}", date, message)
message = re.sub(r"{{\s*from\s*}}", fromEmail, message)
message = re.sub(r"{{\s*to\s*}}", to, message)
message = re.sub(r"{{\s*subject\s*}}", subject, message)
msg = MIMEMultipart("alternative")
msg.set_charset("utf-8")
msg["Subject"] = subject
msg["From"] = fromEmail
msg["To"] = to
#Read from template
html = message[message.find("html:") + len("html:"):message.find("text:")].strip()
text = …Run Code Online (Sandbox Code Playgroud) 我的目标是使用Python向具有内嵌图像的Gmail用户发送电子邮件.href由于图像的敏感性(来自我工作的数据),无法在线托管此图像然后通过a链接到该图像.
我已经尝试将该base64版本编码为HTML然后发送HTML,但众所周知,这不起作用.然后我注意到,在Gmail中,您可以将图像拖放到发送框中,它将在接收端显示为内联.鉴于此,我尝试从Python发送一封电子邮件,并将图像作为附件.这可以在下面的代码中看到,但不幸的是图像没有显示为内联.
我的问题是:如何发送图像使其显示为内联?
import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email import Encoders
import os
gmail_user = "user1@gmail.com"
gmail_pwd = "pass"
to = "user2@gmail.com"
subject = "Report"
text = "Picture report"
attach = 'TESTING.png'
msg = MIMEMultipart()
msg['From'] = gmail_user
msg['To'] = to
msg['Subject'] = subject
msg.attach(MIMEText(text))
part = MIMEBase('application', 'octet-stream')
part.set_payload(open(attach, 'rb').read())
Encoders.encode_base64(part)
part.add_header('Content-Disposition',
'attachment; filename="%s"' % os.path.basename(attach))
msg.attach(part)
mailServer = smtplib.SMTP("smtp.gmail.com", …Run Code Online (Sandbox Code Playgroud) 我正在编写一个使用Python发送电子邮件的程序.我从各种论坛中学到的是以下代码:
#!/usr/bin/env python
import smtplib
sender = "sachinites@gmail.com"
receivers = ["abhisheks@cse.iitb.ac.in"]
yourname = "Abhishek Sagar"
recvname = "receptionist"
sub = "Testing email"
body = "who cares"
message = "From: " + yourname + "\n"
message = message + "To: " + recvname + "\n"
message = message + "Subject: " + sub + "\n"
message = message + body
try:
print "Sending email to " + recvname + "...",
server = smtplib.SMTP('smtp.gmail.com:587')
username = 'XYZ@gmail.com'
password = '*****'
server.ehlo()
server.starttls() …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 python 3.2 SMTPlib.sendmail() 函数发送消息,在对 SMTP 库进行一些修改后(即注释掉抑制错误消息的 rset() 函数)我设法检索到以下错误消息从服务器:
SendMail 失败(554,b'交易失败:由于可能的滥用而无法发送邮件;请访问http://postmaster.yahoo.com/abuse_smtp.html了解更多信息')
雅虎邮件 SMTP 服务器认为我在发送垃圾邮件,该 URL 确实链接到任何有用的东西。我认为这与标题不足有关,我似乎无法找到关于什么构成合规标题的明确答案,而且我已经阅读了 Gmail 的类似问题。此帖子已替换为模拟电子邮件。
任何帮助,将不胜感激
我的完整代码如下:
self.message = email.message_from_string('''To: <ksmith@yahoo.co.nz>
From: <rwilson@yahoo.co.nz>
Reply-To: <rwilson@yahoo.co.nz>
Subject: Test send mail \n\n Hello''')
fromAddress = 'rwilson@yahoo.co.nz'
toAddress = 'ksmith@yahoo.co.nz'
try:
self.smtp = SMTP()
self.smtp.connect('smtp.mail.yahoo.com')
except Exception:
print('Connection Failed')
print(traceback.format_exc())
try:
self.smtp.login('rwilson','tree22')
except Exception:
print('Login Failed!')
print(traceback.format_exc())
try:
self.smtp.sendmail(fromAddress,toAddress ,self.message.as_string())
print("Message sucessfully sent!")
self.smtp.close()
except Exception as e:
print('SendMail Failed')
print(e)
Run Code Online (Sandbox Code Playgroud) email ×7
python ×7
smtp ×3
gmail ×2
smtplib ×2
function ×1
html-email ×1
image ×1
mime ×1
python-3.2 ×1
utf-8 ×1
yahoo-mail ×1