Search code examples
phpemailcodeigniter-3email-attachments

CodeIgniter attach email function issue


It would be appreciated if you help me with following code as I follow correct step but still not receiving email with attachment.

Will be appreciated if any of you find the correct issue.

Here is smtp setting of email:

$config = array(
                "protocol"   => "SMTP",
                "smtp_host"  => "domainname.com",
                "smtp_port"  => "465",
                "smtp_timeout"  => "7",
                "smtp_user"  => '[email protected]',
                "smtp_pass"  => "password", 
                "starttls"   => TRUE,
                "charset"    => "iso-8859-1",
                "mailtype"   => "html", 
                "wordwrap"   => TRUE,
                "newline"    => "\r\n",
                "validate"   => FALSE,
                "smtp_keep_alive" => TRUE
            );
            
            $this->load->library('email', $config);

and there is send email setting, note that i have upload file getting file path through DOCUMENT_ROOT but still not getting email with attachment only empty email with content provided

$this->email->from($Data['Email'], $Data['Name']);
                    $this->email->to('[email protected]');
                    $this->email->subject(sprintf('Email Send by - %s', $Data['Name']));
                    $this->email->message(sprintf('
                        Request ID: %s <br />
                        Name: %s <br />
                        Email: %s<br />
                        Colors: %s <br />
                        Quantity: %s<br />
                        Phone: %s<br />
                        Message: %s <br />
                        Target Price: %s'
                    , $PK,$Data['Name'], $Data['Email'], $Data['Colors'], $Data['Quantity'], $Data['Phone'], $Input->post('Config')['Message'],$Data['TargetPrice']));
                    //attachment
                    if($Row){
                         $ImagePath = GetFileRootPath($Row->EntryDate,'IMG',false,$Row->Image);;
                         // this function is returning this path - /home/User/public_html/domainname.com/resources/upload/img/2023/11/24/download.jpg
                         if($ImagePath){
                            $this->email->attach($ImagePath);
                            
                         }
                    }
                    
                    if($this->email->send()){
                        SetMessage('ENTRYSUCCESS');
                    } else {
                        SetMessage('ENTRYFAILED');
                    }
                
                } else{
                    SetMessage('ENTRYFAILED');
                }

again mentioning below the return of GetFileRootPath function so its easy to read: /home/User/public_html/domainname.com/resources/upload/img/2023/11/24/download.jpg

this is what i am sending in attach function but still getting email without attachment.


Solution

  • Have you logged the path to make sure it is getting set?

    if($ImagePath){
        log_message($ImagePath);
        $this->email->attach($ImagePath);
    }
    

    Unless you are checking somewhere else I would suggest checking to make sure file exists.

    if(is_file($ImagePath)){
        $this->email->attach($ImagePath);
    }
    

    Check the permissions of the file to make sure the account user has rights to it.

    Try attaching a simple text file instead of a image file to see if its something with the file itself. The receiving server may be stripping it out.

    BTW, you have an extra semicolon here:

    $ImagePath = GetFileRootPath($Row->EntryDate,'IMG',false,$Row->Image);;