使用以下代码,我可以发送附加其他服务器图像的电子邮件。
<$php
$to = "your@email.com";
$subject = "A test email";
$random_hash = md5(date('r', time()));
$headers = "From: email@example.com\r\nReply-To: email@example.com";
$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
}
});
答案 0 :(得分:0)
$.ajax({
url: '<?php echo site_url("ajax/email_wallpaper/".$post_details->id); ?>',
你想要尝试的不是在你的javascript中编写任何php。
您发布的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
}
});
尝试这样做,然后尝试再次查看问题...