电子邮件类正在创建一个额外的附件

时间:2018-07-01 04:50:28

标签: php html email-attachments

我用电子邮件的名称创建了一个PHP类,以便以HTML文本格式发送带有附件的电子邮件。该代码正在创建一个名为“ Part_3.txt”的附加附件文件。请让我知道我所忽略的事情。

首先构造该类,然后调用sendMail函数。

    <?php

class eMail {
    private $eol;
    private $boundary;
    private $headers;
    private $body;
    private $from;
    private $to;
    private $cc;
    private $subject;
    private $content;
    private $pathNfile;

    public function __construct($from, $to, $cc, $subject, $content, $pathNfile) {
        $this->eol = PHP_EOL;
        $this->boundary = md5(rand());
        $this->headers = "";
        $this->body = "";
        $this->from = $from;
        $this->to = $to;
        $this->cc = $cc;
        $this->subject = $subject;
        $this->content = $content;
        $this->pathNfile = $pathNfile;
    }

    public function __destruct() {
    }

    public function sendMail() {

        $this->prepareHeader();

        $this->body = $this->eol . "--" . $this->boundary . $this->eol;
        $this->body .= 'Content-Type: text/html; charset=ISO-8859-1' . 
                $this->eol;
        $this->body .= 'Content-Transfer-Encoding: quoted-printable' . 
                $this->eol;
        $this->body .= $this->eol . '<div>' . $this->content . '</div>' . 
                $this->eol;

        if ($this->pathNfile != '' && file_exists($this->pathNfile)) {
            $this->prepareAttachment();
        }

        $this->body .= $this->eol . '--' . $this->boundary . '--' . $this->eol;

        if(mail($this->to, $this->subject, $this->body, $this->headers)) {
            return true;
        } else {
            return false;
        }
    }

    public function prepareHeader() {
        $this->headers = 'From: ' . $this->from . $this->eol;
        $this->headers .= 'Mime-Version: 1.0' . $this->eol;
        $this->headers .= 'Content-Type: multipart/related;boundary=' . 
                $this->boundary . $this->eol;

        //adresses cc and ci
        if ($this->cc != '') {
            $this->headers .= 'Cc: ' . $this->cc . $this->eol;
        }
        $this->headers .= 'boundary = ' . $this->boundary . $this->eol;
    }

    public function prepareAttachment() {
        $file_size = filesize($this->pathNfile);
        $ftype = filetype($this->pathNfile);
        $handle = fopen($this->pathNfile, "r");
        $fileContent = fread($handle, $file_size);
        fclose($handle);

        $this->body .= $this->eol . '--' . $this->boundary . $this->eol;
        $this->body .= 'Content-Type: \'' . $ftype . '\'; name="' . 
                basename($this->pathNfile) . '"' . $this->eol;
        $this->body .= 'Content-ID: <' . basename($this->pathNfile) . '>' . 
                $this->eol;
        $this->body .= 'X-Attachment-Id: ' . rand(1000, 99999) . $this->eol;
        $this->body .= $this->eol . $fileContent . $this->eol;
        $this->body .= $this->eol . '--' . $this->boundary . $this->eol;
    }
}
?>

带有HTML的PHP​​文件。

...       try{
            $em = new eMail($from, $to, $cc, $subject, $message, $pathNfile);
            if($em->sendMail()) {
                $emailSendMessage = 'eMail was sent successfully.';
            } else {
                $emailSendMessage = 'eMail sending failed.';
            }
        } catch (Exception $ex) {
           echo $ex->getMessage();
        }
     ?>

    <!DOCTYPE html>
        <main>
        <h2>Thank you for sending the eMail</h2>
        <h4><span><?php echo $emailSendMessage; ?></span></h4>
        </main>

1 个答案:

答案 0 :(得分:0)

这是新的preparAttachment()函数,现在可以使用。

    public function prepareAttachment() {
    $file_size = filesize($this->pathNfile);
    $ftype = filetype($this->pathNfile);
    $handle = fopen($this->pathNfile, "r");
    $attachment = fread($handle, $file_size);
    fclose($handle);

    $this->body .= $this->eol . '--' . $this->boundary . $this->eol;
    $this->body .= 'Content-Type: \'' . $ftype . '\'; name="' . basename($this->pathNfile) . '"' . $this->eol;
    $this->body .= 'Content-ID: <' . basename($this->pathNfile) . '>' . $this->eol;
    $this->body .= 'X-Attachment-Id: ' . rand(1000, 99999) . $this->eol;
    $this->body .= 'Content-Disposition: attachment' . $this->eol;
    $this->body .= $this->eol . $attachment . $this->eol;
}