如何从超文本标记语言调用wp_mail?

时间:2016-06-04 作者:K.Briggs

这是打算在下周左右,但我有很多麻烦让网站发送电子邮件到一个输入框中指定的地址。

我需要使用wp\\U mail,因为我需要能够将php变量(电子邮件、正文和附件)传递到电子邮件。

据我所知,我已经正确地设置了表单,但在搜索了一段时间后,我发现使用JQuery(特别是AJAX)是最好的/唯一的方法,但我不知道这一切是如何工作的。

我希望这里有人能向我解释这一点,以便我能尽快完成这项工作。

在我的快捷码中,这将添加到页面中(忽略脚本函数,尝试失败):

<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 href="<?php echo $intro;?>" download="<?php echo basename($intro) ?>" class="demoBtn">Download for PC</a>
            <!--<a href="#" class="demoBtn">Demo</a>--><br>
            <input type="text" name="emailValue" placeholder="Email Address" class="emailInput" style="text-align: center;">
            <br><span>(We do NOT collect email addresses.)</span><br><br>
            <button onclick="echoSendMail()" class="downloadBtn" style="width: 100% !important;">Email for Mobile</button>
        </div>
        <script>
            function echoHello(){
                alert("<?PHP emailSend(); ?>");
            }
</script>
后来在同一个php文件中,我有了这个函数($intro是上传文件(附件)的url):

function emailSend(){
    $to = $_GET[\'emailValue\'];
    $subject = \'Download for\'.basename($intro);
    $msg = \'Your download for\'.basename($intro).\'is attached to this email.\';
    $headers = \'From: My Name <[email protected]>\' . "\\r\\n";
    $mail_attachment = array(get_post_meta($post -> ID, $key = \'podcast_file\', true));
    wp_mail($to, $subject, $msg, $headers, $mail_attachment);
}

1 个回复
SO网友:majick

第一种方法是使用post 方法,更容易理解:

<!-- form with AJAX action and iframe target -->
<form method="post" action="<?php echo admin_url(\'admin-ajax.php\'); ?>" target="emailsendframe">
<!-- AJAX action field to trigger function-->
<input type="hidden" name="action" value="download_email_send">
<!-- Post ID field -->
<input type="hidden" name="intro" value="<?php echo basename($intro); ?>">
<input type="hidden" name="postId" value="<?php echo $post->ID; ?>">
<input type="submit" class="downloadBtn" value="Download">
</form>
<!-- iframe for submitting to -->
<iframe name="emailsendframe" id="emailsendframe" src="javascript:void(0);" style="display:none;"></iframe>
当然,您可以在javascript函数中使用get, 但您需要添加id 添加到电子邮件输入元素,以便轻松添加。。。(警告是,如果您在同一页面上有多个下载按钮,那么这将不起作用,因为id 需要是唯一的,您需要添加更多代码才能做到这一点。)

<!-- note id attribute is added -->
<input type="text" name="emailValue" id="emailValue" placeholder="Email Address" class="emailInput" style="text-align: center;">
<!-- button can stay as you have it -->
<button onclick="emailsend();" class="downloadBtn">Download</button>
<!-- get method AJAX email send script -->
<script>function emailsend() {
    emailvalue = document.getElementById(\'emailValue\').value;
    email = encodeURIComponent(emailvalue);
    intro = encodeURIComponent(\'<?php echo basename($intro); ?>\');
    downloadurl = \'<?php admin_url(\'admin-ajax.php\'); ?>?action=download_email_send&postId=<?php echo $post->ID; ?>&emailValue=\'+email+\'&intro=\'+intro;
    document.getElementById(\'emailsendframe\').src = downloadurl;
}</script>
<!-- iframe for submitting to -->
<iframe name="emailsendframe" id="emailsendframe" src="javascript:void(0);" style="display:none;"></iframe>
然后在你的主题的功能。php(或插件或mu插件文件夹)通过附加action 查询值到wp_ajax_ (针对登录用户)和/或wp_ajax_nopriv_ (匿名用户)WordPress中的操作:

// AJAX trigger for download_email_send action
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 \'.$_REQUEST[\'intro\'];
        $msg = \'Your download for \'.$_REQUEST[\'intro\'].\' is attached to this email.\';
        $headers = \'From: My Name <[email protected]>\' . "\\r\\n";
        $mail_attachment = array(get_post_meta($post_id, \'podcast_file\', true));
        $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;
}

相关推荐

尝试在WordPress中实现AJAX注释,遇到WP错误

我试图在WordPress中为我的评论实现Ajax,使用this tutorial. 但我在将教程中的代码集成到自己的预构建主题时遇到了问题。问题是,我要么得到一个WP错误“检测到重复注释;看来你已经说过了!”或标准500错误。以下是我得到的:下面是我对ajax的评论。js文件如下所示: * Let\'s begin with validation functions */ jQuery.extend(jQuery.fn, { /* * check i