通过电子邮件发送图片通过AJAX
问题描述:
使用下面的代码,我能够发送一封电子邮件,附加从另一台服务器的图像。通过电子邮件发送图片通过AJAX
<$php
$to = "[email protected]";
$subject = "A test email";
$random_hash = md5(date('r', time()));
$headers = "From: [email protected]\r\nReply-To: [email protected]";
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";
$attachment = chunk_split(base64_encode(file_get_contents('http://www.ipadwallpapermac.com/wp-content/uploads/2010/06/adriana-lima.jpg')));
$output = "
--PHP-mixed-$random_hash;
Content-Type: multipart/alternative; boundary='PHP-alt-$random_hash'
--PHP-alt-$random_hash
Content-Type: text/plain; charset='iso-8859-1'
Content-Transfer-Encoding: 7bit
Hello World!
This is the simple text version of the email message.
--PHP-alt-$random_hash
Content-Type: text/html; charset='iso-8859-1'
Content-Transfer-Encoding: 7bit
<h2>Hello World!</h2>
<p>This is the <b>HTML</b> version of the email message.</p>
--PHP-alt-$random_hash--
--PHP-mixed-$random_hash
Content-Type: application/jpeg; name=testing.jpg
Content-Transfer-Encoding: base64
Content-Disposition: attachment
$attachment
--PHP-mixed-$random_hash--";
mail($to, $subject, $output, $headers);
?>t;
但是,当我尝试通过AJAX(jQuery)使用此代码时,它发送的电子邮件,但没有附件。有什么我失踪?
这里是我的AJAX jQuery脚本:
$.ajax({
url: '<?php echo site_url("ajax/email_wallpaper/".$post_details->id); ?>',
type: 'POST',
dataType: 'json',
data: $.param({'email':$('#email_address').val()}),
crossDomain: true,
complete: function(data) {
//called when complete
},
success: function(data) {
console.log(data);
if(data.status == 'success'){
setTimeout(function(){
$('#basic-modal-content .status')
.addClass('no_bg')
.find('p')
.html('Success! The wallpaper has been sent to your email.');
setTimeout('$.modal.close();', 1000);
}, 1000);
}
},
error: function(data) {
$('#basic-modal-content .status')
.addClass('no_bg')
.find('p')
.html('Please provide a valid email address.');
console.log(data);
console.log(data.responseText);
//called when there is an error
}
});
答
这是什么?你没有提到上面这个site_url()函数...
$.ajax({
url: '<?php echo site_url("ajax/email_wallpaper/".$post_details->id); ?>',
你想要尝试的是不写任何PHP在你的JavaScript。
您发布的PHP的第一位需要放入PHP文件,以便您可以在参数中调用该文件。
代码不一定需要在那个文件中,你当然可以调用一个php函数,但是你会在ajax将调用的文件中包含函数文件。
因此而不是上面的代码中你的Ajax URL参数会是什么样子:
$.ajax({
url: '/email-with-attatchment.php',
type: 'POST',
dataType: 'json',
data: $.param({'email':$('#email_address').val()}),
crossDomain: true,
complete: function(data) {
//called when complete
},
success: function(data) {
console.log(data);
if(data.status == 'success'){
setTimeout(function(){
$('#basic-modal-content .status')
.addClass('no_bg')
.find('p')
.html('Success! The wallpaper has been sent to your email.');
setTimeout('$.modal.close();', 1000);
}, 1000);
}
},
error: function(data) {
$('#basic-modal-content .status')
.addClass('no_bg')
.find('p')
.html('Please provide a valid email address.');
console.log(data);
console.log(data.responseText);
//called when there is an error
}
});
“通过电子邮件throught AJAX发送图像”尝试这样做,然后尝试看看这个问题又是什么...
--- OMG – zerkms 2011-02-11 03:46:02
由于@zerkms和他有见地的意见已清楚地帮助我弄清楚这个问题... – Torez 2011-02-16 02:41:23