【发布时间】:2018-05-04 05:18:46
【问题描述】:
我是 PHP 新手,我需要创建一个表单,要求用户输入多个字段并上传他/她的简历。当他/她提交表格时,他/她的提交应该通过电子邮件发送给我,并附上他/她的简历作为电子邮件的附件。我已经使用 PHP 使用 php 邮件功能发送电子邮件。一切正常,除了文件附件无法读取。请参阅随附的屏幕截图。
https://imgur.com/a/UnUOyDR
上传的文件也是odt格式。我需要用户上传所有类型的简历格式。
我将发布代码的基本部分。如有错误请指正
if ($_POST['submit_x']) {
$cand_name = trim($_POST['cand_name']);
$appl_email = $_POST['email'];
$target_dir = "/home/test/public_html/new/job/hr/Resume/";
$file = $_FILES['my_file']['name']; // Resume-Test.odt
$path = pathinfo($file);
$ext = $path['extension']; // odt
$temp_name = $_FILES['my_file']['tmp_name']; // /tmp/phpqkLeuL
$path_filename_ext = $target_dir.$file.".".$ext;
move_uploaded_file($temp_name,$path_filename_ext);
$mailto = 'robert.k1254@gmail.com';
$subject = 'Subject';
$message = 'My message';
$content = file_get_contents($path_filename_ext);
$content = chunk_split(base64_encode($content));
// a random hash will be necessary to send mixed content
$separator = md5(time());
// carriage return type (RFC)
$eol = "\r\n";
// main header (multipart mandatory)
$headers = "From: name <test@test.com>" . $eol;
$headers .= "MIME-Version: 1.0" . $eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"" . $separator . "\"" . $eol;
$headers .= "Content-Transfer-Encoding: 7bit" . $eol;
$headers .= "This is a MIME encoded message." . $eol;
// message
$body = "--" . $separator . $eol;
$body .= "Content-Type: text/plain; charset=\"iso-8859-1\"" . $eol;
$body .= "Content-Transfer-Encoding: 8bit" . $eol;
$body .= $message . $eol;
// attachment
$body .= "--" . $separator . $eol;
$body .= "Content-Type: application/octet-stream; name=\"" . $file . "\"" . $eol;
$body .= "Content-Transfer-Encoding: base64" . $eol;
$body .= "Content-Disposition: attachment" . $eol;
$body .= $content . $eol;
$body .= "--" . $separator . "--";
//SEND Mail
if (mail($mailto, $subject, $body, $headers)) {
echo "mail send ... OK"; // or use booleans here
} else {
echo "mail send ... ERROR!";
print_r( error_get_last() );
}
}
<input type="file" name="my_file" /><br /><br />
【问题讨论】:
-
我会建议你使用一些久经考验的邮件库(如 PHPMailer 或 SwiftMailer),而不是 PHP 的低级
mail()-function。它们更更容易使用。 -
@MagnusEriksson 谢谢你的建议,但你能告诉我哪里错了吗?
标签: php forms sendmail email-attachments