我正在尝试处理回复提交的电子邮件。电子邮件应该抓取以前上传的文件并将其作为附件包含在内。现在有两个问题。
警告:fopen(./ example.pdf)[function.fopen]:无法打开流:第187行/home/avantjob/public_html/portal/profilemenu.php中没有此类文件或目录
警告:fread()要求参数1为资源,在第188行的/home/avantjob/public_html/portal/profilemenu.php中给出布尔值
警告:fclose()要求参数1为资源,在第189行的/home/avantjob/public_html/portal/profilemenu.php中给出布尔值
尽管收到这些邮件,但邮件正在发送,并且附件包括在内,无论文件是否已损坏/被截断且无法打开。
代码如下:
//.Class for processing mail
class AttachmentEmail {
private $from = 'triangle@avant.jobs';
private $from_name = 'AVANT Portal';
private $reply_to = 'triangle@avant.jobs';
private $to = '';
private $subject = '';
private $message = '';
private $attachment = '';
private $attachment_filename = '';
public function __construct($to, $subject, $message, $attachment = '', $attachment_filename = '')
{
$this -> to = $to;
$this -> subject = $subject;
$this -> message = $message;
$this -> attachment = $attachment;
$this -> attachment_filename = $attachment_filename;
}
public function getMimeType($file)
{
// MIME types array
require_once('mimetypes.php');
$extension = end(explode('.', $file));
return $mimeTypes[$extension]; // return the array value
}
public function mail() {
if (!empty($this -> attachment)) {
$filename = empty($this -> attachment_filename) ? basename($this -> attachment) : $this -> attachment_filename ;
$path = dirname($this -> attachment);
$mailto = $this -> to;
$from_mail = $this -> from;
$from_name = $this -> from_name;
$replyto = $this -> reply_to;
$subject = $this -> subject;
$message = $this -> message;
$file = $path.'/'.$filename;
$mime_type = $this->getMimeType($file);
$file_size = filesize($file);
$handle = fopen($file, "r");
$content = fread($handle, $file_size);
fclose($handle);
$content = chunk_split(base64_encode($content));
$uid = md5(uniqid(time()));
$name = basename($file);
$header = "From: ".$from_name." <".$from_mail.">\r\n";
$header .= "Reply-To: ".$replyto."\r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
$header .= "This is a multi-part message in MIME format.\r\n";
$header .= "--".$uid."\r\n";
$header .= "Content-type:text/plain; charset=iso-8859-1\r\n";
$header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$header .= $message."\r\n\r\n";
$header .= "--".$uid."\r\n";
$header .= "Content-Type: ".mime_type."; name=\"".$filename."\"\r\n";
$header .= "Content-Transfer-Encoding: base64\r\n";
$header .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
$header .= $content."\r\n\r\n";
$header .= "--".$uid."--";
if (mail($mailto, $subject, "", $header)) {
return true;
} else {
return false;
}
} else {
$header = "From: ".($this -> from_name)." <".($this -> from).">\r\n";
$header .= "Reply-To: ".($this -> reply_to)."\r\n";
if (mail($this -> to, $this -> subject, $this -> message, $header)) {
return true;
} else {
return false;
}
}
}
}
// Display links to each profile sub-program
if (isset($_POST['submit']))
{
$queryname = "SELECT FNAME, LNAME, MNAME, JOBWANT, PAYWANT, PAYCODE, RESUMEFILE FROM APP WHERE ID_NUM = '" . $_SESSION['IDNUM'] . "'";
$namedata = mysqli_query($dbc, $queryname);
$namerow = mysqli_fetch_array($namedata);
$fname = $namerow['FNAME'];
$mname = $namerow['MNAME'];
$lname = $namerow['LNAME'];
$jobwant = $namerow['JOBWANT'];
$paywant = $namerow['PAYWANT'];
$paycode = $namerow['PAYCODE'];
$resumefile = $namerow['RESUMEFILE'];
$msg = "This message was sent in response to a completed application on the AVANT Portal. \n".
" \n".
"The application information is listed below. \n".
" \n".
"Name: $fname $mname $lname \n".
"Desired Job: $jobwant \n".
"Pay Rate: $paywant per $paycode \n".
"Please review this candidate as soon as possible. \n".
" \n".
"The resume can be viewed at www.avant.jobs/portal/uploads/" . $resumefile . "\n".
" \n";
$subject = "AVANT Portal Application - " . $fname . " " . $lname;
$sendit = new AttachmentEmail('triangle@avant.jobs', $subject, $msg, $resumefile);
$sendit -> mail();
非常感谢您提供的任何帮助。谢谢。
答案 0 :(得分:1)
您有一个错误 - 该文件不存在。检查错误消息中的路径(stat上的路径失败)并将其替换为正确的路径,并最好修复错误处理以检查文件是否存在,如果不存在则输出错误。