webdesignerchristian on "wp_mail, front-end and email attachments?"

ساخت وبلاگ

You would not believe the amount of trouble I have had these past few days, I now understand the phrase: "5% of the time is spent doing 95% of the work, 95% is spent on the last 5%".

I am trying frantically (needs to be done in the next week or so) to get anything working, and I can worry about neatening it up later, but I have been having huge trouble doing even that.

I am trying to use wp_mail to send an email with an attachment, but there are a few issues:
1) The attachment does not appear
2) I have to use the direct URL to send the email (not working on button)
3) The variable names for dynamic text fro the email don't work.

The code can be found below, this code covers the front-end display (added in with shortcode), and the "backend" stuff which supposedly sends the email.

Just a heads up, a lot of this code is irrelevant to the issue(just trying to be thorough), this is the front-end bits(shortcoded bits):

function file_download(){ ob_start(); ?> <style> .downloadHolder { background: #181D25; box-shadow: inset 0 0 42px 0 rgba(100,100,100,0.1); margin: 0 0 2.5rem 0; padding: 40px 40px; text-shadow: 0 1px 1px #000000; text-align: center; } .downloadBtn { background-color: #005dac; background-image: -webkit-linear-gradient(top,#0077b3,#0036a3 100%); background-image: linear-gradient(to bottom,#0077b3,#0036a3 100%); color: #fff; text-decoration: none; border-radius: 3px; border: 1px solid; box-shadow: inset 0 1px 0 rgba(255,255,255,0.22),0 1px 2px rgba(0,0,0,0.05); text-shadow: 0 1px 0 rgba(0,0,0,0.5); padding: .5rem 1rem; font-size: 1.125rem; border-color: #002a80 #002a80 #013013; } .downloadBtn:hover { background-color: #006dcc; background-image: -webkit-linear-gradient(top,#08c,#04c 100%); background-image: linear-gradient(to bottom,#08c,#04c 100%); border-color: #0036a3 #0036a3 #026; } .demoBtn { background-color: #fafafa; background-image: -webkit-linear-gradient(top,#fefefe,#f4f4f4 100%); background-image: linear-gradient(to bottom,#fefefe,#f4f4f4 100%); color: #333; text-decoration: none; border-radius: 3px; border: 1px solid; box-shadow: inset 0 1px 0 rgba(255,255,255,0.22),0 1px 2px rgba(0,0,0,0.05); text-shadow: 0 1px 0 rgba(255,255,255,0.5); padding: .5rem 1rem; font-size: 1.125rem; border-color: #e0e0e0 #e0e0e0 #c1c1c1; } .demoBtn:hover { background-color: #ffffff; background-image: -webkit-linear-gradient(top,#F3F3F3, #E1E1E1 100%); background-image: linear-gradient(to bottom,#F3F3F3, #E1E1E1 100%); border-color: #F3F3F3 #F3F3F3 #E1E1E1; } .emailInput { width: 100%; height: 40px; border-radius: 3px; margin-top: 14px !important; margin-bottom: 14px !important; } </style> <?php global $post; $intro = get_post_meta($post -> ID, $key = 'podcast_file', true); $path = str_replace("http://com.areonline.co.uk/wp-content","",$intro); ?> <div class="downloadHolder"> <h3>Download</h3> <p style="margin-bottom: 20px;">Please note: FTB files can only be used if you have Free The Blobs on Android or iOS.</p> <a>" download="<?php echo basename($intro) ?>" class="demoBtn">Download for PC</a> <!--<a href="#">Demo</a>--> <input type="text" name="emailValue" id="emailValue" placeholder="Email Address" class="emailInput" style="text-align: center;"> <button onclick="emailsend();" class="downloadBtn" style="margin-top: 0px; width: 100%;">Email for Mobile</button> <span>(We do NOT collect email addresses.)</span> <script>function emailsend() { email = document.getElementById('emailValue').value; downloadurl = '<?php admin_url('admin-ajax.php'); ?>?action=download_email_send&postId=<?php echo $post->ID; ?>&emailValue='+email; document.getElementById('emailsendframe').src = downloadurl; }</script> <!-- iframe for submitting to --> <iframe name="emailsendframe" id="emailsendframe" src="javascript:void(0);" style="display:none;"></iframe> </div> <?php echo WP_CONTENT_DIR . $path; retu ob_get_clean(); } add_shortcode('level', 'file_download');'
And this is the AJAX code to handle the actual sending of the emails: 'add_action('wp_ajax_download_email_send','download_email_send'); add_action('wp_ajax_nopriv_download_email_send','download_email_send'); // note $_REQUEST will work with $_POST or $_GET methods function download_email_send() { $to = $_REQUEST['emailValue']; // preferably add some email address format validation here // $validate = some_email_validate_function($to); // if ($validated) {$message = 'Please check your email for typos.';} // else { $post_id = $_REQUEST['postID']; // ! you would need to redefine $intro here ! $subject = 'Download for'.basename($intro); $msg = 'Your download for '.basename($intro).' is attached to this email.'; $headers = 'From: Free The Blobs <[email protected]>' . ""; $mail_attachment = array(WP_CONTENT_DIR . $path,); $send = wp_mail($to, $subject, $msg, $headers, $mail_attachment); if ($send) {$message = 'Success! Check you email address.';} else {$message = 'Error: Mail sending failed.';} // } // alert the user to the result echo "<script>alert('".$message."');</script>"; exit; }

In order to get this to send an email at all, I have to use a URL to call it, and cannot use the button (I don't think it can get the email address from the input):

http://com.areonline.co.uk//wp-admin/admin-ajax.php?action=download_email_send&[email protected]

You can see what this is doing wrong by putting your email in the "[email protected]" bit at the end (we don't collect emails, don't worry :D).

As you can see, the emails do not contain all the text information, and are missing the file attachment completely.

What exactly am I doing wrong here?

WordPress ...
ما را در سایت WordPress دنبال می کنید

برچسب : نویسنده : استخدام کار wpss بازدید : 235 تاريخ : شنبه 22 خرداد 1395 ساعت: 20:50