简体   繁体   English

在 PHP 中通过邮件发送附件

[英]Send attachment via mail in PHP

if(isset($input->csvrequest)){

  $mailsubject= $input->mailsubject;
  $mailbody= $input->mailbody;
  $mailto= $input->mailto;
  $to = $mailto; 
  $mail_subject = $mailsubject;
  $mail_message = $mailbody;

  $headers = "From: Test <support@test.com>\r\n";
  $headers .= "Reply-To: support@test.com\r\n";
  $headers .= "X-Mailer: PHP/" . phpversion() . "\r\n";
  $headers .= "X-Priority: 0\r\n";
  $headers .= "MIME-Version: 1.0\r\n";
  $headers .= "Content-type: text/html; charset=win-1251\r\n";

  wp_mail($to, $mail_subject, $mail_message, $headers);

  add_filter('wp_mail_content_type', create_function('', 'return "text/html"; '));
  $arr["error"] = "Email Sent";

}

So, this code is sending mail for me.所以,这段代码正在为我发送邮件。 Actually I want to send an attachment with this mail to the user.(For eg: file.csv)实际上我想用这封邮件向用户发送一个附件。(例如:file.csv)

Assuming you have uploaded the 'file.csv' in wordpress media library.假设您已在 wordpress 媒体库中上传了“file.csv”。 Now, setting attachment variable and passing to wp mail function and that's it.现在,设置附件变量并传递给 wp 邮件 function 就是这样。 Attachment has started to be sent.已开始发送附件。

    $mail_attachment = array(WP_CONTENT_DIR . '/uploads/2021/03/file.csv');
    wp_mail($to, $mail_subject, $mail_message, $headers, $mail_attachment);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM