当前位置: 首页>>代码示例>>Python>>正文


Python SMTP.data方法代码示例

本文整理汇总了Python中smtplib.SMTP.data方法的典型用法代码示例。如果您正苦于以下问题:Python SMTP.data方法的具体用法?Python SMTP.data怎么用?Python SMTP.data使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在smtplib.SMTP的用法示例。


在下文中一共展示了SMTP.data方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: send

# 需要导入模块: from smtplib import SMTP [as 别名]
# 或者: from smtplib.SMTP import data [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()
开发者ID:BoneLee,项目名称:FBT,代码行数:21,代码来源:test_sendCloud.py

示例2: sendn

# 需要导入模块: from smtplib import SMTP [as 别名]
# 或者: from smtplib.SMTP import data [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
开发者ID:ccfr32,项目名称:fileserver,代码行数:50,代码来源:python_smtp.py

示例3: send

# 需要导入模块: from smtplib import SMTP [as 别名]
# 或者: from smtplib.SMTP import data [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
开发者ID:ccfr32,项目名称:fileserver,代码行数:50,代码来源:python_smtp.py

示例4: MailTool

# 需要导入模块: from smtplib import SMTP [as 别名]
# 或者: from smtplib.SMTP import data [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
开发者ID:twotwo,项目名称:tools-python,代码行数:101,代码来源:send_mail.py

示例5: data

# 需要导入模块: from smtplib import SMTP [as 别名]
# 或者: from smtplib.SMTP import data [as 别名]
 def data(self, msg):
     (code, msg) = SMTP.data(self, msg)
     self._last_smtp_response = (code, msg)
     return (code, msg)
开发者ID:lisbitid,项目名称:less_flask,代码行数:6,代码来源:client.py


注:本文中的smtplib.SMTP.data方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。