简体   繁体   中英

Attach file in mail using Codeigniter & PHPMailer

I am using PHPMailer and codeigniter

https://github.com/ivantcholakov/codeigniter-phpmailer to send mail with attach file but I got error my code is

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

Fatal error: Call to undefined method MY_Email::AddAttachment()

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

Try this

$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";
}

So your file structure looks like

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

Read this links

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

On github I found that the method needed to attach a file is not AddAttachment, but attach.

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

Hope this helps!

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM