PHPMailer发送base64图像

我试图通过PHPMailer从MySQL数据库发送图像.

目前我从数据库中取出图像,base64_decode它,然后用plusses替换所有空格给出:

data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAfEAAAOzCAYAAACoPT8zAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAP+lSURBVHhe7P3nc13ZdfaL6m+4X/ix69T9oKpzT5U+dp17zzn9+rUkKlOyZbUty2pn+vVrmbYluyV1juxAEgwIBAkSOedABAIgSIJEIgIzmNlJTcWmuiU1Fewz7vOMscZecy+sDQIgWi3qrNn11DNmWGFvovdvjrnWXvsj/8vX90imTJkyZcqU6f7R+Pi46iMf/UaLZMqUKVOmTJnuH73xxhuqj/yvjw5KpkyZMmXKlOn+0S9+8QvVR/4/jx2RTJkyZcqUKdP9o//6r/9SfeR/e/yoZMqUKVOmTJnuH3nJIJ4pU6ZMmTLdZ/KSQTxTpkyZMmW6z+Qlg3imTJkyZcp0n8lLBvFMmTJlypTpPpOXDOKZMmXKlCnTfSYvGcQzZcqUKVOm+0xeMohnypQpU6ZM95m8ZBDPlClTpkyZ7jN5WSXEb8mNaMP88ht568absjl1G5dv+wvpTO1fvf64/Xsy884dGT+U3r9SffLQj+TG+3pyWt5957bUVKWPTVPnD227G3Pp9XX...

然后我使用PHPMailers AddStringAttachment:

$mail->AddStringAttachment($base64image, "Something Something.png", "base64", "image/png")

这发送正常(有其他设置,但它们不相关).但是,一旦我收到电子邮件,它就说文件已损坏.有谁知道在PHPMailer中发送base64图像的正确例程?

编辑1

我删除了对图像的所有修改,现在直接从数据库发送它.它出现为:

ZGF0YTppbWFnZS9wbmc7YmFzZTY0LGlWQk9SdzBLR2dvQUFBQU5TVWhFVWdBQUFmRUFBQU96Q0FZQUFBQ29QVDh6QUFBQUFYTlNSMElBcnM0YzZRQUFBQVJuUVUxQkFBQ3hqd3Y4WVFVQUFBQUpjRWhaY3dBQURzTUFBQTdEQWNkdnFHUUFBUCBsU1VSQlZIaGU3UDNuYzEzWmRmYUw2bSA0WC9peDY5VDlvS3B6VDVVIGRwMTd6em45IHJVa0tsT3laYlV0eTJwbiB2VnJtYllsdXlWMWp1eEFFZ3dJQkFrU09lZEFCQUlnU0lKRUlnSXptTmxKVGNXbXVpVTFGZXd6N3ZPTXNjWmVjeSBzRFFJZ1dpM3FyTm4xMURObVdHRnZvdmR2anJuV1h2c2ovOHZYOTBpbVRKa3laY3FVNmY3UiBQaTQ2aU1mL1VhTFpNcVVLVk9tVEpudUg3M3h4aHVxai95dmp3NUtwa3laTW1YS2xPbiAwUzkgOFF2VlIvNC9qeDJSVEpreVpjcVVLZFA5by8vNnIvOVNmZVIvZS95b1pNcVVLVk9tVEpudUgzbkpJSjRwVTZaTW1UTGRaL0tTUVR4VHBreVpNbVc2eiBRbGczaW1USmt5WmNwMG44bExCdkZNbVRKbHlwVHBQcE9YRE9LWk1tWEtsQ25UZlNZdkdjUXpaY3FVS1ZPbSAweGVNb2hueXBRcFU2Wk05NW04WkJEUGxDbFRwa3laN2pONVdTWEViOG1OYU1QODhodDU2OGFic2psMUc1ZHYgd3ZwVE8xZnZmNjQvWHN5ODg0ZEdUIFUzcjlTZmZMUWogVEcgM3B5V3Q1OTU3YlVWS1dQVFZQbkQyMjdHM1BwOVhYVHRndFNjIFVYOHU2dmJmOHM3NzczTSBudlBwVSBQbE9tVEpreS9WN0t5enBCM01xUHJpeW1iUE5CNmFyTUtIaC9JelAzQXZGdGI4cGlBT...

尝试打开时仍然会出错.

解析度

$base = base64_decode($row['image']);
$resource = base64_decode(str_replace(" ", "+", substr($base, strpos($base, ","))));
$mail->AddStringAttachement($resource, "Filename.png", "base64", "image/png");

结果我只做了一次解码,当我需要做2才能获得二进制数据.谢谢那些评论的人.

解决方法:

我现在碰巧遇到了这个问题.无论如何,解决方法是,如果你是check the source code of PHPMailer,它已经清楚地说明了,函数的第一个参数是获取文件的二进制数据.

这意味着,要成功发送图像,如果您已经拥有文件的base64_encoded表示,那么您应该只做base64_decode而不更改任何内容,然后将其传入.它应该可以工作.

您不需要替换long base64编码的字符串中的任何字符串. (当然,在没有数据的情况下解码它:image / png; base64,标题文本).

上一篇:使用PHP发送的邮件花费了太多时间


下一篇:php发送邮件