[Python] Unable to execute a remote python code to email-with-attachment using Paramiko from...

Discussão em 'Python' iniciado por Stack, Junho 24, 2025.

  1. Stack

    Stack Membro Participativo

    I am on server1 where I have written a python script to execute another python script whose name is sendmail.py and which is present on server 2.

    The code for the same looks like:

    import paramiko
    import smtplib
    import base64

    ftpServer = "servname
    ftpUser = "username"
    ftpPwd = "*****"
    port = 22

    try:
    client = paramiko.SSHClient()
    client.load_system_host_keys()
    client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    client.connect(ftpServer, port, ftpUser, ftpPwd)
    print "Connection established with " + ftpServer

    stdin, stdout, stderr = client.exec_command('python Codes/sendmail.py')
    client.close()
    except Exception, ex:
    print "Error in sending e-mail: ", ex


    On the other hand, the python code present on server2, sendmail.py (which I am trying to execute in the above script) is as follows:

    # Sending mail with an attachment file 'abc.txt'
    # sendmail.py

    import smtplib
    import base64

    filename = "abc.txt"

    # Read a file and encode it into base64 format
    fo = open(filename, "rb")
    fo = open(filename, "rb")
    filecontent = fo.read()
    encodedcontent = base64.b64encode(filecontent) # base64

    sender = 'xyz@gmail.com'
    receiver = 'xyz@gmail.com'

    marker = "AUNIQUEMARKER"

    body ="""
    This is a test email sent with an attachement.
    """
    # Define the main headers.
    part1 = """From: xyz@gmail.com
    Subject: Trial mail
    MIME-Version: 1.0
    Content-Type: multipart/mixed; boundary=%s
    --%s
    """ % (marker, marker)

    # Define the message action
    part2 = """Content-Type: text/plain
    Content-Transfer-Encoding:8bit

    %s
    --%s
    """ % (body,marker)

    # Define the attachment section
    part3 = """Content-Type: multipart/mixed; name=\"%s\"
    Content-Transfer-Encoding:base64
    Content-Disposition: attachment; filename=%s

    %s
    --%s--
    """ %(filename, filename, encodedcontent, marker)
    message = part1 + part2 + part3

    try:
    smtpObj = smtplib.SMTP('localhost')
    smtpObj.sendmail(sender, receiver, message)
    print "Successfully sent email"
    except Exception, ex:
    print "Error: unable to send email", ex


    Independently, the code sendmail.py (present on server2) runs fine. But the problem is that I'm not able to execute the same from the python code through paramiko on server1. No error is thrown, just the program exits normally without mail getting sent.


    On trying to debug the same by printing at different places in the code sendmail.py, I find that my code running on server1 exits after getting to this line of sendmail.py:
    ​

    fo = open(filename, "rb")


    I don't want to read the file to my current server but instead it should sent the mail from server2, as it happens independently. Please help?

    Thanks!

    Continue reading...

Compartilhe esta Página