0

我正在尝试处理电子邮件以响应提交。电子邮件应获取先前上传的文件并将其作为附件包含在内。现在出现了两个问题。

  1. 当进程运行以发送消息时,我在页面上收到错误消息。错误是:警告:filesize() [function.filesize]: stat failed for ./example.pdf in /home/avantjob/public_html/portal/profilemenu.php 在第 186 行

警告: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 是资源,布尔值在 /home/avantjob/public_html/portal/profilemenu.php 第 189 行给出

尽管收到了这些消息,但电子邮件仍在发送,并且包含附件,无论文件损坏/截断且不会打开。

代码如下:

//.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();

您能提供的任何帮助将不胜感激。谢谢你。

4

1 回答 1

1

您有一个错误 - 文件不存在。检查错误消息中的路径(带有 stat 的路径失败)并将其替换为正确的路径,并且最好修复错误处理以检查文件是否存在,如果不存在则出错。

于 2013-03-12T17:02:47.370 回答