Quantcast
Channel: Active questions tagged email - Stack Overflow
Viewing all articles
Browse latest Browse all 29769

HTML code inside Python function doesn't work

$
0
0

I've written few lines of code for sending emails with the smtplib module. If I run the script as it is:

import smtplib

message = """From: Sender <sender@randomemail.com>
To: Receiver <receive@randomemail.com>
Content-type: text/html
Subject: It works!
<b>It works!</b>\n
<br/>
<br/>
Do not reply to this email or god will strike you down.
"""

server = smtplib.SMTP('mysmtpserver')
sender = 'sender'
receiver = 'receive'

server.set_debuglevel(True)

server.sendmail(sender, receiver, message)

It executes just as intended. And I do have HTML formatting in the email received.

However, if I put the code as a function inside 'bigger' file along other functions:

def emailer():
    import smtplib

    message = """From: Sender <sender@randomemail.com>
    To: Receiver <receive@randomemail.com>
    Content-type: text/html
    Subject: It works!
    <b>It works!</b>\n
    <br/>
    <br/>
    Do not reply to this email or god will strike you down.
    """

    server = smtplib.SMTP('mysmtpserver')
    sender = 'sender'
    receiver = 'receive'

    server.set_debuglevel(True)

    server.sendmail(sender, receiver, message)

Then call the function:

emailer()

I get email with no HTML formatting and totally messed up subject and body, which then get's filtered as spam by my email provider.

I do need some explanation on how to fix that, and also advice if I can improve somehow what I've created.

Thanks


Viewing all articles
Browse latest Browse all 29769

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>