【问题标题】:WordPress send mail with uploaded attachmentWordPress 发送带有上传附件的邮件
【发布时间】:2017-08-28 10:43:04
【问题描述】:

我有一个带有文件上传字段(CV 上传)的联系表。

我可以让邮件和消息发送没有问题,但是,我从网上使用的示例从表单中附加简历并不起作用。

这里有什么我遗漏的吗?

<input type="text" name="fullName" placeholder="Full Name: (required)" required>
<input type="email" name="email" placeholder="Email: (required)" required>
<input type="tel" name="tel" placeholder="Telephone: (required)" required>
<textarea name="message" placeholder="Quick message"></textarea>
<span>Please upload a copy of your cv</span><span><input type="file" name="cv" required></span>


//Handle the file upload and attachment
if ( ! function_exists( 'wp_handle_upload' ) ) {
  require_once( ABSPATH . 'wp-admin/includes/file.php' );
}
$uploadedfile = $_FILES['cv'];
$upload_overrides = array( 'test_form' => false );
$movefile = wp_handle_upload( $uploadedfile, $upload_overrides );
if ( $movefile && ! isset( $movefile['error'] ) ) { 
  $movefile['url'];
}
$attachments = array($movefile['file'] );

$mailoffice = wp_mail('james1@knoppysdev.com', 'New Candidate Application', $messageOffice, $headers, $attachments );

【问题讨论】:

  • 定义“不工作”
  • 邮件发送所有邮件内容和表单中的发布值。但是文件附件的 PHP 不包含附件。
  • 很确定 wp_mail 只需要一个文件名数组...而 wp_handle_upload 返回的内容有所不同。

标签: php wordpress phpmailer email-attachments


【解决方案1】:

好的,所以我通过走了很长的路来完成这项工作。

首先,我将文件作为输出表单的页面的附件上传到站点。

然后我使用以下内容获取附件路径,将其作为附件添加到 wp_mail() 中,最后在发送邮件后删除附件。

//Handle the CV Upload
require_once( ABSPATH . 'wp-admin/includes/image.php' );
require_once( ABSPATH . 'wp-admin/includes/file.php' );
require_once( ABSPATH . 'wp-admin/includes/media.php' );         

$attachment_id = media_handle_upload( 'cv', $_POST['post_id'] );
$attachments = get_attached_file( $attachment_id );

$mailoffice = wp_mail('james1@knoppysdev.com', 'New Candidate Application', $messageOffice, $headers, array($attachments) );
wp_delete_attachment( $attachment_id, true );

它很质朴,但很有效。

【讨论】:

    猜你喜欢
    • 2017-04-26
    • 1970-01-01
    • 2017-12-22
    • 2017-09-13
    • 1970-01-01
    • 2017-07-31
    • 1970-01-01
    • 2012-09-28
    相关资源
    最近更新 更多