Home > other >  Sending mail using python in linux machine
Sending mail using python in linux machine

Time:05-31

am receiving mail when I run the code in jupyter, but i tried in my Linux box via python am not receiving any mail.

Here is the below code:

#! /usr/bin/python
import smtplib
import ssl

port = 587
smtp_server = "smtp-mail.outlook.com"
sender = "[email protected]"
recipient = "[email protected]"
sender_password = "Password"

message = """
Subject: This is a test message
Sent using Python."""

SSL_context = ssl.create_default_context()

with smtplib.SMTP(smtp_server, port) as server:
    server.starttls(context=SSL_context)
    server.login(sender, sender_password)
    server.sendmail(sender, recipient, message)

CodePudding user response:

You are trying to connect Outlook with SMTP. You should require your email account allows smtp, which is not necessarily enabled by default.

Also, you can try other solutions based on libraries;

from O365 import Message

html_template =     """ 
            <html>
            <head>
                <title></title>
            </head>
            <body>
                    {}
            </body>
            </html>
        """

final_html_data = html_template.format(df.to_html(index=False))

o365_auth = ('sender_username@company_email.com','Password')
m = Message(auth=o365_auth)
m.setRecipients('receiver_username@company_email.com')
m.setSubject('Weekly report')
m.setBodyHTML(final)
m.sendMessage()

from https://stackoverflow.com/a/52534847/5942941

or install redmail:

pip install redmail

and try to be use this sample;

from redmail import outlook

outlook.user_name = "[email protected]"
outlook.password = "<MY PASSWORD>"

outlook.send(
    receivers=["[email protected]"],
    subject="An example",
    text="Hi, this is an example."
)

https://stackoverflow.com/a/71056577/5942941

  • Related