本文整理汇总了Python中email.utils.formatdate方法的典型用法代码示例。如果您正苦于以下问题:Python utils.formatdate方法的具体用法?Python utils.formatdate怎么用?Python utils.formatdate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类email.utils
的用法示例。
在下文中一共展示了utils.formatdate方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: message
# 需要导入模块: from email import utils [as 别名]
# 或者: from email.utils import formatdate [as 别名]
def message(self):
encoding = self.encoding or settings.DEFAULT_CHARSET
msg = SafeMIMEText(self.body, self.content_subtype, encoding)
msg = self._create_message(msg)
msg['Subject'] = self.subject
msg['From'] = self.extra_headers.get('From', self.from_email)
msg['To'] = self.extra_headers.get('To', ', '.join(self.to))
if self.cc:
msg['Cc'] = ', '.join(self.cc)
if self.reply_to:
msg['Reply-To'] = self.extra_headers.get('Reply-To', ', '.join(self.reply_to))
# Email header names are case-insensitive (RFC 2045), so we have to
# accommodate that when doing comparisons.
header_names = [key.lower() for key in self.extra_headers]
if 'date' not in header_names:
msg['Date'] = formatdate()
if 'message-id' not in header_names:
# Use cached DNS_NAME for performance
msg['Message-ID'] = make_msgid(domain=DNS_NAME)
for name, value in self.extra_headers.items():
if name.lower() in ('from', 'to'): # From and To are already handled
continue
msg[name] = value
return msg
示例2: send_email
# 需要导入模块: from email import utils [as 别名]
# 或者: from email.utils import formatdate [as 别名]
def send_email(msg_to, msg_subject, msg_body, msg_from=None,
smtp_server='localhost', envelope_from=None,
headers={}):
if not msg_from:
msg_from = app.config['EMAIL_FROM']
if not envelope_from:
envelope_from = parseaddr(msg_from)[1]
msg = MIMEText(msg_body)
msg['Subject'] = Header(msg_subject)
msg['From'] = msg_from
msg['To'] = msg_to
msg['Date'] = formatdate()
msg['Message-ID'] = make_msgid()
msg['Errors-To'] = envelope_from
if request:
msg['X-Submission-IP'] = request.remote_addr
s = smtplib.SMTP(smtp_server)
s.sendmail(envelope_from, msg_to, msg.as_string())
s.close()
示例3: add_auth
# 需要导入模块: from email import utils [as 别名]
# 或者: from email.utils import formatdate [as 别名]
def add_auth(self, request):
if self.credentials is None:
raise NoCredentialsError
if 'Date' in request.headers:
del request.headers['Date']
request.headers['Date'] = formatdate(usegmt=True)
if self.credentials.token:
if 'X-Amz-Security-Token' in request.headers:
del request.headers['X-Amz-Security-Token']
request.headers['X-Amz-Security-Token'] = self.credentials.token
new_hmac = hmac.new(self.credentials.secret_key.encode('utf-8'),
digestmod=sha256)
new_hmac.update(request.headers['Date'].encode('utf-8'))
encoded_signature = encodebytes(new_hmac.digest()).strip()
signature = ('AWS3-HTTPS AWSAccessKeyId=%s,Algorithm=%s,Signature=%s' %
(self.credentials.access_key, 'HmacSHA256',
encoded_signature.decode('utf-8')))
if 'X-Amzn-Authorization' in request.headers:
del request.headers['X-Amzn-Authorization']
request.headers['X-Amzn-Authorization'] = signature
示例4: _set_necessary_date_headers
# 需要导入模块: from email import utils [as 别名]
# 或者: from email.utils import formatdate [as 别名]
def _set_necessary_date_headers(self, request):
# The spec allows for either the Date _or_ the X-Amz-Date value to be
# used so we check both. If there's a Date header, we use the date
# header. Otherwise we use the X-Amz-Date header.
if 'Date' in request.headers:
del request.headers['Date']
datetime_timestamp = datetime.datetime.strptime(
request.context['timestamp'], SIGV4_TIMESTAMP)
request.headers['Date'] = formatdate(
int(calendar.timegm(datetime_timestamp.timetuple())))
if 'X-Amz-Date' in request.headers:
del request.headers['X-Amz-Date']
else:
if 'X-Amz-Date' in request.headers:
del request.headers['X-Amz-Date']
request.headers['X-Amz-Date'] = request.context['timestamp']
示例5: send_mail
# 需要导入模块: from email import utils [as 别名]
# 或者: from email.utils import formatdate [as 别名]
def send_mail(send_from, send_to, subject, text, files=[], server="localhost", ssl=False, username=None, password=None):
msg = MIMEMultipart('alternative')
msg.set_charset('utf-8')
msg['From'] = send_from
msg['To'] = send_to
msg['Date'] = formatdate(localtime=True)
msg['Subject'] = subject
part = MIMEText(text)
part.set_charset('utf-8')
msg.attach(part)
if ssl:
smtp = smtplib.SMTP_SSL(server)
else:
smtp = smtplib.SMTP(server)
if username:
smtp.login(username, password)
smtp.sendmail(send_from, send_to, msg.as_string())
smtp.close()
示例6: send_mail_by_postfix
# 需要导入模块: from email import utils [as 别名]
# 或者: from email.utils import formatdate [as 别名]
def send_mail_by_postfix(mail_to, content_type, content,
subject=DEFAULT_EMAIL_NOTIFY_SUBJECT,
mail_from=EMAIL_NOTIFY_NAME,
server=EMAIL_SMTP_SERVER):
"""
使用 Postfix 作为 SMTP Server 来发送邮件
"""
logger.debug('run send_mail_by_postfix')
if type(mail_to) != list:
raise TypeError('mail_to is not a list')
if content_type not in ('plain', 'html'):
raise ValueError('content_type is not plain or html')
msg = MIMEMultipart()
msg['From'] = mail_from
msg['To'] = COMMASPACE.join(mail_to)
msg['Date'] = formatdate(localtime=True)
msg['Subject'] = subject
msg.attach(MIMEText(content, _subtype=content_type, _charset='utf-8'))
smtp = smtplib.SMTP(server)
smtp.sendmail(mail_from, mail_to, msg.as_string())
smtp.close()
logger.info('邮件发送成功')
示例7: send_mail_main
# 需要导入模块: from email import utils [as 别名]
# 或者: from email.utils import formatdate [as 别名]
def send_mail_main(subject, body, config=None):
if config is None:
config = current_app.config
mail_to = config['ADMIN_EMAIL']
mail_from = config['MAIL_FROM']
msg = MIMEText(body, 'plain', 'UTF-8')
msg['Subject'] = subject
msg['To'] = mail_to
msg['From'] = mail_from
msg['Date'] = formatdate()
msg['Message-ID'] = make_msgid()
s = smtplib.SMTP(config['SMTP_HOST'])
s.sendmail(mail_from, [mail_to], msg.as_string())
s.quit()
示例8: WOSendMail
# 需要导入模块: from email import utils [as 别名]
# 或者: from email.utils import formatdate [as 别名]
def WOSendMail(send_from, send_to, subject, text, files, server="localhost",
port=587, username='', password='', isTls=True):
msg = MIMEMultipart()
msg['From'] = send_from
msg['To'] = send_to
msg['Date'] = formatdate(localtime=True)
msg['Subject'] = subject
msg.attach(MIMEText(text))
for f in files:
part = MIMEBase('application', "octet-stream")
part.set_payload(open(f, "rb").read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment; filename="{0}"'
.format(os.path.basename(f)))
msg.attach(part)
smtp = smtplib.SMTP(server, port)
if isTls:
smtp.starttls()
smtp.sendmail(send_from, send_to, msg.as_string())
smtp.quit()
示例9: test_authentication_timeout
# 需要导入模块: from email import utils [as 别名]
# 或者: from email.utils import formatdate [as 别名]
def test_authentication_timeout(self):
def callback():
blockchain = BtcTxStore()
wif = blockchain.create_key()
address = blockchain.get_address(wif)
farmer = Farmer(address)
timeout = farmer.get_server_authentication_timeout()
date = datetime.now() - timedelta(seconds=timeout)
header_date = formatdate(timeval=mktime(date.timetuple()),
localtime=True, usegmt=True)
message = farmer.get_server_address() + " " + header_date
header_authorization = blockchain.sign_unicode(wif, message)
headers = {"Date": header_date,
"Authorization": header_authorization}
farmer.authenticate(headers)
self.assertRaises(storjcore.auth.AuthError, callback)
示例10: test_success
# 需要导入模块: from email import utils [as 别名]
# 或者: from email.utils import formatdate [as 别名]
def test_success(self):
# create header date and authorization signature
blockchain = BtcTxStore()
wif = blockchain.create_key()
address = blockchain.get_address(wif)
header_date = formatdate(timeval=mktime(datetime.now().timetuple()),
localtime=True, usegmt=True)
message = app.config["ADDRESS"] + " " + header_date
header_authorization = blockchain.sign_unicode(wif, message)
headers = {"Date": header_date, "Authorization": header_authorization}
url = '/api/register/{0}'.format(address)
rv = self.app.get(url, headers=headers)
data = json.loads(rv.data.decode("utf-8"))
self.assertEqual(address, data["btc_addr"])
self.assertEqual(rv.status_code, 200)
示例11: http_date
# 需要导入模块: from email import utils [as 别名]
# 或者: from email.utils import formatdate [as 别名]
def http_date(timeval=None):
"""返回符合HTTP标准的GMT时间字符串,用strftime的格式表示就是"%a, %d %b %Y %H:%M:%S GMT"。
但不能使用strftime,因为strftime的结果是和locale相关的。
"""
return formatdate(timeval, usegmt=True)
示例12: default
# 需要导入模块: from email import utils [as 别名]
# 或者: from email.utils import formatdate [as 别名]
def default(self, object_: Any) -> Any:
if isinstance(object_, date):
return formatdate(timeval=mktime((object_.timetuple())), localtime=False, usegmt=True)
if isinstance(object_, UUID):
return str(object_)
if hasattr(object_, "__html__"):
return str(object_.__html__())
return super().default(object_)
示例13: send_mail
# 需要导入模块: from email import utils [as 别名]
# 或者: from email.utils import formatdate [as 别名]
def send_mail(self, msg):
if _mail.recip:
# write message to temp file for rate limit
with open(self.temp_msg, 'w+') as f:
f.write(msg)
self.current_time()
message = MIMEMultipart()
message['From'] = _mail.s_addr
message['To'] = _mail.recip
message['Subject'] = _mail.subject
message['Date'] = formatdate(localtime=True)
message.attach(MIMEText('{} {}'.format(self.time, msg), 'plain'))
text = message.as_string()
try:
server = smtplib.SMTP(_mail.server, _mail.port)
except socket.error as err:
playout_logger.error(err)
server = None
if server is not None:
server.starttls()
try:
login = server.login(_mail.s_addr, _mail.s_pass)
except smtplib.SMTPAuthenticationError as serr:
playout_logger.error(serr)
login = None
if login is not None:
server.sendmail(_mail.s_addr, _mail.recip, text)
server.quit()
示例14: _strtime_to_httpdate
# 需要导入模块: from email import utils [as 别名]
# 或者: from email.utils import formatdate [as 别名]
def _strtime_to_httpdate(dt):
return email_utils.formatdate(calendar.timegm(
iso8601.parse_date(dt).timetuple()), usegmt=True)
示例15: datetime_to_header
# 需要导入模块: from email import utils [as 别名]
# 或者: from email.utils import formatdate [as 别名]
def datetime_to_header(dt):
return formatdate(calendar.timegm(dt.timetuple()))