Php 电子邮件类正在创建额外的附件
我已经创建了一个名为eMail的PHP类来发送带有附件的HTML文本格式的电子邮件。代码正在创建一个名为“Part_3.txt”的额外附件文件。请让我知道我忽略了什么 首先构造类,然后调用sendMail函数Php 电子邮件类正在创建额外的附件,php,html,email-attachments,Php,Html,Email Attachments,我已经创建了一个名为eMail的PHP类来发送带有附件的HTML文本格式的电子邮件。代码正在创建一个名为“Part_3.txt”的额外附件文件。请让我知道我忽略了什么 首先构造类,然后调用sendMail函数 <?php class eMail { private $eol; private $boundary; private $headers; private $body; private $from; private $to;
<?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>
。。。试一试{
$em=新电子邮件($from,$to,$cc,$subject,$message,$pathNfile);
如果($em->sendMail()){
$emailSendMessage='电子邮件已成功发送';
}否则{
$emailSendMessage='电子邮件发送失败';
}
}捕获(例外$ex){
echo$ex->getMessage();
}
?>
谢谢你发邮件
这是新的prepareAttachment()函数,现在可以使用了
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;
}
公共功能准备附件(){
$file\u size=filesize($this->pathNfile);
$ftype=filetype($this->pathNfile);
$handle=fopen($this->pathNfile,“r”);
$attachment=fread($handle,$file\u 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:'。$this->eol;
$this->body.='X-Attachment-Id:'.rand(100099999)。$this->eol;
$this->body.='Content Disposition:attachment'.$this->eol;
$this->body.=$this->eol.$attachment.$this->eol;
}
如果有附件,您将添加边界两次,一次是在prepareAttachment()
中,另一次是在sendMail()
中。添加另一个边界会给我另一个文件附件。我在prepareAttachment()函数中删除了底部边界,现在它可以正常工作了。关于[]PHPMailer]()-你不必使用它,但我保证如果你重新发明一切,这不会是你的最后一个问题