Sending an email using smtp lib with body containing multiple lines and variables

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pd4860
    New Member
    • Aug 2014
    • 2

    Sending an email using smtp lib with body containing multiple lines and variables

    I have cracked sending an email containing text and a value from a variable using some code I found online
    Code:
    SERVER = "server"
    
    FROM = "from address"
    TO = ["to address"]
    SUBJECT = "subject"
    
    TEXT = "Line Count     :- " + str(linecount)
    
    # Prepare actual message
    
    message = """\
    From: %s
    To: %s
    Subject: %s
    
    %s
    """ % (FROM, ", ".join(TO), SUBJECT, TEXT)
    
    server = smtplib.SMTP(SERVER)
    server.sendmail(FROM, TO, message)
    server.quit()
    However I would like to add extra lines for other totals. I don't seem to be able to figure out how to do this.

    Any help would be greatly appreciated.
    Last edited by bvdet; Aug 13 '14, 03:59 PM. Reason: Add code tags
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Here is one possibility:
    Code:
    >>> ("Line Count     :- %s\n"
    ...  "another line count: %s" % (linecount, linecount*2))
    'Line Count     :- 6\nanother line count: 12'
    >>> print ("Line Count     :- %s\n"
    ...     "another line count: %s" % (linecount, linecount*2))
    Line Count     :- 6
    another line count: 12
    >>>

    Comment

    • pd4860
      New Member
      • Aug 2014
      • 2

      #3
      Thanks - that did the required job

      Comment

      Working...