使用wordpress中的wp_mail()发送多个电子邮件

时间:2016-07-05 16:46:04

标签: php wordpress email

我为插件编写了以下代码,我必须从数据库中获取电子邮件ID。我添加了自定义字段my_location和pharmacy_name

该插件可以将电子邮件发送到一个电子邮件地址,但无法发送到多个电子邮件地址。

请帮帮我。

    <?php
/**
 * Plugin Name: HelloMD Mail Sender
 * Plugin URI: http://www.ankitbaid.com
 * Description: This plugin is used to send emails based on location.
 * Version: 1.0.0
 * Author: Ankit Baid
 * Author URI: http://ankitbaid.com
 * License: GPL2
 */

function html_form_code() {
    echo '<form action="' . esc_url( $_SERVER['REQUEST_URI'] ) . '" method="post">';
    echo '<p>';
    echo 'Your Requirement (required) <br />';
    echo '<textarea rows="10" cols="35" name="cf-message">' . ( isset( $_POST["cf-message"] ) ? esc_attr( $_POST["cf-message"] ) : '' ) . '</textarea>';
    echo '</p>';
    echo '<p><input type="submit" name="cf-submitted" value="Send"/></p>';
    echo '</form>';
}


function deliver_mail() {
    $toemail = "";

    $current_user = wp_get_current_user();
    /**
     * @example Safe usage: $current_user = wp_get_current_user();
     * if ( !($current_user instanceof WP_User) )
     *     return;
     */
    $currentuseremail = $current_user->user_email;
    $currentuserfirstname = $current_user->user_firstname;
    $currentuserlastname = $current_user->user_lastname;
    $currentuserphone = $current_user->user_lastname;
    $currentuserloc = $current_user->my_location;
    // if the submit button is clicked, send the email
    if ( isset( $_POST['cf-submitted'] ) ) {

        $users = get_users(array(
        'meta_key'     => sanitize_text_field('my_location'),
        'meta_value'   => sanitize_text_field($currentuserloc),
        'meta_compare' => '=',
        ));

        foreach ( $users as $row ) 
            { if ($row->pharmacy_name != '')
            {$toemail = $toemail . $row->user_email.","."";}}
            $toemail = $toemail . "";
            $allemail = explode(',', $toemail);

        // sanitize form values
        $name    = sanitize_text_field( $currentuserfirstname );
        $email   = sanitize_email( $currentuseremail );
        $subject = 'CUSTOMER QUERY FOR MEDICINES';
        $message = esc_textarea( $_POST["cf-message"] );

        // get the blog administrator's email address
        $to = $toemail;

        $headers = "From: $name <$email>" . "\r\n";
        foreach ( (array) $allemail as $recipient ) {
        // If email has been process for sending, display a success message
        if ( wp_mail( $recipient, $subject, $message, $headers ) ) {
            echo '<div>';
            echo '<p>Thanks for contacting us, expect a response soon from our pharmacies.</p>';
            echo '</div>';
            echo '<p>';
            echo $recipient . $currentuserloc . $toemail ;
            echo '</p>';
        } else {
            echo 'An unexpected error occurred';
            print_r(error_get_last());

        }
        }
    }
}


function cf_shortcode() {
    ob_start();
    html_form_code();
    deliver_mail();
    return ob_get_clean();
}
add_shortcode( 'sitepoint_contact_form', 'cf_shortcode' );

?>

0 个答案:

没有答案