skip to Main Content

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"    => "rn",
                "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.

2

Answers


  1. 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);;
    
    Login or Signup to reply.
  2. Assume your file is in assets/uploads/, (I Recommend inside the assets folder)

    I use the code below to open the file.

    # test with normal file and add your original file path
    $filePath = FCPATH . 'assets/uploads/download.jpg'; 
    

    FCPATH contains the absolute path to your project’s root folder.

    And then use $this->email->attach($filePath); to attach the file.


    Example

    $this->load->library('email');
    
    $config = array(
        'protocol'  => 'SMTP',
        ....
        'mailtype'  => 'html',
        'charset'   => 'iso-8859-1'
    );
    
    $this->email->initialize($config);
    
    $this->email->from('[email protected]', 'Your Name');
    $this->email->to('[email protected]');
    $this->email->subject('Email with Attachment');
    $this->email->message('Testing Email with Attachment.....');
    

    $filePath = FCPATH . ‘assets/uploads/download.jpg’;
    $this->email->attach($filePath);

    if($this->email->send()) {
        echo 'success';
    } else {
        echo $this->email->print_debugger();
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search