Attach file in mail using Codeigniter & PHPMailer


Attach file in mail using Codeigniter & PHPMailer

我正在使用PHPMailer和代码点火器

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()

Codeigniter中,我们使用attach()而不是AddAttachment()。您附加的文件将其放在Codeigniter项目文件夹中。(外部应用程序文件夹)。

试试这个

$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 = '']]])

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

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

希望这能有所帮助!

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

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