• 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;
        return 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 <noreply@freetheblobs.com>' . "\r\n";
                $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&emailValue=example@email.com

    You can see what this is doing wrong by putting your email in the “example@email.com” 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?

Viewing 15 replies - 1 through 15 (of 20 total)
  • possible could be theme or plugin conflict disable all and try

    Moderator bcworkz

    (@bcworkz)

    You are trying to use undefined variables in your AJAX callback: $intro, $path If these are properly defined within the callback, you should be able to send a complete message with attachment.

    $intro is easy enough to define. $path for the attachment needs to be passed as an URL parameter. Though if the file is also an attachment, you could just send the attachment ID, then get the path from the DB.

    If you check your error log, you will find PHP is probably telling you what is wrong with your code. “Notice: Undefined variable intro in [directory path to code file] on line ##” Basically what I just told you 🙂

    Thread Starter kjgbriggs

    (@kjgbriggs)

    I don’t get any errors at all unfortunately.

    I don’t know how I would get $intro in there, would I just redefine it the same way I did before, but in the AJAX code?

    Moderator bcworkz

    (@bcworkz)

    Yes, redefine it. Despite being on the same file, the AJAX callback is separate and unrelated to the rest of the file. The only values known to the callback is what is included in the URL parameters. Any variables (except globals) declared outside of the callback are useless within the callback, they are “out of scope”.

    Some server configurations suppress the logging of non-fatal errors, which makes debugging AJAX a lot more difficult. If you cannot directly modify the php.ini configuration, the error logging configuration can sometimes be re-defined at run-time, unless this ability has also been suppressed in php.ini.

    Hopefully, after these adjustments, everything works and there is no need for detailed error logging 🙂

    what about setting variables to global?
    global $intro;
    global $path;

    Moderator bcworkz

    (@bcworkz)

    It may work, it depends if the original values are static or dynamic. If they don’t change once defined, then yes. If they change along the way, such as due to a loop, then no, you’ll get the last value which may not be the right value.

    What will work are session variables, they are pretty much meant for situations like this. http://php.net/manual/en/intro.session.php

    Thread Starter kjgbriggs

    (@kjgbriggs)

    Both redefining and setting them as global didn’t seem to do anything to rectify the situation.

    I am fairly certain that all the problems I have stem from not being able to trigger the AJAX from the button (so getting any information currently on that page doesn’t work as I have to call the function manually through a URL).

    At the moment, my email send button:
    <button onclick="emailsend();" class="downloadBtn" style="margin-top: 0px; width: 100%;">Email for Mobile</button>

    Doesn’t do anything when clicked apart from make the page reload, nothing sends and nothing is in the console.

    I think my issues are that as I have to call the function from a URL outside of that page to get it to send an email, it no longer has access to any of the stuff it is trying to get, such as $intro and all the others.

    I am not sure what is wrong with it, I’m a C# developer but am trying to move into a little PHP but do far I absolutely hate it (not the point).

    Can anyone tell me what I have done wrong to prevent the button from actually sending the email?

    ok nm found it...
    <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>

    it all looks good… maybe <script type=”text/javascript”>

    also curious why does your test sample link
    http://com.areonline.co.uk//wp-admin/admin-ajax.php?action=download_email_send&emailValue=example@email.com
    use //wp-admin^

    Thread Starter kjgbriggs

    (@kjgbriggs)

    Any ideas as to what might be wrong?

    I have found that if I set the iframe’s src to the email URL (hardcode it), the email sends when the page loads, but again, no custom text, no attachment 🙁

    remove

    '
    
    And this is the AJAX code to handle the actual sending of the emails:
    
        '

    this line is giving me errors.
    ‘add_action(‘wp_ajax_download_email_send’,’download_email_send’);

    Thread Starter kjgbriggs

    (@kjgbriggs)

    Hmm, I haven’t been getting any errors on it.

    I did remove it though and it doesn’t seem to make the slightest bit of difference 🙁

    Thread Starter kjgbriggs

    (@kjgbriggs)

    Update: removing that line removes the action required for calling that URL, so that would remove the ability to even access the AJAX function to send emails.

    no dont remove the action just the ‘…’
    i think the ‘ infront of add-action is also a problem.
    ‘add_action
    should be
    add_action

    Thread Starter kjgbriggs

    (@kjgbriggs)

    Oh, you got that from the initial post…
    I didn’t put that in there, that was wp.org’s code tag thing.

    My code is as below:

    /*		Shortcode		*/
    
    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: 20px 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: 0px !important;
    				border: 0px solid #000 !important;
    				margin-top: 14px !important;
    				margin-bottom: 8px !important;
    				background-color: #eeeeee !important;
    				font-size: 18px;
    				color: #999999 !important;
    			}
    		</style>
    <?php
    global $intro;
    global $path;
    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 <?php echo $post->post_title ?> (<?php echo basename($intro, ".ftb") ?>)</h3>
    			<p style="margin-bottom: 20px;">Please note: FTB files can only be used if you have Free The Blobs (Full Version) on Android or iOS.</p>
      			<a href="<?php echo $intro;?>" download="<?php echo basename($intro) ?>" class="demoBtn">Download <?php echo basename($intro, ".ftb") ?> for PC</a>
      			<!--<a href="#" class="demoBtn">Demo</a>--><br>
    			<input type="text" name="emailValue" id="emailValue" placeholder="Email Address" class="emailInput" style="text-align: center;">
    			<button  onclick="javascript: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
    return ob_get_clean();
    }
    add_shortcode('level', 'file_download');
    
    /*		AJAX		*/
    
    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() {
    	$intro = get_post_meta($post -> ID, $key = 'podcast_file', true);
        $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 <noreply@freetheblobs.com>' . "\r\n";
            $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;
    }

    u have $intro defined in both functions.
    $intro = get_post_meta($post -> ID, $key = 'podcast_file', true);

    also maybe try to echo variables on page with an exit.

    function download_email_send() {
    echo $_REQUEST['postID'];
    exit;
    }

    do u see the intro string or anything?

Viewing 15 replies - 1 through 15 (of 20 total)
  • The topic ‘wp_mail, front-end and email attachments?’ is closed to new replies.