所以基本上我已经尽力了。
我在我的网站上添加了一项新功能,其中包括创建一个PNG图像(html2canvas),允许用户下载图像(工作正常)和/或将其发送给朋友"朋友&# 34;或者"你自己"一旦图像被创建......
事情是我无法让PHPmailer捕获图像并将其发送到给定的电子邮件地址,它只是发送附带附件的电子邮件,但该附件是空的。
这是我的最后一段代码:
HTML:
<form id="form_id" method="post" enctype="multipart/form-data">
<input id="form_image_data" type="hidden" name="image" value="">
<button id="send">Send</button>
</form>
JS (点击&#39;发送&#39;按钮时会发生这种情况):
var canvas = document.querySelector('canvas').toDataURL();
var image = document.getElementById('form_image');
image.setAttribute('name', 'image');
var formData = new FormData($('#form_id')[0]);
$.ajax({
dataType: 'JSON',
data: formData,
type: 'POST',
cache: false,
contentType: false,
processData: false,
url: 'the_url_to_phpmailer',
success: function(data) {
alert(data.confirm);
}
});
PHP:
$email = $_POST['email'];
$image = $_POST['image'];
$content = '.....';
$mail->setFrom('myemail@myemail.com', 'Website Name');
$mail->addAddress($email);
$mail->isHTML(true);
$mail->addStringAttachment($image, 'image.png');
$mail->Subject = 'Image from website.';
$mail->Body = $content;
$mail->AltBody = $content;
$mail->send();
非常感谢! 一切都很受欢迎!
答案 0 :(得分:0)
我终于明白了!我做了大量研究,从stackoverflow中看到了一个帖子并实现了它。
这是从JS向PHPmailer动态(或非动态地)发送图像或画布的方式。
HTML:
<form id="form_id" method="post" enctype="multipart/form-data">
<canvas id="canvas"> <!--image--> </canvas>
<input id="hidden_input" type="hidden" name="image" value="">
<button id="send">Send</button>
</form>
JS:
var send_this = document.getElementById('canvas').toDataURL("image/png");
document.querySelector('#hidden_input').value=send_this;
var formData = new FormData($('#form_id')[0]);
$.ajax({
dataType: 'JSON',
data: formData,
type: 'POST',
cache: false,
contentType: false,
processData: false,
url: 'the_url_to_phpmailer',
success: function(data) {
alert(data.confirm);
}
});
PHP:
<?php
$email = $_POST['email'];
$image = $_POST['image'];
$data = substr($image, strpos($image, ",")); /*This is important in order to make it work,
the dataURL comming from JS comes with an extra string at the begining that does not belong to a base64 string,
so we remove it and base64_decode after.*/
$filename="image.png";
$encoding = "base64";
$type = "image/png";
$email = $_POST['email'];
$image = $_POST['image'];
$content = '.....';
$mail->setFrom('myemail@myemail.com', 'My Website Name');
$mail->addAddress($email);
$mail->isHTML(true);
$mail->AddStringAttachment(base64_decode($data), $filename, $encoding, $type);
$mail->Subject = 'Image from website.';
$mail->Body = $content;
$mail->AltBody = $content;
$mail->send();
?>
希望这可以帮助未来的人,它确实完美。收件人会收到附加的png图像。