Philippe Lambotte added the comment:

The code is :

#!/usr/bin/env python3
#-*- coding: utf-8 -*-

import smtplib, os
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email.mime.text import MIMEText
from email.utils import COMMASPACE, formatdate
from email import encoders

EMAIL_FROM = 'emailf...@mywebsite.net'
EMAIL_TO = ['emai...@mywebsite.net']

SENDER_LOGIN = 'mylogin'
SENDER_PASSWORD = 'mypassword'
SMTP_HOST = 'smtp.myhoster.net'
SMTP_PORT = 587
SMTP_IS_STARTTLS = True
SUBJECT ="This is the subject"
TEXT = 'This is a test'
FILES = list() # ['dummy_text.txt']


def send_mail_with_attachment( send_from, send_to, subject, text, files=[], 
server="localhost", port=587, username='', password='', isTls=True):
        msg = MIMEMultipart()
        msg['From'] = send_from
        msg['To'] = COMMASPACE.join(send_to)
        msg['Date'] = formatdate(localtime = True)
        msg['Subject'] = subject

        msg.attach( MIMEText(text) )
        if len(files) > 0 :
                for f in files:
                        part = MIMEBase('application', "octet-stream")
                        part.set_payload( open(f,"rb").read() )
                        encoders.encode_base64(part)
                        part.add_header('Content-Disposition', 'attachment; 
filename="{0}"'.format(os.path.basename(f)))
                        msg.attach(part)

        smtp = smtplib.SMTP(server, port)
        if isTls: smtp.starttls()
        smtp.login(username,password)
        smtp.sendmail(send_from, send_to, msg.as_string())
        smtp.quit()

send_mail_with_attachment(EMAIL_FROM,EMAIL_TO,SUBJECT,TEXT,FILES,SMTP_HOST,SMTP_PORT,SENDER_LOGIN,SENDER_PASSWORD,SMTP_IS_STARTTLS)



When I run it, I have the following message :

Traceback (most recent call last):
  File "snippet.email.envoyer_un_mail_au_format_html.py", line 46, in <module>
    
send_mail_with_attachment(EMAIL_FROM,EMAIL_TO,SUBJECT,TEXT,FILES,SMTP_HOST,SMTP_PORT,SENDER_LOGIN,SENDER_PASSWORD,SMTP_IS_STARTTLS)
  File "snippet.email.envoyer_un_mail_au_format_html.py", line 42, in 
send_mail_with_attachment
    smtp.login(username,password)
  File "/usr/lib/python3.2/smtplib.py", line 595, in login
    self.ehlo_or_helo_if_needed()
  File "/usr/lib/python3.2/smtplib.py", line 554, in ehlo_or_helo_if_needed
    if not (200 <= self.ehlo()[0] <= 299):
  File "/usr/lib/python3.2/smtplib.py", line 421, in ehlo
    (code, msg) = self.getreply()
  File "/usr/lib/python3.2/smtplib.py", line 367, in getreply
    line = self.file.readline(_MAXLINE + 1)
TypeError: readline() takes exactly 1 positional argument (2 given)

----------

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue25045>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to