在Local vs Live上使用MailGun附加文件

时间:2016-03-31 22:06:36

标签: python mailgun

非常简单,我可以在本地附加文件,但是当我部署此代码时,附件不会通过......

def send_email(subject, sender, recipients, html_body, attachment_path=None):

    if attachment_path != None:
        if ENV == "local":
            path = open(os.path.realpath(attachment_path), "rb") 
        else:
            path = open("/app/" + attachment_path, "rb") 
        files = [("attachment", path)]  

    else:
        files = None        

    return requests.post(
        "https://api.mailgun.net/v3/mg.lhvtalenttracker.com/messages",
        auth=("api", MAILGUN_KEY),
        files=files, 
        data={"from": "LHV TalentTracker <postmaster@mg.lhvtalenttracker.com>",
                "to": recipients,
                "subject": subject,
                "html": html_body})

1 个答案:

答案 0 :(得分:0)

我不知道为什么本地与现场的行为不同,但答案相当简单......

files = [("attachment", path)]

应该是......

files = [("attachment", ("file_name", path))]