Python email.Utils.formatdate() Examples

The following are 30 code examples of email.Utils.formatdate(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module email.Utils , or try the search function .
Example #1
Source File: message.py    From python-compat-runtime with Apache License 2.0 6 votes vote down vote up
def message(self):
        encoding = self.encoding or settings.DEFAULT_CHARSET
        msg = SafeMIMEText(smart_str(self.body, encoding),
                           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'] = ', '.join(self.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:
            msg['Message-ID'] = make_msgid()
        for name, value in self.extra_headers.items():
            if name.lower() == 'from':  # From is already handled
                continue
            msg[name] = value
        return msg 
Example #2
Source File: smtp.py    From peach with Mozilla Public License 2.0 6 votes vote down vote up
def send(self, data):
        """
        Publish some data

        @type	data: string
        @param	data: Data to publish
        """
        # Build Message Body
        msg = MIMEMultipart()
        msg['From'] = self.msgFrom
        msg['To'] = self.msgTo
        msg['Date'] = formatdate(localtime=True)
        msg['Subject'] = self.msgSubject
        msg.attach(MIMEText(self.msgText))
        # Attach file
        part = MIMEBase('application', 'pdf')
        part.set_payload(data)
        Encoders.encode_base64(part)
        part.add_header('Content-Disposition', 'attachment; filename="%s"' % self.fileName)
        msg.attach(part)
        # Send email
        smtp = smtplib.SMTP(self.server)
        smtp.sendmail(self.msgFrom, self.msgTo, msg.as_string())
        smtp.close() 
Example #3
Source File: test_email.py    From CTFCrackTools-V2 with GNU General Public License v3.0 5 votes vote down vote up
def test_formatdate_localtime(self):
        now = time.time()
        self.assertEqual(
            Utils.parsedate(Utils.formatdate(now, localtime=True))[:6],
            time.localtime(now)[:6]) 
Example #4
Source File: git_multimail_upstream.py    From pagure with GNU General Public License v2.0 5 votes vote down vote up
def __next__(self):
        formatted = formatdate(self.time, True)
        self.time += 1
        return formatted 
Example #5
Source File: test_email.py    From medicare-demo with Apache License 2.0 5 votes vote down vote up
def test_formatdate(self):
        now = time.time()
        self.assertEqual(Utils.parsedate(Utils.formatdate(now))[:6],
                         time.gmtime(now)[:6]) 
Example #6
Source File: test_email.py    From medicare-demo with Apache License 2.0 5 votes vote down vote up
def test_formatdate_localtime(self):
        now = time.time()
        self.assertEqual(
            Utils.parsedate(Utils.formatdate(now, localtime=True))[:6],
            time.localtime(now)[:6]) 
Example #7
Source File: test_email.py    From medicare-demo with Apache License 2.0 5 votes vote down vote up
def test_formatdate_usegmt(self):
        now = time.time()
        self.assertEqual(
            Utils.formatdate(now, localtime=False),
            time.strftime('%a, %d %b %Y %H:%M:%S -0000', time.gmtime(now)))
        self.assertEqual(
            Utils.formatdate(now, localtime=False, usegmt=True),
            time.strftime('%a, %d %b %Y %H:%M:%S GMT', time.gmtime(now))) 
Example #8
Source File: _urllib2_fork.py    From BruteXSS with GNU General Public License v3.0 5 votes vote down vote up
def open_local_file(self, req):
        try:
            import email.utils as emailutils
        except ImportError:
            # python 2.4
            import email.Utils as emailutils
        import mimetypes
        host = req.get_host()
        file = req.get_selector()
        localfile = url2pathname(file)
        try:
            stats = os.stat(localfile)
            size = stats.st_size
            modified = emailutils.formatdate(stats.st_mtime, usegmt=True)
            mtype = mimetypes.guess_type(file)[0]
            headers = mimetools.Message(StringIO(
                'Content-type: %s\nContent-length: %d\nLast-modified: %s\n' %
                (mtype or 'text/plain', size, modified)))
            if host:
                host, port = splitport(host)
            if not host or \
                (not port and socket.gethostbyname(host) in self.get_names()):
                return addinfourl(open(localfile, 'rb'),
                                  headers, 'file:'+file)
        except OSError, msg:
            # urllib2 users shouldn't expect OSErrors coming from urlopen()
            raise URLError(msg) 
Example #9
Source File: _urllib2_fork.py    From pelisalacarta-ce with GNU General Public License v3.0 5 votes vote down vote up
def open_local_file(self, req):
        try:
            import email.utils as emailutils
        except ImportError:
            # python 2.4
            import email.Utils as emailutils
        import mimetypes
        host = req.get_host()
        file = req.get_selector()
        localfile = url2pathname(file)
        try:
            stats = os.stat(localfile)
            size = stats.st_size
            modified = emailutils.formatdate(stats.st_mtime, usegmt=True)
            mtype = mimetypes.guess_type(file)[0]
            headers = mimetools.Message(StringIO(
                'Content-type: %s\nContent-length: %d\nLast-modified: %s\n' %
                (mtype or 'text/plain', size, modified)))
            if host:
                host, port = splitport(host)
            if not host or \
                (not port and socket.gethostbyname(host) in self.get_names()):
                return addinfourl(open(localfile, 'rb'),
                                  headers, 'file:'+file)
        except OSError, msg:
            # urllib2 users shouldn't expect OSErrors coming from urlopen()
            raise URLError(msg) 
Example #10
Source File: test_email.py    From CTFCrackTools-V2 with GNU General Public License v3.0 5 votes vote down vote up
def test_formatdate(self):
        now = time.time()
        self.assertEqual(Utils.parsedate(Utils.formatdate(now))[:6],
                         time.gmtime(now)[:6]) 
Example #11
Source File: http.py    From python-compat-runtime with Apache License 2.0 5 votes vote down vote up
def http_date(epoch_seconds=None):
    """
    Formats the time to match the RFC1123 date format as specified by HTTP
    RFC2616 section 3.3.1.

    Accepts a floating point number expressed in seconds since the epoch, in
    UTC - such as that outputted by time.time(). If set to None, defaults to
    the current time.

    Outputs a string in the format 'Wdy, DD Mon YYYY HH:MM:SS GMT'.
    """
    rfcdate = formatdate(epoch_seconds)
    return '%s GMT' % rfcdate[:25]

# Base 36 functions: useful for generating compact URLs 
Example #12
Source File: test_email.py    From CTFCrackTools-V2 with GNU General Public License v3.0 5 votes vote down vote up
def test_formatdate_usegmt(self):
        now = time.time()
        self.assertEqual(
            Utils.formatdate(now, localtime=False),
            time.strftime('%a, %d %b %Y %H:%M:%S -0000', time.gmtime(now)))
        self.assertEqual(
            Utils.formatdate(now, localtime=False, usegmt=True),
            time.strftime('%a, %d %b %Y %H:%M:%S GMT', time.gmtime(now))) 
Example #13
Source File: test_email.py    From CTFCrackTools-V2 with GNU General Public License v3.0 5 votes vote down vote up
def test_formatdate(self):
        now = time.time()
        self.assertEqual(Utils.parsedate(Utils.formatdate(now))[:6],
                         time.gmtime(now)[:6]) 
Example #14
Source File: test_email.py    From CTFCrackTools-V2 with GNU General Public License v3.0 5 votes vote down vote up
def test_formatdate_localtime(self):
        now = time.time()
        self.assertEqual(
            Utils.parsedate(Utils.formatdate(now, localtime=True))[:6],
            time.localtime(now)[:6]) 
Example #15
Source File: utils.py    From canvas with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def emit(self, record):
        """
        Emit a record.

        Format the record and send it to the specified addressees.
        It would be really nice if I could add authorization to this class
        without having to resort to cut and paste inheritance but, no.
        """
        try:
            port = self.mailport
            if not port:
                port = smtplib.SMTP_PORT
            smtp = smtplib.SMTP(self.mailhost, port)
            smtp.login(self.username, self.password)
            msg = self.format(record)
            msg = "From: %s\r\nTo: %s\r\nSubject: %s\r\nDate: %s\r\n\r\n%s" % (
                            self.fromaddr,
                            ','.join(self.toaddrs),
                            self.getSubject(record),
                            formatdate(), msg)
            smtp.sendmail(self.fromaddr, self.toaddrs, msg)
            smtp.quit()
        except (KeyboardInterrupt, SystemExit):
            raise
        except:
            self.handleError(record) 
Example #16
Source File: test_email.py    From CTFCrackTools with GNU General Public License v3.0 5 votes vote down vote up
def test_formatdate(self):
        now = time.time()
        self.assertEqual(Utils.parsedate(Utils.formatdate(now))[:6],
                         time.gmtime(now)[:6]) 
Example #17
Source File: test_email.py    From CTFCrackTools with GNU General Public License v3.0 5 votes vote down vote up
def test_formatdate_localtime(self):
        now = time.time()
        self.assertEqual(
            Utils.parsedate(Utils.formatdate(now, localtime=True))[:6],
            time.localtime(now)[:6]) 
Example #18
Source File: test_email.py    From CTFCrackTools with GNU General Public License v3.0 5 votes vote down vote up
def test_formatdate_usegmt(self):
        now = time.time()
        self.assertEqual(
            Utils.formatdate(now, localtime=False),
            time.strftime('%a, %d %b %Y %H:%M:%S -0000', time.gmtime(now)))
        self.assertEqual(
            Utils.formatdate(now, localtime=False, usegmt=True),
            time.strftime('%a, %d %b %Y %H:%M:%S GMT', time.gmtime(now))) 
Example #19
Source File: test_email.py    From CTFCrackTools with GNU General Public License v3.0 5 votes vote down vote up
def test_formatdate(self):
        now = time.time()
        self.assertEqual(Utils.parsedate(Utils.formatdate(now))[:6],
                         time.gmtime(now)[:6]) 
Example #20
Source File: test_email.py    From CTFCrackTools with GNU General Public License v3.0 5 votes vote down vote up
def test_formatdate_localtime(self):
        now = time.time()
        self.assertEqual(
            Utils.parsedate(Utils.formatdate(now, localtime=True))[:6],
            time.localtime(now)[:6]) 
Example #21
Source File: test_email.py    From datafari with Apache License 2.0 5 votes vote down vote up
def test_formatdate_usegmt(self):
        now = time.time()
        self.assertEqual(
            Utils.formatdate(now, localtime=False),
            time.strftime('%a, %d %b %Y %H:%M:%S -0000', time.gmtime(now)))
        self.assertEqual(
            Utils.formatdate(now, localtime=False, usegmt=True),
            time.strftime('%a, %d %b %Y %H:%M:%S GMT', time.gmtime(now))) 
Example #22
Source File: test_mail.py    From bitmask-dev with GNU General Public License v3.0 5 votes vote down vote up
def _get_msg_time():
    timestamp = time.mktime((2010, 12, 12, 1, 1, 1, 1, 1, 1))
    return formatdate(timestamp) 
Example #23
Source File: test_email.py    From ironpython2 with Apache License 2.0 5 votes vote down vote up
def test_formatdate(self):
        now = time.time()
        self.assertEqual(Utils.parsedate(Utils.formatdate(now))[:6],
                         time.gmtime(now)[:6]) 
Example #24
Source File: test_email.py    From ironpython2 with Apache License 2.0 5 votes vote down vote up
def test_formatdate_localtime(self):
        now = time.time()
        self.assertEqual(
            Utils.parsedate(Utils.formatdate(now, localtime=True))[:6],
            time.localtime(now)[:6]) 
Example #25
Source File: test_email.py    From ironpython2 with Apache License 2.0 5 votes vote down vote up
def test_formatdate_usegmt(self):
        now = time.time()
        self.assertEqual(
            Utils.formatdate(now, localtime=False),
            time.strftime('%a, %d %b %Y %H:%M:%S -0000', time.gmtime(now)))
        self.assertEqual(
            Utils.formatdate(now, localtime=False, usegmt=True),
            time.strftime('%a, %d %b %Y %H:%M:%S GMT', time.gmtime(now))) 
Example #26
Source File: mailer.py    From anytask with MIT License 5 votes vote down vote up
def mail_headers(self, group, params):
    from email import Utils
    subject = self.make_subject(group, params)
    try:
      subject.encode('ascii')
    except UnicodeError:
      from email.Header import Header
      subject = Header(subject, 'utf-8').encode()
    hdrs = 'From: %s\n'    \
           'To: %s\n'      \
           'Subject: %s\n' \
           'Date: %s\n' \
           'Message-ID: %s\n' \
           'MIME-Version: 1.0\n' \
           'Content-Type: text/plain; charset=UTF-8\n' \
           'Content-Transfer-Encoding: 8bit\n' \
           'X-Svn-Commit-Project: %s\n' \
           'X-Svn-Commit-Author: %s\n' \
           'X-Svn-Commit-Revision: %d\n' \
           'X-Svn-Commit-Repository: %s\n' \
           % (self.from_addr, ', '.join(self.to_addrs), subject, 
              Utils.formatdate(), Utils.make_msgid(), group,
              self.repos.author or 'no_author', self.repos.rev,
              os.path.basename(self.repos.repos_dir))
    if self.reply_to:
      hdrs = '%sReply-To: %s\n' % (hdrs, self.reply_to)
    return hdrs + '\n' 
Example #27
Source File: mailer.py    From daf-recipes with GNU General Public License v3.0 5 votes vote down vote up
def _mail_recipient(recipient_name, recipient_email,
                    sender_name, sender_url, subject,
                    body, headers={}):
    mail_from = config.get('smtp.mail_from')
    msg = MIMEText(body.encode('utf-8'), 'plain', 'utf-8')
    for k, v in headers.items():
        msg[k] = v
    subject = Header(subject.encode('utf-8'), 'utf-8')
    msg['Subject'] = subject
    msg['From'] = _("%s <%s>") % (sender_name, mail_from)
    recipient = u"%s <%s>" % (recipient_name, recipient_email)
    msg['To'] = Header(recipient, 'utf-8')
    msg['Date'] = Utils.formatdate(time())
    msg['X-Mailer'] = "CKAN %s" % ckan.__version__

    # Send the email using Python's smtplib.
    smtp_connection = smtplib.SMTP()
    if 'smtp.test_server' in config:
        # If 'smtp.test_server' is configured we assume we're running tests,
        # and don't use the smtp.server, starttls, user, password etc. options.
        smtp_server = config['smtp.test_server']
        smtp_starttls = False
        smtp_user = None
        smtp_password = None
    else:
        smtp_server = config.get('smtp.server', 'localhost')
        smtp_starttls = paste.deploy.converters.asbool(
            config.get('smtp.starttls'))
        smtp_user = config.get('smtp.user')
        smtp_password = config.get('smtp.password')

    try:
        smtp_connection.connect(smtp_server)
    except socket.error, e:
        log.exception(e)
        raise MailerException('SMTP server could not be connected to: "%s" %s'
                              % (smtp_server, e)) 
Example #28
Source File: test_email.py    From BinderFilter with MIT License 5 votes vote down vote up
def test_formatdate(self):
        now = time.time()
        self.assertEqual(Utils.parsedate(Utils.formatdate(now))[:6],
                         time.gmtime(now)[:6]) 
Example #29
Source File: test_email.py    From BinderFilter with MIT License 5 votes vote down vote up
def test_formatdate_localtime(self):
        now = time.time()
        self.assertEqual(
            Utils.parsedate(Utils.formatdate(now, localtime=True))[:6],
            time.localtime(now)[:6]) 
Example #30
Source File: test_email.py    From BinderFilter with MIT License 5 votes vote down vote up
def test_formatdate_usegmt(self):
        now = time.time()
        self.assertEqual(
            Utils.formatdate(now, localtime=False),
            time.strftime('%a, %d %b %Y %H:%M:%S -0000', time.gmtime(now)))
        self.assertEqual(
            Utils.formatdate(now, localtime=False, usegmt=True),
            time.strftime('%a, %d %b %Y %H:%M:%S GMT', time.gmtime(now)))