如何在wp_mail函数中添加附件

时间:2014-05-12 13:08:30

标签: php wordpress

我在html&中添加了一个文件附件。现在我想用phpmail函数配置它。

我使用classipress主题,我尝试编辑和配置它。

我在

的html中添加了此文件
    <?php
/**
 * This is the sidebar contact form used on the single ad page
 *
 */

$msg = '';

// if contact form has been submitted, send the email
if ( isset( $_POST['submit'] ) && $_POST['send_email'] == 'yes' ) {

    $result = cp_contact_ad_owner_email( $post->ID );

    if ( $result->get_error_code() ) {
        $error_html = '';
        foreach ( $result->errors as $error )
            $error_html .= $error[0] . '<br />';
        $msg = '<p class="red center"><strong>' . $error_html . '</strong></p>';
    } else {
        $msg = '<p class="green center"><strong>' . __( 'Your message has been sent!', APP_TD ) . '</strong></p>';
    }

}

?>


<form name="mainform" id="mainform" class="form_contact" action="#priceblock2" method="post" enctype="multipart/form-data">

    <?php echo $msg; ?>

    <p class="contact_msg"><?php _e( 'To inquire about this ad listing, complete the form below to send a message to the ad poster.', APP_TD ); ?></p>

    <ol>
        <li>
            <label><?php _e( 'Name:', APP_TD ); ?></label>
            <input name="from_name" id="from_name" type="text" minlength="2" value="<?php if(isset($_POST['from_name'])) echo esc_attr( stripslashes($_POST['from_name']) ); ?>" class="text required" />
            <div class="clr"></div>
        </li>

        <li>
            <label><?php _e( 'Email:', APP_TD ); ?></label>
            <input name="from_email" id="from_email" type="text" minlength="5" value="<?php if(isset($_POST['from_email'])) echo esc_attr( stripslashes($_POST['from_email']) ); ?>" class="text required email" />
            <div class="clr"></div>
        </li>

        <li>
            <label><?php _e( 'Subject:', APP_TD ); ?></label>
            <input name="subject" id="subject" type="text" minlength="2" value="<?php _e( 'Re:', APP_TD ); ?> <?php the_title();?>" class="text required" />
            <div class="clr"></div>
        </li>

        <li>
            <label><?php _e( 'Message:', APP_TD ); ?></label>
            <textarea name="message" id="message" rows="" cols="" class="text required"><?php if(isset($_POST['message'])) echo esc_attr( stripslashes($_POST['message']) ); ?></textarea>
            <div class="clr"></div>
        </li>

        <li>
            <label><?php _e( 'Attach:', APP_TD ); ?></label>
            <input name="attachments" id="attachments" type="file" />
            <div class="clr"></div>
        </li>

        <li>
        <?php
            // create a random set of numbers for spam prevention
            $randomNum = '';
            $randomNum2 = '';
            $randomNumTotal = '';

            $rand_num = rand(0,9);
            $rand_num2 = rand(0,9);
            $randomNumTotal = $randomNum + $randomNum2;
        ?>
            <label><?php _e( 'Sum of', APP_TD ); ?> <?php echo $rand_num; ?> + <?php echo $rand_num2; ?> =</label>
            <input name="rand_total" id="rand_total" type="text" minlength="1" value="" class="text required number" />
            <div class="clr"></div>
        </li>

        <li>
            <input name="submit" type="submit" id="submit_inquiry" class="btn_orange" value="<?php _e( 'Send Inquiry', APP_TD ); ?>" />
        </li>

    </ol>

    <input type="hidden" name="rand_num" value="<?php echo $rand_num; ?>" />
    <input type="hidden" name="rand_num2" value="<?php echo $rand_num2; ?>" />
    <input type="hidden" name="send_email" value="yes" />

</form>

这是我的phpmail函数:

    function cp_contact_ad_owner_email( $post_id ) {
    $errors = new WP_Error();

    // check for required post data
    $expected = array( 'from_name', 'from_email', 'subject', 'message' );
    foreach ( $expected as $field_name ) {
        if ( empty( $_POST[ $field_name ] ) ) {
            $errors->add( 'empty_field', __( 'ERROR: All fields are required.', APP_TD ) );
            return $errors;
        }
    }

    // check for required anti-spam post data
    $expected_numbers = array( 'rand_total', 'rand_num', 'rand_num2' );
    foreach ( $expected_numbers as $field_name ) {
        if ( ! isset( $_POST[ $field_name ] ) || ! is_numeric( $_POST[ $field_name ] ) ) {
            $errors->add( 'invalid_captcha', __( 'ERROR: Incorrect captcha answer.', APP_TD ) );
            return $errors;
        }
    }

    // verify captcha answer
    $rand_post_total = (int) $_POST['rand_total'];
    $rand_total = (int) $_POST['rand_num'] + (int) $_POST['rand_num2'];
    if ( $rand_total != $rand_post_total )
        $errors->add( 'invalid_captcha', __( 'ERROR: Incorrect captcha answer.', APP_TD ) );

    // verify email
    if ( ! is_email( $_POST['from_email'] ) )
        $errors->add( 'invalid_email', __( 'ERROR: Incorrect email address.', APP_TD ) );

    // verify post
    $post = get_post( $post_id );
    if ( ! $post )
        $errors->add( 'invalid_post', __( 'ERROR: Ad does not exist.', APP_TD ) );

    if ( $errors->get_error_code() )
        return $errors;

    $mailto = get_the_author_meta( 'user_email', $post->post_author );

    $from_name = appthemes_filter( appthemes_clean( $_POST['from_name'] ) );
    $from_email = appthemes_clean( $_POST['from_email'] );
    $subject = appthemes_filter( appthemes_clean( $_POST['subject'] ) );
    $posted_message = appthemes_filter( appthemes_clean( $_POST['message'] ) );
    $attachments = appthemes_filter( appthemes_clean( $_POST['attachments'] ) );

    $sitename = wp_specialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES );
    $siteurl = home_url('/');
    $permalink = get_permalink( $post_id );


    $message = sprintf( __( 'Someone is interested in your ad listing: %s', APP_TD ), $permalink ) . "\r\n\r\n";
    $message .= '"' . wordwrap( $posted_message, 70 ) . '"' . "\r\n\r\n";
    $message .= sprintf( __( 'Name: %s', APP_TD ), $from_name ) . "\r\n";
    $message .= sprintf( __( 'E-mail: %s', APP_TD ), $from_email ) . "\r\n\r\n";
    $message .= '-----------------------------------------' . "\r\n";
    $message .= sprintf( __( 'This message was sent from %s', APP_TD ), $sitename ) . "\r\n";
    $message .=  $siteurl . "\r\n\r\n";
    $message .= __( 'Sent from IP Address: ', APP_TD ) . appthemes_get_ip() . "\r\n\r\n"; 

    $email = array( 'to' => $mailto, 'subject' => $subject, 'message' => $message, 'from' => $from_email, 'from_name' => $from_name,  'attachments' => $attachments );
    $email = apply_filters( 'cp_email_user_ad_contact', $email, $post_id );

    APP_Mail_From::apply_once( array( 'email' => $email['from'], 'name' => $email['from_name'], 'reply' => true ) );
    wp_mail( $email['to'], $email['subject'], $email['message'], $email['attachments'] );

    return $errors;
}

请帮帮我。我需要它。已经尝试过在线的10种方法:(但我失败了。

1 个答案:

答案 0 :(得分:0)

你为什么不这样做:

wp_mail( $to, $subject, $message, $headers, $attachments );

参考:http://codex.wordpress.org/Function_Reference/wp_mail

相关问题