Thursday, 30 May 2019

How to Send Email from Python

Please find the below sample code to send email from Python.


import os
import smtplib

from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

msg = MIMEMultipart('alternative')

msg['Subject'] = "Hello from Mandrill, Python style!"
msg['From']    = "Please give From Email Addresss" # Your from name and email address
msg['To']      = "Please give To Email Address"

#text = "Mandrill speaks plaintext"
#part1 = MIMEText(text, 'plain')

html = "<em>Mandrill speaks <strong>HTML</strong></em>"
part2 = MIMEText(html, 'html')

username = "Please give user id of the email account"
password = "Please give password of the email account"

#msg.attach(part1)
msg.attach(part2)

s = smtplib.SMTP('smtp.mandrillapp.com', 587)

s.login(username, password)
s.sendmail(msg['From'], msg['To'], msg.as_string())

s.quit()



How to install/Import Python Packages


First Download Python exe from https://www.python.org/downloads/



Once Python software installed you will see the below python IDE in the start-up menu


Python Default Installation Location.
C:\Users\ramasubbareddy.m\AppData\Local\Programs\Python\Python37

Above mentioned path will be default path. If you don’t select any customized path while installing the python it will install in the above mentioned default path.

How to install/Import Python Packages:
Open command window and enter into the python installation path.
Change the directory to python installation path

We need to use “Pip” command to install any packages in Python.

Note: We no need to install manually any in-build packages. We need install only the external packages using “Pip” command.

Here I will show you how to install “pymssql” python package which is used to connect MS SQL from python source code.
Command to install Python Package: pip install pymssql


Now you can use pymssql python package to connect SQL Server from you Python IDE.

How to Use IDE to build Python Programs:

Search IDLE in start-up menu.


How to create new python script file

 




How to Send Email from Python

Please find the below sample code to send email from Python. import os import smtplib from email.mime.multipart import MIMEMultipart...