开发者

Sending Image via Email throught AJAX

开发者 https://www.devze.com 2023-02-09 17:27 出处:网络
Using the code below, I am able to send an email attaching an image from another server. <$php $to = \"your@email.com\";

Using the code below, I am able to send an email attaching an image from another server.

<$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-$r开发者_JS百科andom_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;

But, when I try to use this code via AJAX (jquery), it sends the email but with no attachment. Is there something I am missing?

Here is my AJAX jquery script:

$.ajax({
    url: '&lt;?php echo site_url("ajax/email_wallpaper/".$post_details->id); ?&gt;',
    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
    }
});


what is this? You do not mention this site_url() function above...

$.ajax({
url: '&lt;?php echo site_url("ajax/email_wallpaper/".$post_details->id); ?&gt;',

What you are going to want to try is not writing any php in your javascript.

The first bit of PHP you posted needs to be put into a PHP file so you can call that file in the parameter.

The code does not necessarily need to be in that file you can of course just call a php function but you would include the functions file within the file the ajax will call.

so rather than the above code your ajax URL parameter would look like:

$.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
}

});

Try doing this then attempt to see what the problem is again...

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号