WordPress前端发布表单和电子邮件之后

时间:2013-05-08 作者:Karl

我已经编写了一个前端表单,可以发布到自定义帖子类型,我有一个用于电子邮件的表单输入字段,当表单发布到自定义帖子类型时,我希望在提交时将电子邮件发送到插入电子邮件输入的电子邮件地址。

有什么想法吗??

编辑这是我的代码(忘记了lol)

if(\'POST\' == $_SERVER[\'REQUEST_METHOD\'] && !empty( $_POST[\'action\'] ) &&  $_POST[\'action\'] == "new_booking") {

if(isset($_POST[\'submit\'])) {

    $sucess = "We will contact you regarding this booking.";

    $errors = "";

    if(!empty($_POST[\'booking_name\'])) {
        $booking_name = trim($_POST[\'booking_name\']);
    } else {
        $errors .= "<li>Please enter your name.</li>";
    }

    if(!empty($_POST[\'booking_email\'])) {
        $booking_email = trim($_POST[\'booking_email\']);
    } else {
        $errors .= "<li>Please enter your email.</li>";  
    }

    if(!empty($_POST[\'booking_address\'])) {
        $booking_address = trim($_POST[\'booking_address\']);
    } else {
        $errors .= "<li>Please enter your address.</li>";
    }

    if(!empty($_POST[\'booking_phone\'])) {
        $booking_phone = trim($_POST[\'booking_phone\']);
    } else {
        $errors .= "<li>Please enter your phone number.</li>";
    }

    if(!empty($_POST[\'booking_rooms\'])) {
        $booking_rooms = trim($_POST[\'booking_rooms\']);
    } else {
        $errors .= "<li>Please enter number of rooms.</li>";
    }

    if(!empty($_POST[\'booking_adults\'])) {
        $booking_adults = $_POST[\'booking_adults\'];
    } else {
        $errors .= "<li>Please enter number of adults.</li>";
    }

    if(!empty($_POST[\'booking_arrival\'])) {
        $booking_arrival = trim($_POST[\'booking_arrival\']);
    } else {
        $errors .= "<li>Please enter an arrival date.</li>";  
    }

    if(!empty($_POST[\'booking_departure\'])) {
        $booking_departure = trim($_POST[\'booking_departure\']);
    } else {
        $errors .= "<li>Please enter a departure date.</li>";  
    }

    $booking_requirements = trim($_POST[\'booking_requirements\']);

    if(empty($errors)) {
        $new_booking = array(
            \'post_title\' => $booking_name,
            \'post_status\' => \'publish\',
            \'post_type\' => \'listings\',
            \'booking_email\' => $booking_email,
            \'booking_address\' => $booking_address,
            \'booking_phone\' => $booking_phone,
            \'booking_rooms\' => $booking_rooms,
            \'booking_adults\' => $booking_adults,
            \'booking_arrival\' => $booking_arrival,
            \'booking_departure\' => $booking_departure,
            \'booking_requirements\' => $booking_requirements
        );

        $pid = wp_insert_post($new_booking);

        add_post_meta($pid, \'booking_email\', $booking_email, true);
        add_post_meta($pid, \'booking_address\', $booking_address, true);
        add_post_meta($pid, \'booking_phone\', $booking_phone, true);
        add_post_meta($pid, \'booking_rooms\', $booking_rooms, true);
        add_post_meta($pid, \'booking_adults\', $booking_adults, true);
        add_post_meta($pid, \'booking_arrival\', $booking_arrival, true);
        add_post_meta($pid, \'booking_departure\', $booking_departure, true);
        add_post_meta($pid, \'booking_requirements\', $booking_requirements, true);

    }

}
}

do\\u action(“wp\\u insert\\u post”,“wp\\u insert\\u post”);

1 个回复
SO网友:Santoro

将此置于if empty$错误后,并进行编辑以满足您的需要:

if(empty($errors)) {
$subject = __(\'[Your Subject]\', \'your_text_domain\').\' \'.$title;
$body = __("Name: ","your_text_domain").$booking_name;
$body .= __("\\n\\nEmail: ","your_text_domain").$booking_email;
$body .= __("\\n\\nPhone: ","your_text_domain").$booking_phone;
$body .= __("\\n\\nMessage: ","your_text_domain").$message;

$headers = __(\'From: [email protected] \', \'your_text_domain\').\' <\'.$booking_email.\'>\' . "\\r\\n" . __(\'Reply-To: \',\'your_text_domain\') . $email; // Change this $email to whatever you want where you will get messages from booked persons

$mail_sent = false;
$mail_sent = mail($mailto, $subject, $body, $headers);

if ($mail_sent == true) {
    echo __(\'Your message has been sent succesfully.\',\'your_text_domain\');      
} else {
    $error = true;
    echo __(\'Sorry, an error occured while sending your message. Please try again.\',\'your_text_domain\');        
    }
}   

结束

相关推荐

Sending out scheduled emails

如果我有计划发送的电子邮件,wordpress会运行cron吗?似乎如果没有对站点进行访问(请求),计划的任务将不会运行,因此不会发送电子邮件,例如每小时发送一次。根据目前的情况,如果几个小时内没有访问该网站(并且进行了多次访问),则会同时发送所有预定的电子邮件如何防止这种情况发生,是否有添加cron任务的插件?