Php 通过AJAX通过电子邮件发送图像
使用下面的代码,我可以从另一台服务器发送一封附加图像的电子邮件Php 通过AJAX通过电子邮件发送图像,php,jquery,ajax,email,Php,Jquery,Ajax,Email,使用下面的代码,我可以从另一台服务器发送一封附加图像的电子邮件 <$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;
<$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;
这是什么?您没有在上面提到此网站的url功能
$.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
}
})
尝试这样做,然后再尝试看看问题出在哪里…通过AJAX OMGThanks向@zerkms发送电子邮件,以及他富有洞察力的评论,这些评论清楚地帮助我解决了这个问题…site_url函数创建了一个绝对url,仅此而已。它是一个内置的CodeIgniter功能。此外,电子邮件发送,但没有附件。
$.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
}