无法使用CID将文件添加为内联附件

2024-10-01 00:29:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我花了好几个小时寻找解决办法,但都没有结果。我试图在自动生成的电子邮件中包含一些图像,遇到了一些问题。我不能包括实际的网址,因为gmail完全阻止图片,所以我试图作为附件发送,然后使用Cids引用附件。问题是我还没找到办法。任何帮助都是王牌。你知道吗

我在Ubuntu服务器上用Apache2运行Python3.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,我可以用在我挣扎的地方。你知道吗


Tags: 图像附件valueemailhtml图片msgconnection