如何从数据库加载 user_email 并在 Php wp_mail() 函数的 $to 中加载 post
how to load user_email from database and post it in $to of Php wp_mail() function
我正在尝试使用 wordpress 的 PHP wp_mail() 功能向注册用户发送生日邮件。但是,我无法发送电子邮件。请输入以下代码:
# Get user ID
$id = get_current_user_id();
global $wpdb;
//query to extract birthdate from database
$result = $wpdb->get_results("SELECT um.meta_value FROM `wp_usermeta` AS um WHERE um.user_id = $id AND um.meta_key = 'birth_date'");
//query to extract email from database
$query= $wpdb->get_results("SELECT u.user_email FROM `wp_users` AS u WHERE u.ID = $id");
//load data from database and store it in user_name variable
foreach ($query as $row1) {
$user_name = $row1->user_email;
}
//birth_date
$newDate = date("d-m", strtotime($birth_date ));
//echo "birth date format is: ".$newDate;
//present date
//$now = time();
//$newNow= date("d-m", strtotime($now));
$newNow = date("d-m");
//echo "Today date format is: ".$newNow;
if ($newDate == $newNow) {
$to = $user_name;
$subject = 'Happy Birthday';
$body = 'Hello Gaurav';
wp_mail( $to, $subject,$body );
}
?>
在上面的代码中,我尝试将 $to=$user_name
放在 $to 部分,它不会向注册用户发送电子邮件。但是,如果我手动输入整个电子邮件地址,即 $to= "xyz@gmail.com"
,xyz 用户会成功收到电子邮件。
我修改了你的代码。你应该检查 email
是否为空。试试下面的代码。
对于电子邮件,您可以使用 get_user_by()
获取用户
$user = get_user_by( 'id', get_current_user_id() );
要检索用户元数据,您可以使用 get_user_meta()
函数。
$birth_date = get_user_meta( $user->ID, 'birth_date', true );
function sent_birthday_mail(){
$user = get_user_by( 'id', get_current_user_id() );
if ( $user ) {
$email = $user->user_email;
$birth_date = get_user_meta( $user->ID, 'birth_date', true );
$birth_date = date("d-m", strtotime( $birth_date ) );
$today_date = date("d-m");
if ( $email != '' && $today_date == $birth_date) {
$to = $email;
$subject = 'Happy Birthday';
$body = 'Hello '.$user->first_name;
if( wp_mail( $to, $subject, $body ) ){
echo "sent";
}
}
}
}
add_action( 'init', 'sent_birthday_mail', 10, 1 );
此外,您可以使用 WP wp_mail_failed
操作挂钩检查 wp_mail 错误。
// show wp_mail() errors
add_action( 'wp_mail_failed', 'onWPMailError', 10, 1 );
function onWPMailError( $wp_error ) {
echo "<pre>"; print_r($wp_error); echo "</pre>";
}
我正在尝试使用 wordpress 的 PHP wp_mail() 功能向注册用户发送生日邮件。但是,我无法发送电子邮件。请输入以下代码:
# Get user ID
$id = get_current_user_id();
global $wpdb;
//query to extract birthdate from database
$result = $wpdb->get_results("SELECT um.meta_value FROM `wp_usermeta` AS um WHERE um.user_id = $id AND um.meta_key = 'birth_date'");
//query to extract email from database
$query= $wpdb->get_results("SELECT u.user_email FROM `wp_users` AS u WHERE u.ID = $id");
//load data from database and store it in user_name variable
foreach ($query as $row1) {
$user_name = $row1->user_email;
}
//birth_date
$newDate = date("d-m", strtotime($birth_date ));
//echo "birth date format is: ".$newDate;
//present date
//$now = time();
//$newNow= date("d-m", strtotime($now));
$newNow = date("d-m");
//echo "Today date format is: ".$newNow;
if ($newDate == $newNow) {
$to = $user_name;
$subject = 'Happy Birthday';
$body = 'Hello Gaurav';
wp_mail( $to, $subject,$body );
}
?>
在上面的代码中,我尝试将 $to=$user_name
放在 $to 部分,它不会向注册用户发送电子邮件。但是,如果我手动输入整个电子邮件地址,即 $to= "xyz@gmail.com"
,xyz 用户会成功收到电子邮件。
我修改了你的代码。你应该检查 email
是否为空。试试下面的代码。
对于电子邮件,您可以使用 get_user_by()
$user = get_user_by( 'id', get_current_user_id() );
要检索用户元数据,您可以使用 get_user_meta()
函数。
$birth_date = get_user_meta( $user->ID, 'birth_date', true );
function sent_birthday_mail(){
$user = get_user_by( 'id', get_current_user_id() );
if ( $user ) {
$email = $user->user_email;
$birth_date = get_user_meta( $user->ID, 'birth_date', true );
$birth_date = date("d-m", strtotime( $birth_date ) );
$today_date = date("d-m");
if ( $email != '' && $today_date == $birth_date) {
$to = $email;
$subject = 'Happy Birthday';
$body = 'Hello '.$user->first_name;
if( wp_mail( $to, $subject, $body ) ){
echo "sent";
}
}
}
}
add_action( 'init', 'sent_birthday_mail', 10, 1 );
此外,您可以使用 WP wp_mail_failed
操作挂钩检查 wp_mail 错误。
// show wp_mail() errors
add_action( 'wp_mail_failed', 'onWPMailError', 10, 1 );
function onWPMailError( $wp_error ) {
echo "<pre>"; print_r($wp_error); echo "</pre>";
}