我正在尝试制作联系表格,但 wp_mail() 不起作用。我没有收到任何消息。我正在使用 XAMPP localhost,我的代码是:
$name = sanitize_text_field($_POST['yourname']);
$email = sanitize_email($_POST['email']);
$subject = sanitize_text_field($_POST['subject']);
$message = sanitize_text_field($_POST['message']);
if ( isset( $_POST['submit']) ) {
//check for empty fields
if ( empty( $name ) || empty( $email ) || empty( $subject ) || empty( $message ) ) {
echo sprintf( '<h5 class="form_erros">%s</h5>', __('Please Fill All Fields!', 'promag') );
}else {
// check if input characters are valid
if ( !preg_match('/[a-zA-Z_x7f-xff][a-zA-Z0-9_x7f-xff]*/', $name ) ) {
echo sprintf( '<h5 class="form_erros">%s</h5>', __('Please Enter Valid Name!', 'promag') );
}else {
// check if email is valid
if ( !filter_var( $email, FILTER_VALIDATE_EMAIL )) {
echo sprintf( '<h5 class="form_erros">%s</h5>', __('Please Enter Valid E-Mail!', 'promag') );
}else {
// sending the message
$to = get_option('admin_email');
$headers = "From:" . get_option("blogname") . $email . "\r\n";
wp_mail( $to, $subject, $message, $headers, array( '' ) );
echo sprintf( '<h5 class="form_success">%s</h5>', __('Mail Successfully Sent!', 'promag') );
}
}
}
}//万一