Python smtplib 模块,SMTPDataError() 实例源码

我们从Python开源项目中,提取了以下6个代码示例,用于说明如何使用smtplib.SMTPDataError()

项目:TCP-IP    作者:JackZ0    | 项目源码 | 文件源码
def notify(subject, whom, what):
    """Send email notification.

    Try to notify the addressee (``whom``) by e-mail, with Subject:
    defined by ``subject`` and message body by ``what``.

    """
    msg = email.message_from_string(what)
    msg.add_header("From", "Certbot renewal agent <root>")
    msg.add_header("To", whom)
    msg.add_header("Subject", subject)
    msg = msg.as_string()
    try:
        lmtp = smtplib.LMTP()
        lmtp.connect()
        lmtp.sendmail("root", [whom], msg)
    except (smtplib.SMTPHeloError, smtplib.SMTPRecipientsRefused,
            smtplib.SMTPSenderRefused, smtplib.SMTPDataError, socket.error):
        # We should try using /usr/sbin/sendmail in this case
        try:
            proc = subprocess.Popen(["/usr/sbin/sendmail", "-t"],
                                    stdin=subprocess.PIPE)
            proc.communicate(msg)
        except OSError:
            return False
    return True
项目:certbot    作者:nikoloskii    | 项目源码 | 文件源码
def notify(subject, whom, what):
    """Send email notification.

    Try to notify the addressee (``whom``) by e-mail, with Subject:
    defined by ``subject`` and message body by ``what``.

    """
    msg = email.message_from_string(what)
    msg.add_header("From", "Certbot renewal agent <root>")
    msg.add_header("To", whom)
    msg.add_header("Subject", subject)
    msg = msg.as_string()
    try:
        lmtp = smtplib.LMTP()
        lmtp.connect()
        lmtp.sendmail("root", [whom], msg)
    except (smtplib.SMTPHeloError, smtplib.SMTPRecipientsRefused,
            smtplib.SMTPSenderRefused, smtplib.SMTPDataError, socket.error):
        # We should try using /usr/sbin/sendmail in this case
        try:
            proc = subprocess.Popen(["/usr/sbin/sendmail", "-t"],
                                    stdin=subprocess.PIPE)
            proc.communicate(msg)
        except OSError:
            return False
    return True
项目:malicious_file_maker    作者:carnal0wnage    | 项目源码 | 文件源码
def send_gmail(to, subject, text, attach):
  try:
    msg = MIMEMultipart()
    msg['From'] = gmail_user
    msg['To'] = to
    msg['Subject'] = subject

    msg.attach(MIMEText(text))

    part = MIMEBase('application', 'octet-stream')
    part.set_payload(open(attach, 'rb').read())
    Encoders.encode_base64(part)
    part.add_header('Content-Disposition',
           'attachment; filename="%s"' % os.path.basename(attach))
    msg.attach(part)

    mailServer = smtplib.SMTP("smtp.gmail.com", 587)
    mailServer.ehlo()
    mailServer.starttls()
    mailServer.ehlo()
    mailServer.login(gmail_user, gmail_pwd)
    mailServer.sendmail(gmail_user, to, msg.as_string())
    # Should be mailServer.quit(), but that crashes...
    mailServer.close()
  except smtplib.SMTPDataError as e:
      print e
项目:malicious_file_maker    作者:carnal0wnage    | 项目源码 | 文件源码
def send_sendgrid(to, subject, text, attach):
  try:
    msg = MIMEMultipart()
    msg['From'] = sendgrid_user
    msg['To'] = to
    msg['Subject'] = subject

    msg.attach(MIMEText(text))

    part = MIMEBase('application', 'octet-stream')
    part.set_payload(open(attach, 'rb').read())
    Encoders.encode_base64(part)
    part.add_header('Content-Disposition',
                    'attachment; filename="%s"' % os.path.basename(attach))
    msg.attach(part)

    mailServer = smtplib.SMTP("smtp.sendgrid.net", 587)
    mailServer.ehlo()
    mailServer.starttls()
    mailServer.ehlo()
    mailServer.login(sendgrid_user, sendgrid_pwd)
    mailServer.sendmail(sendgrid_user, to, msg.as_string())
                    # Should be mailServer.quit(), but that crashes...
    mailServer.close()
  except smtplib.SMTPDataError as e:
      print e
项目:malicious_file_maker    作者:carnal0wnage    | 项目源码 | 文件源码
def send_yandex(to, subject, text, attach):
  try:
    msg = MIMEMultipart()
    msg['From'] = yandex_user
    msg['To'] = to
    msg['Subject'] = subject

    msg.attach(MIMEText(text))

    part = MIMEBase('application', 'octet-stream')
    part.set_payload(open(attach, 'rb').read())
    Encoders.encode_base64(part)
    part.add_header('Content-Disposition',
                    'attachment; filename="%s"' % os.path.basename(attach))
    msg.attach(part)

    mailServer = smtplib.SMTP('smtp.yandex.com', 587)
    mailServer.ehlo()
    mailServer.starttls()
    mailServer.ehlo()
    mailServer.login(yandex_user, yandex_pwd)
    mailServer.sendmail(yandex_user, to, msg.as_string())
                    # Should be mailServer.quit(), but that crashes...
    mailServer.close()
  except smtplib.SMTPDataError as e:
      print e
项目:ngas    作者:ICRAR    | 项目源码 | 文件源码
def data(self,
             msg):
        """
        SMTP 'DATA' command -- sends message data to server.

        Automatically quotes lines beginning with a period per rfc821.
        Raises SMTPDataError if there is an unexpected reply to the
        DATA command; the return value from this method is the final
        response code received when the all data is sent.

        msg:      Message in a string buffer or a filename referring to
                  a file containin the data to be send (string).

        Returns:  Tuple with the status code + a message from the SMTP
                  host (tuple/integer, string).
        """
        self.putcmd("data")
        (code, repl) = self.getreply()
        if self.debuglevel >0 : print "data:", (code, repl)
        if code != 354:
            raise smtplib.SMTPDataError(code, repl)
        else:
            if (not self.__msgInFile):
                # Data contained in the message in memory.
                q = smtplib.quotedata(msg)
                if q[-2:] != smtplib.CRLF: q = q + smtplib.CRLF
                q = q + "." + smtplib.CRLF
                self.send(q)
            else:
                # Data to send is contained in a file.
                fo = open(msg)
                while (1):
                    buf = fo.read(16384)
                    if (buf == ""): break
                    qBuf = smtplib.quotedata(buf)
                    self.send(buf)
                fo.close()
                self.send(smtplib.CRLF + "." + smtplib.CRLF)

            (code, msg) = self.getreply()
            if (self.debuglevel > 0): print "data:", (code, msg)
            return (code, msg)