Question

im try create simple email sender in python, im write this code :

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

def send_mail(send_from, send_to, subject, text, files=[], server="smtp.gmail.com", port=587, username='username', password='passwordd', isTls=True):
    msg = MIMEMultipart()
    msg['From'] = send_from
    msg['To'] = send_to
    msg['Date'] = formatdate(localtime = True)
    msg['Subject'] = subject

    msg.attach( MIMEText(text) )

    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('username@gmail.com', 'to@gmail.com', 'test python send', 'hello', files=['smtp.py'] )

and the script smtp.py work, the problem is after compile script in py2exe, the code for setup py2exe is this :

from distutils.core import setup
import py2exe
import sys

if len(sys.argv) == 1:
    sys.argv.append("py2exe")

setup( options = {"py2exe": {"compressed": 1, "optimize": 2,"dll_excludes": "w9xpopen.exe", "ascii": 1, "bundle_files": 1}},
       zipfile = None,
       console = [
        {
            "script": "smtp.py",
            "icon_resources": [(0, "icon.ico")]
        }
    ],)

if try run smtp.exe give me this error :

C:\Users\Lavoro\Desktop\Desktop>smtp.exe
Traceback (most recent call last):
  File "smtp.py", line 30, in <module>
    send_mail('username@gmail.com', 'to@gmail.com', 'test python send',
'hello', files=['smtp.py'] )
  File "smtp.py", line 15, in send_mail
    msg.attach( MIMEText(text) )
  File "email\mime\text.pyo", line 30, in __init__
  File "email\message.pyo", line 226, in set_payload
  File "email\message.pyo", line 248, in set_charset
  File "email\charset.pyo", line 212, in __init__
LookupError: unknown encoding: ascii

how can resolve this problem ? thanks all

Was it helpful?

Solution

You should change the option "ascii": 1 in options to "ascii": 0. "ascii": 1 means that your program is supposed to only use ascii strings. The encodings are not included into the exe in this case (see this).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top