电子邮件附件不工作wordpress [作业管理器插件修改]?

时间:2014-04-10 09:22:45

标签: php wordpress

我想对JobManager Wordpress插件进行一些修改。目前,JobManager没有发送带附件的电子邮件的功能。只有带文字的简单电子邮件。

我对代码做了几处修改。使用wordpress wp_mail()函数。但它仍无效。

这是代码。

    function jobman_application_mailout() {
    global $wpdb, $current_user;
    $options = get_option( 'jobman_options' );
    get_currentuserinfo();

    $fromid = $options['application_email_from'];

    $apps = get_posts( array( 'post_type' => 'jobman_app', 'post__in' => $_REQUEST['application'], 'numberposts' => -1, 'post_status' => 'publish,private' ) );

    $emails = array();
    $appids = array();
    foreach( $apps as $app ) {
        $email = get_post_meta( $app->ID, "data$fromid", true );
        if( empty( $email ) )
            // No email for this application
            continue;

        $emails[] = $email;
        $appids[] = $app->ID;
    }
    $email_str = implode( ', ', array_unique( $emails ) );
?>
    <div class="wrap">
        <h2><?php _e( 'Job Manager: Application Email', 'jobman' ) ?></h2>

        <form action="" method="post">
        <input type="hidden" name="jobman-mailout-send" value="1" />
        <input type="hidden" name="jobman-appids" value="<?php echo implode(',', $appids ) ?>" />
<?php
    wp_nonce_field( 'jobman-mailout-send' );
?>
        <table id="jobman-email-edit" class="form-table">
            <tr>
                <th scope="row"><?php _e( 'From', 'jobman' ) ?></th>
                <td><input class="regular-text code" type="text" name="jobman-from" value="<?php echo '&quot;' . $current_user->display_name . '&quot; <' . $current_user->user_email . '>' ?>" /></td>
            </tr>
            <tr>
                <th scope="row"><?php _e( 'To', 'jobman' ) ?></th>
                <td><?php echo $email_str ?></td>
            </tr>
            <tr>
                <th scope="row"><?php _e( 'Subject', 'jobman' ) ?></th>
                <td><input class="regular-text code" type="text" name="jobman-subject" /></td>
            </tr>
            <tr>
                <th scope="row"><?php _e( 'Message', 'jobman' ) ?></th>
                <td><textarea class="large-text code" name="jobman-message" rows="15"></textarea></td>
            </tr>
            <tr>
                <th scope="row"><?php _e( 'Upload Offer Letter', 'jobman' ) ?></th>
                <td><input type="file" name="attachment" rows="15"></td>
            </tr>
        </table>

        <p class="submit"><input type="submit" name="submit"  class="button-primary" value="<?php _e( 'Send Email', 'jobman' ) ?>" /></p>
        </form>
    </div>
<?php
}

function jobman_application_mailout_send() {
    global $current_user;
    get_currentuserinfo();

    $options = get_option( 'jobman_options' );

    $fromid = $options['application_email_from'];

    $from = $_REQUEST['jobman-from'];
    $subject = $_REQUEST['jobman-subject'];
    $message = $_REQUEST['jobman-message'];
    $attachments = '';
if (!empty($_FILES['attachment']['tmp_name'])) {
    $path = $_FILES['attachment']['name'];
    if (copy($_FILES['attachment']['tmp_name'], $path)) $attachments = $path;
}
}

    $header = "From: $from" . PHP_EOL;
    $header .= "Reply-To: $from" . PHP_EOL;
    $header .= "Return-Path: $from" . PHP_EOL;
    $header .= 'Content-type: text/plain; charset='. get_option( 'blog_charset' ) . PHP_EOL;

    $page = array(
                'comment_status' => 'closed',
                'ping_status' => 'closed',
                'post_status' => 'private',
                'post_author' => $current_user->ID,
                'post_content' => $message,
                'post_title' => $subject,
                'post_type' => 'jobman_email',
                'post_parent' => $options['main_page']
            );
    $emailid = wp_insert_post( $page );

    $appids = explode(',', $_REQUEST['jobman-appids'] );
    $emails = array();
    foreach( $appids as $appid ) {
        $appmeta = get_post_custom( $appid );
        if( ! array_key_exists("data$fromid", $appmeta ) || '' == $appmeta["data$fromid"] )
            // No email for this application
            continue;

        if( is_array( $appmeta["data$fromid"] ) )
            $emails[] = $appmeta["data$fromid"][0];
        else
            $emails[] = $appmeta["data$fromid"];

        add_post_meta( $appid, 'contactmail', $emailid, false );
    }

    $emails = array_unique( $emails );

    foreach( $emails as $to ) {
        wp_mail( $to, $subject, $message, $header, $attachments );
    }


?>

希望你能提供帮助。

1 个答案:

答案 0 :(得分:0)

您可以尝试以下代码。我希望它会有所帮助:

function jobman_email_application( $appid, $sendto = '' ) {
    $options = get_option( 'jobman_options' );

    $app = get_post( $appid );
    if( NULL == $app )
            return;

    $parent = get_post( $app->ancestors[0] );
    $job_email = '';

    $jobs = get_post_meta( $app->ID, 'job', false );
    if( ! empty( $jobs ) ) {
            $job_emails = array();
            foreach( $jobs as $job ) {
                    $je = get_post_meta( $job, 'email', true );
                    if( ! empty( $je ) && ! in_array( $je, $job_emails ) )
                            $job_emails[] = $je;
            }
            $job_email = implode( ',', $job_emails );
    }

    $appmeta = get_post_custom( $appid );

    $appdata = array();
    foreach( $appmeta as $key => $value ) {
            if( is_array( $value ) )
                    $appdata[$key] = $value[0];
            else
                    $appdata[$key] = $value;
    }

    $categories = wp_get_object_terms( $appid, 'jobman_category' );

    $to = '';
    if( '' != $sendto ) {
        $to = $sendto;
    }
    else if( '' != $job_email ) {
        $to = $job_email;
    }
    else if( count( $categories ) > 0 ) {
            $ii = 1;
            foreach( $categories as $cat ) {
                    $to .= $cat->description;
                    if( $ii < count( $categories ) )
                            $to .= ', ';
            }
    }

    if( '' == $to )
            $to = $options['default_email'];

    if( '' == $to )
            return;

    $fromid = $options['application_email_from'];
    $from = '';

    if('' == $fromid )
            $from = $options['default_email'];
    else if( array_key_exists( "data$fromid", $appdata ) )
            $from = $appdata["data$fromid"];

    if( '' == $from )
            $from = get_option( 'admin_email' );

    $fids = $options['application_email_from_fields'];

    $fromname = '';
    if( count( $fids ) > 0 ) {
            foreach( $fids as $fid ) {
                    if( array_key_exists( "data$fid", $appdata ) && '' != $appdata["data$fid"] )
                            $fromname .= $appdata["data$fid"] . ' ';
            }
    }
    $fromname = trim( $fromname );

    $from = "\"$fromname\" <$from>";

    $subject = $options['application_email_subject_text'];
    if( ! empty( $subject ) )
            $subject .= ' ';

    $fids = $options['application_email_subject_fields'];

    if( count( $fids ) > 0 ) {
            foreach( $fids as $fid ) {
                    if( array_key_exists( "data$fid", $appdata ) && '' != $appdata["data$fid"] )
                            $subject .= $appdata["data$fid"] . ' ';
            }
    }

    trim( $subject );

    if( empty( $subject ) )
            $subject = __( 'Job Application', 'jobman' );

    $random_hash = md5(date('r', time()));

    $mime_boundary = "==Multipart_Boundary_x{$random_hash}x";

    $msg = '--'.$mime_boundary.PHP_EOL;
    $msg .= 'Content-Type: text/plain; charset="iso-8859-1"'.PHP_EOL;
    $msg .= 'Content-Transfer-Encoding: 7bit'.PHP_EOL.PHP_EOL;

    $msg .= __( 'Application Link', 'jobman' ) . ': ' . admin_url( 'admin.php?page=jobman-list-applications&appid=' . $app->ID ) . PHP_EOL;

    $parents = get_post_meta( $app->ID, 'job', false );
    if( ! empty( $parents ) ) {
            $msg .= PHP_EOL;
            foreach( $parents as $parent ) {
                    $data = get_post( $parent );
                    $msg .= __( 'Job', 'jobman' ) . ': ' . $data->ID . ' - ' . $data->post_title . PHP_EOL;
                    $msg .= get_page_link( $data->ID ) . PHP_EOL;
            }
            $msg .= PHP_EOL;
    }

    $msg .= __( 'Timestamp', 'jobman' ) . ': ' . $app->post_date . PHP_EOL . PHP_EOL;

    $fields = $options['fields'];

    $files = array();

    if( count( $fields ) > 0 ) {
            uasort( $fields, 'jobman_sort_fields' );
            foreach( $fields as $id => $field ) {
                    // Don't include the field if it has no data
                    if( ! array_key_exists("data$id", $appdata ) || '' == $appdata["data$id"] )
                            continue;

                    // Don't include the field if it has been blocked
                    if( $field['emailblock'] )
                            continue;

                    switch( $field['type'] ) {
                            case 'text':
                            case 'radio':
                            case 'checkbox':
                            case 'date':
                            case 'select':
                                    $msg .= $field['label'] . ': ' . $appdata['data'.$id] . PHP_EOL;
                                    break;
                            case 'textarea':
                                    $msg .= $field['label'] . ':' . PHP_EOL . $appdata['data'.$id] . PHP_EOL;
                                    break;
                            case 'file':
                                    $files[] = $id;
                                    $msg .= $field['label'] . ': ' . wp_get_attachment_url( $appdata["data$id"] ) . PHP_EOL;
                                    break;
                            case 'geoloc':
                                    $msg .= $field['label'] . ': ' . $appdata['data-display'.$id] . ' (' . $appdata['data'.$id] . ')' . PHP_EOL;
                                    $msg .= 'http://maps.google.com/maps?q=' . urlencode( $appdata['data'.$id] ) . PHP_EOL;
                                    break;
                    }
            }
    }

    $msg .= PHP_EOL.'--'.$mime_boundary.PHP_EOL;

    foreach ($files as $file)
    {
            $file = get_post($appdata["data$file"]);
            $path = get_attached_file($file->ID);
            $attachment = chunk_split(base64_encode(file_get_contents($path)));

            $msg .= 'Content-Type: {"application/octet-stream"}; name="'.basename($file->guid).'"'.PHP_EOL;
            $msg .= 'Content-Disposition: attachment; filename="'.basename($file->guid).'"'.PHP_EOL;
            $msg .= 'Content-Transfer-Encoding: base64'.PHP_EOL.PHP_EOL;
            $msg .= $attachment.PHP_EOL.PHP_EOL;
            $msg .= '--'.$mime_boundary.PHP_EOL;
    }

    $header = "From: $from" . PHP_EOL;
    $header .= "Reply-To: $from" . PHP_EOL;
    $header .= "Return-Path: $from" . PHP_EOL;
    $header .= 'MIME-Version: 1.0'.PHP_EOL;
    $header .= 'Content-type: multipart/mixed; boundary="'.$mime_boundary.'"'.PHP_EOL;

    wp_mail( $to, $subject, $msg, $header );
}