使用 Codeigniter 和 PHPMailer 在邮件中附加文件

Attach file in mail using Codeigniter & PHPMailer

我正在使用 PHPMailer 和 codeigniter

https://github.com/ivantcholakov/codeigniter-phpmailer 发送带有附件的邮件但我收到错误代码是

$this->load->library('email');
$result = $this->email
                ->from('xxxx@gmail.com',"xxx xxx")
                ->reply_to('xxx@xxx.com')    // Optional, an account where a human being reads.
                ->to('xxx@xxx.com')
                ->subject($subject)
                ->message($body)
                ->AddAttachment('/var/www/html/phase2.png', 'test.png')
                ->send();

致命错误:调用未定义的方法 MY_Email::AddAttachment()

在github我发现需要附加文件的方法不是AddAttachment,而是attach。

https://github.com/ivantcholakov/codeigniter-phpmailer/blob/master/libraries/MY_Email.php#L416

希望对您有所帮助!

In Codeigniter we use attach() instead of AddAttachment(). And what ever the file you attaching place it inside Codeigniter project folder.(outside application folder).

试试这个

$this->email->from('xxxx@gmail.com',"xxx xxx");
$this->email->reply_to('xxx@xxx.com');
$this->email->to('xxx@xxx.com');
$this->email->subject($subject);
$this->email->message($body);
$this->email->attach('assets/mail/phase2.png');

if(!$this->email->send()){
    echo $this->email->print_debugger();
}
else{
    echo "Success";
}

所以你的文件结构看起来像

application
assets
    - images
    - css
    - js
    - mail
        - phase2.png
system
index.php
.htaccess

阅读此链接

  1. attach($filename[, $disposition = ''[, $newname = NULL[, $mime = '']]])

尝试使用 $_SERVER['DOCUMENT_ROOT'] 如:

$mail->AddAttachment($_SERVER['DOCUMENT_ROOT']."/beta/uploads/file.pdf");