Git Product home page Git Product logo

pyzmail's Introduction

pyzmail

Pyzmail is a high level mail library for Python, providing functions to read, compose and send emails. Pyzmail hides the problems of using multiple encoding and the complexity of the MIME structure encoding/decoding. Pyzmail is available for python 2.6+ and 3.2+ under the LGPL license.

More info here: http://www.magiksys.net/pyzmail

pyzmail's People

Contributors

aspineux avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

pyzmail's Issues

Unable to install on Python 3.6

Hi,

After running the command in pip install pyzmail, i get an issue with distribute. I am unable to find the maintenance page of that package so i decided to report it here instead.

Collecting pyzmail
  Using cached pyzmail-1.0.3.tar.gz
Collecting distribute (from pyzmail)
  Using cached distribute-0.7.3.zip
    Complete output from command python setup.py egg_info:
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/tmp/pip-build-qu6a358p/distribute/setuptools/__init__.py", line 2, in <module>
        from setuptools.extension import Extension, Library
      File "/tmp/pip-build-qu6a358p/distribute/setuptools/extension.py", line 5, in <module>
        from setuptools.dist import _get_unpatched
      File "/tmp/pip-build-qu6a358p/distribute/setuptools/dist.py", line 7, in <module>
        from setuptools.command.install import install
      File "/tmp/pip-build-qu6a358p/distribute/setuptools/command/__init__.py", line 8, in <module>
        from setuptools.command import install_scripts
      File "/tmp/pip-build-qu6a358p/distribute/setuptools/command/install_scripts.py", line 3, in <module>
        from pkg_resources import Distribution, PathMetadata, ensure_directory
      File "/tmp/pip-build-qu6a358p/distribute/pkg_resources.py", line 1518, in <module>
        register_loader_type(importlib_bootstrap.SourceFileLoader, DefaultProvider)
    AttributeError: module 'importlib._bootstrap' has no attribute 'SourceFileLoader'
    
    ----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-qu6a358p/distribute/

pyzmail crashes when parsing a mail with badly encoded UTF-8 header

Hello aspineux,

I have a crash happenning at pyzmail, at some rare malformed mail file. It seems like a pyzmail mistreating such file.

Details:
Python version:pyt 3.4.2
pyzmail - 1.0.3
Linux - Debian 8

$ grep version /usr/lib/python3.4/email/init.py
version = '5.1.0'

Crash reason:
If header can not be encoded (UTF-8 is badly encoded), Compat32._sanitize_header() at _policy_base.py doesn't return a string, but an instance of class email.header.Header
That causes pyzmail to crash when trying to activate Header.startswith()

Reproduce:
Take the attached file, and run from python3:
import pyzmail
pyzmail.message_from_bytes(open('/tmp/mail_utf8_error', 'rb').read())

Traceback (most recent call last):
File "", line 1, in
File "/usr/local/lib/python3.4/dist-packages/pyzmail/parse.py", line 774, in message_from_bytes
return PyzMessage(email.message_from_bytes(s, _args, *_kws))
File "/usr/local/lib/python3.4/dist-packages/pyzmail/parse.py", line 634, in init
self.mailparts=get_mail_parts(self)
File "/usr/local/lib/python3.4/dist-packages/pyzmail/parse.py", line 482, in get_mail_parts
mailparts.append(MailPart(part, filename=filename, type=type, charset=charset, content_id=part.get('Content-Id'), description=part.get('Content-Description'), disposition=disposition, is_body=parts.get(part, False)))
File "/usr/local/lib/python3.4/dist-packages/pyzmail/parse.py", line 98, in init
if self.content_id.startswith('<') and self.content_id.endswith('>'):
AttributeError: 'Header' object has no attribute 'startswith'

mail_utf8_error.zip

Documentation: some more examples for common use cases (adding extra headers)

I'm opening this as an issue so it's tracked. In particular, it would be nice to have in the docs some indication on how to add extra headers for common operations like Disposition-Notification-To. I had to hunt down the API docs and I couldn't quite get them to work (malformed email addresses).

So, a little example in the documentation would help a lot.

easy way to do content replacement

Assuming i'd be using pyzmail as an email processor which replaces all url's with a shortener (for tracking), is the following approach correct?

for part in msg.mailparts:
    payload = part.get_payload() (this also decodes it and trasnforms it into unicode)
    if payload.type in ["text/plain", "text/html"]:
        # process payload in some way, replace links, etc..
        payload = payload.encode(part.charset)
        cte = part.part.get("Content-Transfer-Encoding") or "7bit"
        part.part.set_payload(encode payload according to cte, quopri, base64, etc..)

If this is correct, do you support a patch for a set_payload function?

File name for message/rfc822 parts is always message.eml

Hi,

I've noticed a problem with the names of the files from message/rfc822 parts. Instead of getting the file name from the mail part you are fixing it to message.eml.

The bug is on line 465 in parse.py.

Instead of:
filename='message.eml'
it should be:
filename=get_filename(part)

Cheers,
Petar

issue extracting attachments with improper boundary

I recently ran across an email where the attachment could not be extracted using pzymail. The issue appears to be with the parsing of boundaries and over relying on them to extract content. Below is an example that will not extract the attachment.

To: [email protected]
Subject: Testing
Message-ID: <[email protected]>
Return-Path: [email protected]
Date: Tue, 06 Oct 2015 11:25:00 +0000
From: "testing" <[email protected]>
MIME-Version: 1.0
Content-Type: multipart/mixed; charset="UTF-8"; boundary="b1_000001"
Content-Transfer-Encoding: 8bit
Content-Disposition: inline

--b1_000001
Content-Type: multipart/alternative;
    boundary="b3_000001"

--b3_000001
Content-Type: text/plain; format=flowed; charset="UTF-8"
Content-Transfer-Encoding: 8bit

testing

--b3_000001
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: 8bit

<html>
<head>
</head>
<body>
testing
</body>
</html>

--b3_000001--
--b1_000002
Content-Type: application/octet-stream;
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
 filename="file.txt"

VGhpcyBpcyBhIHRlc3QgZmlsZS4K

--b1_000002--

Encoded attachment filenames are not correctly decoded

To recreate:

>>> import pyzmail
>>> m = pyzmail.message_from_string("""Content-Type:application/pdf
Content-Transfer-Encoding: base64
Content-Disposition:attachment;
 filename="=?ISO-8859-1?B?RE9DMDkw?=
 =?ISO-8859-1?B?OTEzLTA5MDky?=
 =?ISO-8859-1?B?MDEzMTIyNTM0?=
 =?ISO-8859-1?B?LnBkZg==?="
""")
>>> m.get_filename()
'=?ISO-8859-1?B?RE9DMDkw?=\n =?ISO-8859-1?B?OTEzLTA5MDky?=\n =?ISO-8859-1?B?MDEzMTIyNTM0?=\n =?ISO-8859-1?B?LnBkZg==?='

The filename should be decoded:

>>> import email.header
>>> email.header.decode_header(m.get_filename())
[('DOC090913-09092013122534.pdf', 'iso-8859-1')]

Problem with distribute package

On Python Version: 3.5.9
I can't install pyzmail==1.0.3 because I receive the follow error:

Collecting pyzmail; python_version < "3.6"
 Downloading pyzmail-1.0.3.tar.gz (57 kB)
Collecting pytz
 Downloading pytz-2020.1-py2.py3-none-any.whl (510 kB)
Collecting lockfile
 Downloading lockfile-0.12.2-py2.py3-none-any.whl (13 kB)
Collecting distribute
 Downloading distribute-0.7.3.zip (145 kB)
   ERROR: Command errored out with exit status 1:
    command: /usr/local/bin/python -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-w5vr59s7/distribute/setup.py'"'"'; __file__='"'"'/tmp/pip-install-w5vr59s7/distribute/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' egg_info --egg-base /tmp/pip-pip-egg-info-6v571h0r
        cwd: /tmp/pip-install-w5vr59s7/distribute/
   Complete output (11 lines):
   Traceback (most recent call last):
     File "<string>", line 1, in <module>
     File "/tmp/pip-install-w5vr59s7/distribute/setuptools/__init__.py", line 2, in <module>
       from setuptools.extension import Extension, Library
     File "/tmp/pip-install-w5vr59s7/distribute/setuptools/extension.py", line 2, in <module>
       import distutils.core
     File "/usr/local/lib/python3.5/site-packages/_distutils_hack/__init__.py", line 83, in create_module
       return importlib.import_module('._distutils', 'setuptools')
     File "/usr/local/lib/python3.5/importlib/__init__.py", line 126, in import_module
       return _bootstrap._gcd_import(name[level:], package, level)
   ImportError: No module named 'setuptools._distutils'
   ----------------------------------------
ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.

Could anyone help me, please?

Thank you

code is not properly translated for python33 with pip on windows

Sir,
Just in case, I tried to put pyzmail at work with Python33 rather than the "old" Python27 I usually stick with, on my windows-xpsp3 "box".
Taking care to use the appropriate pip version, I simply run:

pip install pyzmail

from my C:\Python33\Scripts folder.
Unfortunately the resulting code, beginning with the pyzsendmail script, has many errors.

How to attach binary files to outgoing emails

Hi,

thank you for this great module. Until now, it has provided a useful support.

Unfortunately, all the examples I have seen so far (even from the API documentation) show only how to attach TEXT FILES or a GIF IMAGE into an html email. What I want to do in short:

My application receives an email and extracts its attachments. After deciding what to do, some of these attachments (PDF, text, image,...) are to be forwarded to various revipients using the pyzmail module. But I can not figure out how to correctly tell the compose_mail() function how to do this. Here is my code so far:

# create new payload
payload, mail_from, rcpt_to, msg_id = pyzmail.compose_mail(
    ( <sendername> , <senderemail> ),
    [ <receiveremail> ],
    <subjectstring> ,
    'utf-8',
    ( <bodytext> , 'utf-8'),
    attachment=[( ...???... )]
    )

For example:

I have these three files within my folder /home/temp/:

  • test.PDF
  • test.PNG
  • test.TXT

How would I design the attachment part of the compose_mail() function to successfully get these files to the desired receiver?

Thank you in advance.

Couldn't install onto Python 3.4

Hello

I get the following error when trying to install on Python 3.4, any ideas?

Thanks

(venv3.4)pgtrafficcop@core01:/opt/pythonapps/sunnyboy$ pip install pyzmail
Downloading/unpacking pyzmail
Downloading pyzmail-1.0.2.tar.gz (57kB): 57kB downloaded
Running setup.py (path:/home/pgtrafficcop/venv3.4/build/pyzmail/setup.py) egg_info for package pyzmail
Downloading http://pypi.python.org/packages/source/ ... .21.tar.gz
Extracting in /tmp/tmp9z06l8ae
Now working in /tmp/tmp9z06l8ae/distribute-0.6.21
Building a Distribute egg in /home/pgtrafficcop/venv3.4/build/pyzmail
Failed to import the site module
Traceback (most recent call last):
File "/tmp/tmp9z06l8ae/distribute-0.6.21/site.py", line 73, in
__boot()
File "/tmp/tmp9z06l8ae/distribute-0.6.21/site.py", line 2, in __boot
import sys, imp, os, os.path
File "/home/pgtrafficcop/venv3.4/lib/python3.4/imp.py", line 22, in
from importlib import util
File "/home/pgtrafficcop/venv3.4/lib/python3.4/importlib/util.py", line 12, in
from contextlib import contextmanager
ImportError: No module named 'contextlib'
/home/pgtrafficcop/venv3.4/build/pyzmail/distribute-0.6.21-py3.4.egg
Traceback (most recent call last):
File "/home/pgtrafficcop/venv3.4/build/pyzmail/distribute_setup.py", line 143, in use_setuptools
raise ImportError
ImportError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "", line 17, in
File "/home/pgtrafficcop/venv3.4/build/pyzmail/setup.py", line 11, in
use_setuptools()
File "/home/pgtrafficcop/venv3.4/build/pyzmail/distribute_setup.py", line 145, in use_setuptools
return _do_download(version, download_base, to_dir, download_delay)
File "/home/pgtrafficcop/venv3.4/build/pyzmail/distribute_setup.py", line 125, in _do_download
_build_egg(egg, tarball, to_dir)
File "/home/pgtrafficcop/venv3.4/build/pyzmail/distribute_setup.py", line 116, in _build_egg
raise IOError('Could not build the egg.')
OSError: Could not build the egg.
Complete output from command python setup.py egg_info:
Downloading http://pypi.python.org/packages/source/ ... .21.tar.gz

Extracting in /tmp/tmp9z06l8ae

Now working in /tmp/tmp9z06l8ae/distribute-0.6.21

Building a Distribute egg in /home/pgtrafficcop/venv3.4/build/pyzmail

Failed to import the site module

Traceback (most recent call last):

File "/tmp/tmp9z06l8ae/distribute-0.6.21/site.py", line 73, in

__boot()

File "/tmp/tmp9z06l8ae/distribute-0.6.21/site.py", line 2, in __boot

import sys, imp, os, os.path

File "/home/pgtrafficcop/venv3.4/lib/python3.4/imp.py", line 22, in

from importlib import util

File "/home/pgtrafficcop/venv3.4/lib/python3.4/importlib/util.py", line 12, in

from contextlib import contextmanager

ImportError: No module named 'contextlib'

/home/pgtrafficcop/venv3.4/build/pyzmail/distribute-0.6.21-py3.4.egg

Traceback (most recent call last):

File "/home/pgtrafficcop/venv3.4/build/pyzmail/distribute_setup.py", line 143, in use_setuptools

raise ImportError

ImportError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):

File "", line 17, in

File "/home/pgtrafficcop/venv3.4/build/pyzmail/setup.py", line 11, in

use_setuptools()

File "/home/pgtrafficcop/venv3.4/build/pyzmail/distribute_setup.py", line 145, in use_setuptools

return _do_download(version, download_base, to_dir, download_delay)

File "/home/pgtrafficcop/venv3.4/build/pyzmail/distribute_setup.py", line 125, in _do_download

_build_egg(egg, tarball, to_dir)

File "/home/pgtrafficcop/venv3.4/build/pyzmail/distribute_setup.py", line 116, in _build_egg

raise IOError('Could not build the egg.')

OSError: Could not build the egg.


Cleaning up...
Command python setup.py egg_info failed with error code 1 in /home/pgtrafficcop/venv3.4/build/pyzmail
Storing debug log for failure in /home/pgtrafficcop/.pip/pip.log
(venv3.4)pgtrafficcop@core01:/opt/pythonapps/sunnyboy$

This project still maintained alive?!

I'm going to use this library for a long running project so I might probably find one thing or another to improve in the library itself. @aspineux are you willing to review/merge eventual changes?

Attachment with encoded filename

I recently implented an email project with pyzMail and it worked well.

We did run into one problem with an email that contained the following:

Content-Disposition: attachment;
	filename="=?windows-1252?Q?Beleg_Georg_Steinh=E4u=DFer=2EPDF?="

I dont know how common these encoded filenames are and I understand there a quite a few RFC covering attachment filename options. I programmed around it in python by parsing the string and using binascii.unhexlify()

Wanted to bring it to your attention.

Thanks,

George

Attachment corruption using pyzsendmail

Sir,

Before all, thanks for your hard work in order to provide a more user-friendly email library.
However I've made various attempts to send an attachment using the included pyzsendmail which have all proven unsuccessful. My files somehow got "corrupted" (they seem "tailed") when downloading them back. I tried both application/x-bittorrent and image/jpg types with similar results.
I did inspect the code, but I still fail to see where the problem could lie.
Note that I could send the very same files in similar conditions (sender=recipient=me, smtp server) without corruption, using python's "low level" email and smtplib libs.

Settings: WinXPsp3, python 2.7.6, pyzmail package installed with a "standard" pip command.

indentation error in utils.py

started using python 3.7 and when i added pyzmail to my env and run a program using it i got this error:

Traceback (most recent call last): File "d:/code/emailtrade/getsignal.py", line 3, in <module> import pyzmail File "<frozen importlib._bootstrap>", line 983, in _find_and_load File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked File "<frozen importlib._bootstrap>", line 668, in _load_unlocked File "<frozen importlib._bootstrap>", line 638, in _load_backward_compatible File "C:\Users\death\Miniconda3\envs\emailtrade\lib\site-packages\pyzmail-1.0.3-py3.7.egg\pyzmail\__init__.py", line 7, in <module> File "<frozen importlib._bootstrap>", line 983, in _find_and_load File "<frozen importlib._bootstrap>", line 963, in _find_and_load_unlocked File "<frozen importlib._bootstrap>", line 906, in _find_spec File "<frozen importlib._bootstrap_external>", line 1280, in find_spec File "<frozen importlib._bootstrap_external>", line 1254, in _get_spec File "<frozen importlib._bootstrap_external>", line 1235, in _legacy_get_spec File "<frozen importlib._bootstrap>", line 441, in spec_from_loader File "<frozen importlib._bootstrap_external>", line 594, in spec_from_file_location File "C:\Users\death\Miniconda3\envs\emailtrade\lib\site-packages\pyzmail-1.0.3-py3.7.egg\pyzmail\utils.py", line 31 b'\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f' \ ^ IndentationError: unexpected indent

Does not happen if I use the 3.6 version installed using easy_install or pip

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.