【问题标题】:Cannot add file as inline attachment python flask using CID无法使用 CID 将文件添加为内联附件 python 烧瓶
【发布时间】:2019-04-01 04:16:54
【问题描述】:

我花了几个小时寻找解决方案,但无济于事。我正在尝试在自动生成的电子邮件中包含一些图像,但遇到了一些问题。我不能包含实际的 url,因为 gmail 完全阻止了图像,所以我试图作为附件发送,然后使用 Cids 来引用附件。问题是我还没有找到一种方法来做到这一点。任何帮助都是王牌。

我在 Ubuntu 服务器上使用 Apache2 运行 python 3.6。我曾尝试在 base64 中编码图像,但这根本不起作用。电子邮件中的图像根本没有显示出来。

def createVoucher(email, expiry):
    voucherId = str(uuid.uuid4())
    email = email
    value = 1
    expiryDate = expiry
    redeemed = 1
    connection = mysql.get_db()
    cursor = connection.cursor()
    cursor.execute("INSERT INTO vouchers (VoucherID, Value, ExpiryDate, Redeemed, Email) VALUES (%s,%s,%s,%s,%s)", (voucherId, value, expiryDate, redeemed, email))
    msgBody = render_template('admin/eVoucherEmail.html', voucherId=voucherId, expiry=expiry)
    msg = Message('New Sunday Funday eVoucher Received', sender = MAIL_USERNAME, recipients = [email])
    msg.html = msgBody
    with app.open_resource("static/img/Facebook.jpg") as fp:
        msg.attach("Facebook.jpg", "image/jpg", fp.read())
    mail.send(msg)    
    connection.commit()

所以发布的代码可以很好地附加文件,它只是分配一个我可以在我遇到困难的地方使用的内容 ID。

【问题讨论】:

    标签: python flask-mail


    【解决方案1】:

    这是我使用 flask_mail 插入内联 jpg 的方法。

    版本:python=3.7.6、flask=1.1.2 和 flask-mail=0.9.1

    message.attach 中的“disposition”需要是“inline”,img 的 html 应该包含“cid:my_cid”作为 src。

    import uuid
    from flask import Flask
    from flask_mail import Mail, Message
    from pathlib import Path
    
    app = Flask(__name__)
    app.config.update(
        MAIL_SERVER='smtp.gmail.com', MAIL_PORT=465, MAIL_USE_SSL=True,
        MAIL_USERNAME="myfakeemail54858939@gmail.com", MAIL_PASSWORD="myfakepw54858939")
    flask_mail = Mail(app)
    
    def test_send_voucher():
        to_addr = "myfakeemail54858939@gmail.com"
        expiry = "29 February"
        voucher_png_path = Path(__file__).parent / "static/Facebook.jpg"
        sendVoucherEmail(app, to_addr, expiry, voucher_png_path)
    
    def sendVoucherEmail(app: Flask, to_addr: str, expiry: str, voucher_png_path: Path):
        voucher_id = str(uuid.uuid4())
        html = f"""<html><head></head><body>
        <p>Congratulations on your voucher!<br>Your voucher code is {voucher_id}. 
        The offer expires on {expiry}.<br>
        <img src="cid:voucher_png" width=200>
        </p></body></html>"""
        with app.app_context():
            mail = Mail(app)
            message: Message = Message(subject="my subject", sender="myfakeemail54858939@gmail.com", 
                                       recipients=[to_addr], html=html)
            with app.open_resource(voucher_png_path) as fp:
                message.attach(filename="myfilename.png", content_type="image/png", data=fp.read(), 
                               disposition="inline", headers=[['Content-ID', '<voucher_png>']])
            mail.send(message)
    

    此示例通过 gmail 成功发送了一封电子邮件,其中包含内嵌图片“Facebook.jpg”,如原始问题中所示。

    screenshot of received email

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-06-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-30
      相关资源
      最近更新 更多