Describe how to send mail from a Python script.

Answer Posted / chaitanya

The smtplib module defines an SMTP client session object that can be used to send mail to any Internet machine.

A sample email is demonstrated below.

import smtplib

SERVER = smtplib.SMTP(‘smtp.server.domain’)

FROM = sender@mail.com

TO = ["user@mail.com"] # must be a list

SUBJECT = "Hello!"

TEXT = "This message was sent with Python's smtplib."

# Main message

message = """

From: Sarah Naaz < sender@mail.com >

To: CarreerRide user@mail.com

Subject: SMTP email msg

This is a test email. Acknowledge the email by responding.

""" % (FROM, ", ".join(TO), SUBJECT, TEXT)

server = smtplib.SMTP(SERVER)

server.sendmail(FROM, TO, message)

server.quit()

Is This Answer Correct ?    0 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is tuple when we have to use ?

467


What is enumerate() function in python?

412


Is python interpreted language?

427


What is __init__.py used for?

494


Does python have a-main() method?

466






Does python have static classes?

449


What does do in python?

474


What is used to create unicode strings in Python?

458


What is python distutils?

442


What is the print in python?

475


Which is better r or python?

438


Is c++ or python better?

464


What are pandas in python?

444


What mode is used for both writing and reading in binary format in file.?

518


What are advantages and disadvantages of python?

424