我正在尝试使用SMTP通过python发送电子邮件。 这是我的代码:
import smtplib
from email.mime.text import MIMEText
textfile='msg.txt'
you='ashwin@gmail.com'
me='ashwin@gmail.com'
fp = open(textfile, 'rb')
msg = MIMEText(fp.read())
fp.close()
# me == the sender's email address
# you == the recipient's email address
msg['Subject'] = 'The contents of %s' % textfile
msg['From'] = me
msg['To'] = you
print "reached before s"
s = smtplib.SMTP('127.0.0.1',8000)
print "reached after s"
s.sendmail(me, [you], msg.as_string())
s.quit()
当我尝试执行此代码时,"在s"之前到达打印然后进入无限循环左右,即"在s"之后到达没有打印,程序仍在运行。 这是服务器的代码:
import sys
import BaseHTTPServer
from SimpleHTTPServer import SimpleHTTPRequestHandler
HandlerClass = SimpleHTTPRequestHandler
ServerClass = BaseHTTPServer.HTTPServer
Protocol = "HTTP/1.0"
if sys.argv[1:]:
port = int(sys.argv[1])
else:
port = 8000
server_address = ('127.0.0.1', port)
HandlerClass.protocol_version = Protocol
httpd = ServerClass(server_address, HandlerClass)
sa = httpd.socket.getsockname()
print "Serving HTTP on", sa[0], "port", sa[1], "..."
httpd.serve_forever()
有人可以找出问题所在吗?
答案 0 :(得分:0)
我相信yagmail(免责声明:我是维护者)对您有很大的帮助。
它的目标是让发送带附件的电子邮件变得容易。
$array = $_POST['selectedImageArray'];
$sql = $dbh->prepare("INSERT INTO pending_trades (steam_id, trade_items, is_giving, is_receiving, has_started) VALUES (:steamid, :itemlist, '1', '0', '0')");
$sql->bindParam(':steamid', $steamprofile['steamid']);
$sql->bindParam(':itemlist', $array);
$sql->execute();
}
确实只有三行。
请注意,yagmail import yagmail
yag = yagmail.SMTP('ashwin@gmail.com', 'yourpassword')
yag.send(to = 'ashwin@gmail.com', subject ='The contents of msg.txt', contents = 'msg.txt')
将尝试加载传递的字符串。如果它不能,它将作为文本发送,如果它可以加载它,它将附加它。
如果传递字符串列表,它会尝试将每个字符串作为文件加载(或者,再次只显示文本)。
使用{3}}或contents
安装Python 3。
github的更多信息。
答案 1 :(得分:0)
这是一种创建电子邮件发件人类的不同方法,其中包含所有错误处理:
import getpass
import smtplib
class EmailBot(object):
# Gets the username and password and sets up the Gmail connection
def __init__(self):
self.username = \
raw_input('Please enter your Gmail address and hit Enter: ')
self.password = \
getpass.getpass('Please enter the password for this email account and hit Enter: ')
self.server = 'smtp.gmail.com'
self.port = 587
print 'Connecting to the server. Please wait a moment...'
try:
self.session = smtplib.SMTP(self.server, self.port)
self.session.ehlo() # Identifying ourself to the server
self.session.starttls() # Puts the SMTP connection in TLS mode. All SMTP commands that follow will be encrypted
self.session.ehlo()
except smtplib.socket.gaierror:
print 'Error connecting. Please try running the program again later.'
sys.exit() # The program will cleanly exit in such an error
try:
self.session.login(self.username, self.password)
del self.password
except smtplib.SMTPAuthenticationError:
print 'Invalid username (%s) and/or password. Please try running the program again later.'\
% self.username
sys.exit() # program will cleanly exit in such an error
def send_message(self, subject, body):
try:
headers = ['From: ' + self.username, 'Subject: ' + subject,
'To: ' + self.username, 'MIME-Version: 1.0',
'Content-Type: text/html']
headers = '\r\n'.join(headers)
self.session.sendmail(self.username, self.username, headers + '''\r\r''' + body)
except smtplib.socket.timeout:
print 'Socket error while sending message. Please try running the program again later.'
sys.exit() # Program cleanly exits
except:
print "Couldn't send message. Please try running the program again later."
sys.exit() # Program cleanly exits
您需要做的就是创建一个EmailBot实例并在其上调用send_message方法,并将相应的详细信息作为输入。