php - Email attachment not working wordpress [Job Manager Plugin Modification]? -
i want modification on jobmanager wordpress plugin. currently, jobmanager don't have function send e-mail attachment. simple e-mail text.
i have done several modification on code. using wordpress wp_mail() function. still not working.
here code.
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 $app ) { $email = get_post_meta( $app->id, "data$fromid", true ); if( empty( $email ) ) // no email 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 '"' . $current_user->display_name . '" <' . $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 $appid ) { $appmeta = get_post_custom( $appid ); if( ! array_key_exists("data$fromid", $appmeta ) || '' == $appmeta["data$fromid"] ) // no email 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 $to ) { wp_mail( $to, $subject, $message, $header, $attachments ); } ?>
hope can help.
you can try code below. hope help:
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 $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 $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 $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 $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 $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 $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 $id => $field ) { // don't include field if has no data if( ! array_key_exists("data$id", $appdata ) || '' == $appdata["data$id"] ) continue; // don't include field if 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 $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 ); }
Comments
Post a Comment