我有以下代码
import smtplib
sender = 'sender@sender.com'
receivers = ['receiver@receiver.com']
message = """From: From Person <sender@sender.com>
To: To Person <receiver@receiver.com>
Subject: We are blending baby !
This is a test e-mail message.
"""
try:
smtpObj = smtplib.SMTP('localhost')
smtpObj.sendmail(sender, receivers, message)
print "Successfully sent email"
except SMTPException:
print "Error: unable to send email"
从某处复制/粘贴它可以正常工作。
然而。 。 。一旦包含在我的整个程序中,我收到的电子邮件没有发件人或收件人可用???
它只是空白......但它的代码相同。
import paramiko
import time
import smtplib
def disable_paging(remote_conn):
'''Disable paging on a Cisco router'''
remote_conn.send("terminal length 0\n")
time.sleep(1)
# Clear the buffer on the screen
output = remote_conn.recv(1000)
return output
def main():
# VARIABLES THAT NEED CHANGED
ip = '1.2.3.4'
username = 'xxx'
password = 'xxx'
# Create instance of SSHClient object
remote_conn_pre = paramiko.SSHClient()
# Automatically add untrusted hosts (make sure okay for security policy in your environment)
remote_conn_pre.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# initiate SSH connection
remote_conn_pre.connect(ip, username=username, password=password, look_for_keys=False, allow_agent=False)
# Use invoke_shell to establish an 'interactive session'
remote_conn = remote_conn_pre.invoke_shell()
# Strip the initial router prompt
output = remote_conn.recv(1000)
# Turn off paging
disable_paging(remote_conn)
# Now let's try to send the router a command
remote_conn.send("\n")
remote_conn.send("show log last 50\n")
# Wait for the command to complete
time.sleep(2)
output = remote_conn.recv(10000)
if 'bad.thing' in output:
email_sender()
def email_sender():
sender = 'sender@sender.com'
receivers = ['receiver@receiver.com']
message = """From: From Person <sender@sender.com>
To: To Person <receiver@receiver.com>
Subject: We are blending baby !
This is a test e-mail message.
"""
try:
smtpObj = smtplib.SMTP('localhost')
smtpObj.sendmail(sender, receivers, message)
print "Successfully sent email"
except SMTPException:
print "Error: unable to send email"
main()
我很困惑,请原谅任何可能错误的缩进,这只是为了这篇文章的目的。
答案 0 :(得分:0)
如果代码的缩进是粘贴的,请正确缩进代码并检查代码是否有效:
def email_sender():
sender = 'sender@sender.com'
receivers = ['receiver@receiver.com']
message = """From: From Person <sender@sender.com>
To: To Person <receiver@receiver.com>
Subject: We are blending baby !
This is a test e-mail message.
"""
try:
smtpObj = smtplib.SMTP('localhost')
smtpObj.sendmail(sender, receivers, message)
print "Successfully sent email"
except SMTPException:
print "Error: unable to send email"