Describe how to send mail from a Python script.



Describe how to send mail from a Python script...

Answer / 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

More Python Interview Questions

In python, how do I read a file line-by-line into a list?

1 Answers  


What is the output of print str * 2 if str = 'hello world!'?

1 Answers  


How to install python and prepare environment?

1 Answers  


Does python interact with database?

1 Answers  


How do you write code in python?

1 Answers  


Explain me what is the statement that can be used in python if the program requires no action but requires it syntactically?

1 Answers  


How do you define xrange in python 3?

1 Answers  


What are methods in python?

1 Answers  


How do you split a string in python?

1 Answers  


What command do we use to debug a python program?

1 Answers  


What are python libraries? Name a few of them.

1 Answers  


What is the difference between = and == in python?

4 Answers  


Categories