Sending email with python
<p>I have the following code </p>

<pre><code>import smtplib

sender = 'sender@sender.com'
receivers = ['receiver@receiver.com']

message = """From: From Person &lt;sender@sender.com&gt;
To: To Person &lt;receiver@receiver.com&gt;
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"
</code></pre>

<p>its a copy/paste from somewhere and it works fine.</p>

<p>However . . . Once included into my overall program, the email that i receive does not have the sender or receiver available ??? </p>

<p>Its just blank.... but its the same code.</p>

<pre><code>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 &lt;sender@sender.com&gt;
To: To Person &lt;receiver@receiver.com&gt;
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()
</code></pre>

<p>I'm puzzled, please excuse any indentation that may be wrong, that was only done for the purpose of this post.</p>

<p>If the indentation of your code is as pasted, please indent your code correctly and check if it works:</p>

<pre><code>def email_sender():
    sender = 'sender@sender.com'
    receivers = ['receiver@receiver.com']

    message = """From: From Person &lt;sender@sender.com&gt;
    To: To Person &lt;receiver@receiver.com&gt;
    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"
</code></pre>

python email