dongmei9508 2019-07-27 13:45
浏览 48
已采纳

从profile.php页面发送带有wp_mail的电子邮件

I need to add a button in the profile.php pages for every user in the back-end, in order to send an email via php. I managed to do it correctly when I click the button in the profile user I'm logged into, but when I tried to do in other users, I got the wordpress error "Invalid user ID" I used the hooks show_user_profile and edit_user_profile correctly but I think there is some issue with the POST method. After this issue will be solved, I need to change dynamically the email with the user's email I'm into.

    function fileEmailNotification(WP_User $user) {

        ?>
    <h2>Invia notifica file</h2>
    <?php

    if(isset($_POST['submit']))
    {
        $to      = 'XXX@email.it';
        $subject = 'XXX';
        $body = 'XXX';
        $headers = 'Content-type: text/html;charset=utf-8' . "
";
        $headers .= 'From: XXX <XXX@XXX.it>' . "
";   

        wp_mail( $to, $subject, $body, $headers );

        echo "<script type='text/javascript'>alert('Email Sent');</script>";
    }
        submit_button('Invia notifica');
    ?>  <form action="" method="post">
        </form>
        <?php

}
add_action('show_user_profile', 'fileEmailNotification');
add_action('edit_user_profile', 'fileEmailNotification');
  • 写回答

1条回答 默认 最新

  • doulan1073 2019-07-27 23:07
    关注

    It only works when you edit your own profile because you are missing two parameters in your form when you edit someone else's (not the logged in user)'s profile :

    These 2 fields: user_id and _wp_http_referer

    You can add them to your form like this :

    <form action="" method="POST">
        <?php 
        $logged_in_user_id = get_current_user_id();
        if ($logged_in_user_id != $user->ID) : ?>
        <input type="hidden" name="user_id" value="<?= $user->ID ?>"
        <?= wp_referer_field(); ?>
        <?php endif; ?>
    </form>
    

    However, i noticed that this code adds a form inside the default profile edit form and i think this will get you in trouble.

    I'd suggest you to use a simple button linked to an ajax call.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 对于知识的学以致用的解释
  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败