我们从Python开源项目中,提取了以下6个代码示例,用于说明如何使用smtplib.SMTPDataError()。
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
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
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
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
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)