Email with python


Asked by wiki @ & viewed by 16 persons


How to send an email with Python?

This code works and sends me an email just fine:

import smtplib
#SERVER = "localhost"

FROM = 'monty@python.com'

TO = ["jon@mycompany.com"] # must be a list

SUBJECT = "Hello!"

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

# Prepare actual message

message = """\
From: %s
To: %s
Subject: %s

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

# Send the mail

server = smtplib.SMTP('myserver')
server.sendmail(FROM, TO, message)
server.quit()

However if I try to wrap it in a function like this:

def sendMail(FROM,TO,SUBJECT,TEXT,SERVER):
    import smtplib
    """this is some test documentation in the function"""
    message = """\
        From: %s
        To: %s
        Subject: %s
        %s
        """ % (FROM, ", ".join(TO), SUBJECT, TEXT)
    # Send the mail
    server = smtplib.SMTP(SERVER)
    server.sendmail(FROM, TO, message)
    server.quit()

and call it I get the following errors:

 Traceback (most recent call last):
  File "C:/Python31/mailtest1.py", line 8, in <module>
    sendmail.sendMail(sender,recipients,subject,body,server)
  File "C:/Python31\sendmail.py", line 13, in sendMail
    server.sendmail(FROM, TO, message)
  File "C:\Python31\lib\smtplib.py", line 720, in sendmail
    self.rset()
  File "C:\Python31\lib\smtplib.py", line 444, in rset
    return self.docmd("rset")
  File "C:\Python31\lib\smtplib.py", line 368, in docmd
    return self.getreply()
  File "C:\Python31\lib\smtplib.py", line 345, in getreply
    raise SMTPServerDisconnected("Connection unexpectedly closed")
smtplib.SMTPServerDisconnected: Connection unexpectedly closed

Can anyone help me understand why?

Do you know the better answer?

Similar questions

Validate email in python

Asked by wiki @ & viewed by 8 persons

How to check for valid email address? [duplicate] This question already has answers here: How to validate an email address …

Python script to automatically send email

Asked by wiki @ & viewed by 11 persons

How to make a python script automatically send an email when certain data is changed? So basically, I made a …

Python send email utf 8

Asked by wiki @ & viewed by 10 persons

Python - How to send utf-8 e-mail? how to send utf8 e-mail please? import sys import smtplib import email import …

Read email python

Asked by wiki @ & viewed by 10 persons

How to read email content in Python 3 I've tried many code to access and read email content, for example, …

Python email spoofer

Asked by wiki @ & viewed by 6 persons

Trying to spoof an email address I'm sure this has been asked, but I can't find anything to get mine …

Send automatic email python

Asked by wiki @ & viewed by 11 persons

How to send an email with Python? This code works and sends me an email just fine: import smtplib #SERVER …

Send email without smtp server python

Asked by wiki @ & viewed by 9 persons

how to send email with python directly from server and without smtp I'm php programmer and with php you can …

Python smtp receive email

Asked by wiki @ & viewed by 9 persons

Receive and send emails in python How can I receive and send email in python? A 'mail server' of sorts. …

Sendgrid python send email

Asked by wiki @ & viewed by 8 persons

Python Sendgrid send email with PDF attachment file I'm trying to attach a PDF file to my email sent with …

Reading email with python

Asked by wiki @ & viewed by 8 persons

How to read email content in Python 3 I've tried many code to access and read email content, for example, …

Check valid email python

Asked by wiki @ & viewed by 8 persons

How to check for valid email address? [duplicate] This question already has answers here: How to validate an email address …

Email address validation in python

Asked by wiki @ & viewed by 13 persons

How to check for valid email address? [duplicate] This question already has answers here: How to validate an email address …

Send email using python script

Asked by wiki @ & viewed by 7 persons

How to send an email with Python? This code works and sends me an email just fine: import smtplib #SERVER …

How to use python to send an email

Asked by wiki @ & viewed by 8 persons

How to send an email with Python? This code works and sends me an email just fine: import smtplib #SERVER …

Email trigger python script

Asked by wiki @ & viewed by 9 persons

Trigger script upon email receipt I need to trigger a python script every time an email is received by my …

Most viewed questions


Python strftime milliseconds

Asked by wiki @ & viewed by 200 persons

Format a datetime into a string with milliseconds I want to have a datetime string from the date with milliseconds. …

What does the percent sign mean in python

Asked by wiki @ & viewed by 136 persons

What does the percentage sign mean in Python In the tutorial there is an example for finding prime numbers: >>> …

Self keyword in python

Asked by wiki @ & viewed by 134 persons

What is the purpose of the word 'self'? What is the purpose of the self word in Python? I understand …

Check object type python

Asked by wiki @ & viewed by 132 persons

Determine the type of an object? Is there a simple way to determine if a variable is a list, dictionary, …

Correlation matrix python

Asked by wiki @ & viewed by 131 persons

Plot correlation matrix using pandas I have a data set with huge number of features, so analysing the correlation matrix …

Alphabetical sort python

Asked by wiki @ & viewed by 131 persons

Python data structure sort list alphabetically I am a bit confused regarding data structure in python; (),[], and {}. I …

Python random

Asked by wiki @ & viewed by 127 persons

Generate random integers between 0 and 9 How can I generate random integers between 0 and 9 (inclusive) in Python? …

Python one line for loop

Asked by wiki @ & viewed by 124 persons

Python one-line “for” expression I'm not sure if I need a lambda, or something else. But still, I need the …

Python create dataframe from list

Asked by wiki @ & viewed by 124 persons

Python: create a pandas data frame from a list I am using the following code to create a data frame …

How to add a column to dataframe python

Asked by wiki @ & viewed by 123 persons

Adding new column to existing DataFrame in Python pandas I have the following indexed DataFrame with named columns and rows …

Why we use self in python

Asked by wiki @ & viewed by 116 persons

What is the purpose of the word 'self'? What is the purpose of the self word in Python? I understand …

Shape 0 python

Asked by wiki @ & viewed by 115 persons

x.shape[0] vs x[0].shape in NumPy Let say, I have an array with x.shape = (10,1024) when I try to print …

Python print working directory

Asked by wiki @ & viewed by 112 persons

Find current directory and file's directory [duplicate] This question already has answers here: How do you properly determine the current …

Image shape python

Asked by wiki @ & viewed by 110 persons

Python OpenCV2 (cv2) wrapper to get image size? How to get the size of an image in cv2 wrapper in …

Interface grafica python

Asked by wiki @ & viewed by 108 persons

Como criar interface gráfica em Python? [duplicada] Essa pergunta já tem uma resposta aqui: É possível criar interfaces gráficas para …