无法使用mail命令发送MIME电子邮件正文?

时间:2018-04-02 04:12:32

标签: python macos email mime-mail

我编写了一个愚蠢的Python脚本来构建多部分/混合MIME电子邮件正文,并尝试使用mail命令发送此电子邮件正文。收件人收到邮件,但只是将其视为纯文本正文并显示其源代码。我想知道如何让我的MIME电子邮件正文被我的收件人识别或者是否不可能?感谢您的帮助。

1,这是我的mbuilder.py文件代码,用于构建MIME电子邮件正文:

#! /usr/bin/env python
# coding : utf-8

import sys, getopt, os
import MimeWriter
import quopri, base64, StringIO
import re
import urllib, mimetypes


def print_helper():
    print """

------------------------------------------ mbuilder manual ------------------------------------------

NAME
    mbuilder -- build a email body file

SYNOPSIS
    mbuilder [[-a file1,file2,...] [--attachments file1,file2,...]] [[-f file] [--file file]] content
    mbuilder [-h] [--help]

DESCRIPTION
    The mbuilder is a email body builder. It build a multimixed email body to store or to print
    to the stdout.
    -a                      the attachment file list. files item should be conjected with comma.
    --attachments           the same to -a option.
    -f                      the file to accept the output. If not specified, it is the stdout.
    --file                  the same to -f option.
    -h                      print the this manual.
    --help                  the same to -h.                    
"""


def parse_args():
    """
    this function analyze the command line arguments and return a tuple consist of three
    parts: one is a string which presents the email body text, second is a list of email
    attachment paths, last is a string which presents the output file path. If parse err
    -or happened, return an empty tuple.
    :return: ()
    """
    try:
        options, cmd_args = getopt.getopt(sys.argv[1:], "a:f:h", ["attachments=", "file=", "help"])
    except:
        return ()
    content = ""
    attachments = []
    out = ""
    for (opt, val) in options:
        if opt in ["-a", "--attachments"]:
            attachments = map(lambda item: os.path.expanduser(item), val.split(","))
            for attachment in attachments:
                if not os.path.exists(attachment):
                    return ()
        elif opt in ["-f", "--file"]:
            out = os.path.expanduser(val)
            file_dir = os.path.dirname(out)
            if not os.path.exists(file_dir):
                return ()
        elif opt in ["-h", "--help"]:
            return ()
    args_len = len(cmd_args)
    if args_len == 0:
        pass
    elif args_len == 1:
        content = cmd_args[0]
    else:
        return ()
    if len(content) == 0 and len(attachments) == 0:
        return ()
    return content, attachments, out


def write_content(writer, content):
    temp_file = StringIO.StringIO(content)
    part = writer.nextpart()
    # check if string contains non-ascii characters
    must_quote = re.compile("[\177-\377]").search
    encoding = "quoted-printable"
    encoder = lambda i, o: quopri.encode(i, o, 0)
    if content and not must_quote(content):
        encoding = "7bit"
        encoder = None
    part.addheader("Content-Transfer-Encoding", encoding)
    part_body = part.startbody("text/plain")
    if encoder:
        encoder(temp_file, part_body)
    else:
        part_body.write(content)


def write_attachments(writer, attachments):
    encoder = base64.encode
    for file_path in attachments:
        input_file = open(file_path, "rb")
        part = writer.nextpart()
        part.addheader("Content-Transfer-Encoding", "base64")
        url = urllib.pathname2url(file_path)
        mime_type = mimetypes.guess_type(url)[0]
        if not mime_type:
            mime_type = "application/octet-stream"
        part_body = part.startbody(mime_type)
        encoder(input_file, part_body)
        input_file.close()


def build(content, attachments, out_path=""):
    blurb = "if you can read this, your mailer is not MIME-aware\n"
    if len(out_path):
        out = open(out_path, "w")
    else:
        out = sys.stdout
    mime = MimeWriter.MimeWriter(out)
    mime.addheader("Mime-Version", "1.0")
    body = mime.startmultipartbody("mixed","VGhpc0lzQW5FbWFpbEJvdW5kYXJ5Cg==")
    body.write(blurb)
    write_content(mime, content)
    write_attachments(mime, attachments)
    mime.lastpart()


args = parse_args()
if args == ():
    print_helper()
else:
    build(*args)

2,然后我用这个命令发送邮件:

./mbuilder.py -a /Users/heping/Desktop/week.xlsx,/Users/heping/Desktop/person_slected.png "This is an MIME test email" | mail -s "MIME email test" heping_tsdwx@163.com,476702292@qq.com

3,不幸的是,我的收件人收到了这封电子邮件。 recipient look

0 个答案:

没有答案