本文整理汇总了Python中smtplib.SMTP.rcpt方法的典型用法代码示例。如果您正苦于以下问题:Python SMTP.rcpt方法的具体用法?Python SMTP.rcpt怎么用?Python SMTP.rcpt使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类smtplib.SMTP
的用法示例。
在下文中一共展示了SMTP.rcpt方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: sendn
# 需要导入模块: from smtplib import SMTP [as 别名]
# 或者: from smtplib.SMTP import rcpt [as 别名]
def sendn(mail_from, ffrom, x_smtpapi, reply_to, subject, content, files, custom_headers={}):
ret = {}
s = SMTP('%s:%d' % (HOST, PORT))
s.set_debuglevel(DEBUG_MODE)
if USE_SSL:
s.starttls()
s.login(API_USER, API_KEY)
msg = MIMEMultipart('alternative')
msg['subject'] = subject
msg['from'] = ffrom
msg['reply-to'] = reply_to
for k, v in custom_headers.items():
msg[k] = Header(v)
msg['X-SMTPAPI'] = Header(base64.b64encode(simplejson.dumps(x_smtpapi)))
part = MIMEText(content, 'html', 'utf8')
msg.attach(part)
for f in files:
part = MIMEBase('application', 'octet-stream') # 'octet-stream': binary data
part.set_payload(open(f, 'rb').read())
encoders.encode_base64(part)
# 如果附件名称含有中文, 则 filename 要转换为gb2312编码, 否则就会出现乱码
# unicode转换方法: basename.encode('gb2312')
# utf-8转换方法: basename.decode('utf-8').encode('gb2312')
filename = os.path.basename(f)
part.add_header(
'Content-Disposition',
'attachment; filename="%s"' %
filename.decode('utf8').encode('gb2312'))
msg.attach(part)
s.mail(mail_from)
# now, rcpt_to is useless due to X_SMTPAPI
s.rcpt(x_smtpapi['to'][0])
reply_data = s.data(msg.as_string())
s.quit()
message_id = _message_id(reply_data)
for rcpt_to in x_smtpapi['to']:
ret[rcpt_to] = message_id + str(x_smtpapi['to'].index(rcpt_to)) + '$' + rcpt_to
print ret
return ret
示例2: send
# 需要导入模块: from smtplib import SMTP [as 别名]
# 或者: from smtplib.SMTP import rcpt [as 别名]
def send(mail_from, ffrom, rcpt_tos, reply_to, subject, content, files):
ret = {}
s = SMTP('%s:%d' % (HOST, PORT))
s.set_debuglevel(DEBUG_MODE)
if USE_SSL:
s.starttls()
s.login(API_USER, API_KEY)
for rcpt_to in rcpt_tos:
ret[rcpt_to] = None
msg = MIMEMultipart('alternative')
msg['subject'] = subject
msg['from'] = ffrom
msg['reply-to'] = reply_to
msg['to'] = rcpt_to
part = MIMEText(content, 'html', 'utf8')
msg.attach(part)
for f in files:
part = MIMEBase('application', 'octet-stream') # 'octet-stream': binary data
part.set_payload(open(f, 'rb').read())
encoders.encode_base64(part)
# 如果附件名称含有中文, 则 filename 要转换为gb2312编码, 否则就会出现乱码
# unicode转换方法: basename.encode('gb2312')
# utf-8转换方法: basename.decode('utf-8').encode('gb2312')
filename = os.path.basename(f)
part.add_header(
'Content-Disposition',
'attachment; filename="%s"' %
filename.decode('utf8').encode('gb2312'))
msg.attach(part)
s.mail(mail_from)
s.rcpt(rcpt_to)
reply_data = s.data(msg.as_string())
s.rset()
message_id = _message_id(reply_data)
ret[rcpt_to] = message_id + '0$' + rcpt_to
s.quit()
print ret
return ret
示例3: send
# 需要导入模块: from smtplib import SMTP [as 别名]
# 或者: from smtplib.SMTP import rcpt [as 别名]
def send(mail_from, ffrom, rcpt_to, subject, content):
ret = {}
s = SMTP('%s:%d' % (HOST, PORT))
s.login(API_USER, API_KEY)
msg = MIMEMultipart('alternative')
msg['subject'] = subject
msg['from'] = ffrom
msg['to'] = rcpt_to
part = MIMEText(content, 'html', 'utf8')
msg.attach(part)
s.mail(mail_from)
s.rcpt(rcpt_to)
print s.data(msg.as_string())
s.rset()
s.quit()
示例4: GoodGrammarTests
# 需要导入模块: from smtplib import SMTP [as 别名]
# 或者: from smtplib.SMTP import rcpt [as 别名]
class GoodGrammarTests(unittest.TestCase):
"""Collection of tests of valid SMTP grammar (i.e. they should not generate any error responses from server)"""
addrs = [
'[email protected]', '[email protected]', '[email protected]',
'[email protected].com', '[email protected]'
]
def setUp(self):
self.smtp = SMTP('localhost', 1025)
def tearDown(self):
# self.smtp.quit()
self.smtp.close()
self.smtp = None
def assertOk(self, result):
self.assertEqual(result, (250, 'Ok'))
def testConnect(self):
"""On connecting the server sends a 220 response with a welcome message."""
smtp = SMTP();
self.assertEqual(smtp.connect('localhost', 1025), (220, 'test node.js smtpevent server 0.0.2'))
smtp.quit();
smtp.close()
def testHelo(self):
"""The server responds to a valid HELO command."""
self.assertEqual(self.smtp.helo('example.com'), (250, 'test Hello 127.0.0.1'))
def testNoop(self):
"""The NOOP command takes no arguments."""
self.assertOk(self.smtp.noop())
def testQuit(self):
"""The QUIT command works without an argument"""
self.assertEqual(self.smtp.quit(), (221, 'test closing connection'))
def testQuitWithArgument(self):
"""The QUIT command works with an argument"""
self.assertEqual(self.smtp.docmd('QUIT', 'See you later'), (221, 'test closing connection'))
def testRset(self):
"""The RSET command takes no arguments."""
self.assertOk(self.smtp.rset())
def testMailFrom(self):
"""The MAIL command will extract the email address from the FROM:."""
self.assertEqual(self.smtp.mail('[email protected]'), (250, 'Ok'))
def testMailFromEmpty(self):
"""The MAIL command handles empty addresses"""
self.assertEqual(self.smtp.mail('<>'), (250, 'Ok'))
def testMultipleRcpts(self):
"""Multiple RCPT commands can be issued to add recipients."""
self.assertOk(self.smtp.docmd('MAIL', 'FROM:<[email protected]>'))
for rcpt in self.addrs:
self.assertOk(self.smtp.docmd('RCPT', 'TO:<%s>' % rcpt))
def testDataResponse(self):
"""The DATA instructs the self.smtp to end the message with <CR><LF>.<CR><LF>."""
self.assertOk(self.smtp.mail('[email protected]'))
self.assertOk(self.smtp.rcpt('[email protected]'))
self.assertEqual(self.smtp.docmd('DATA'), (354, 'End data with <CR><LF>.<CR><LF>'))
示例5: MailTool
# 需要导入模块: from smtplib import SMTP [as 别名]
# 或者: from smtplib.SMTP import rcpt [as 别名]
class MailTool(object):
"""一个发邮件的工具类
"""
def __init__(self, host, port, user, password, ssl=False):
"""初始化客户端连接
"""
# #timeout=socket._GLOBAL_DEFAULT_TIMEOUT
# self.timeout = timeout
self.client = SMTP(host=host, port=port)
if ssl:
self.client.starttls()
try:
self.client = SMTP(host, port)
print self.client.ehlo()
print self.client.login(user, password)
# 设置邮件服务的发送者为发送账号
self.sender = user
except SMTPConnectError as e:
logging.error('SMTPConnectError({0}): on {1}:{2}'.format(e.message, host, port))
except SMTPAuthenticationError as e:
logging.error('SMTPAuthenticationError({0}): on {1}:{2}'.format(e.message, user, password))
print e
raw_input("Mail Account Authentication Failed! Press Enter to Quit:(")
exit(-1)
self.client.set_debuglevel(1) # debug=True
def close(self):
"""释放连接
"""
self.client.close()
# self.client.quit()
@staticmethod
def msg(ffrom, rcpt_tos, reply_to, subject, content, files):
"""生成待发送内容
"""
msg = MIMEMultipart('alternative')
msg['subject'] = subject
msg['from'] = ffrom
msg['reply-to'] = reply_to
msg['to'] = ','.join(rcpt_tos).strip()
if len(content) > 0:
part = MIMEText(content, 'html', 'utf8')
# print 'content:', content
msg.attach(part)
if files != None:
for f in files:
part = MIMEBase('application', 'octet-stream') #'octet-stream': binary data
part.set_payload(open(f, 'rb').read())
encoders.encode_base64(part)
# 如果附件名称含有中文, 则 filename 要转换为gb2312编码, 否则就会出现乱码
# unicode转换方法: basename.encode('gb2312')
# utf-8转换方法: basename.decode('utf-8').encode('gb2312')
filename = os.path.basename(f)
part.add_header('Content-Disposition', 'attachment; filename="%s"' % filename.encode('gb2312'))
msg.attach(part)
logging.info('gen msg for '+ reply_to)
return msg
def _message_id(self, reply):
message_id = None
if str(reply[0]) == '250':
message_id = str(reply[1]).split('#')[1]
return message_id
def send(self, msg):
self.client.mail(self.sender)
self.client.rcpt(msg['to'])
# print msg.as_string()
try:
(code, resp) = self.client.data(msg.as_string())
self.client.rset()
logging.info('send %s' % resp)
if code == 250:
logging.info('send to %s' % msg['to'])
return True
logging.error('code=%d, msg=%s' % (code, resp))
return False
except:
return False