0

I want to attach two excel files (sealing_production.xls & sealing_assembly.xls)to my email using php,but I have no idea on how to do it.

Problem

When I click on my send mail button, I gets the mail. with all details. but attachment is not working.

Try

I echo the file name, its prints the file name from controller

Here my php code for the send Email function

function sendEmail() {

     $user     = "dheanas@gmail.com";
     $cc        = "as@gmail.com";
     $status    =  "Dear All,";

     $date = date("y-m-d") ;
     $xls        = "Sealing Report/{$date}/sealing_production.xls";
     $xls1        = "Sealing Report/{$date}/sealing_assembly.xls";

     $subject   = "Sealing Daily Production & Daily Output Report ";

      $content   = "<p>{$status}</p>
                    <p>
                    <p>Please find the attached files of Sealing Daily Production 
                         & Daily Output Report</p> 
                    <p>for your reference and further actions.</p> 

                    <br><p>Thank you very much. </p>
                    <p>This is Computer Generated Email, please do not reply. </p></br>";

    $this -> global_model -> sendfullmail($user, $subject, $content,$cc, '');  

}

and this the function of sendfullmail in my model:

//send out full email include cc and bcc
    function sendfullmail($receipient, $title, $message,$cc,$bcc){
        $this -> load -> library('email');
        $this -> email -> from($this -> from, $this ->fromname);
        $this -> email -> to($receipient);
        $this -> email -> cc($cc);
        $this -> email -> set_alt_message($this -> alt);
        if ($bcc != '') {
        $bcc .= ',sindisystem@gmail.com';
        } else {
        $bcc = 'sindisystem@gmail.com';
        }
        $this -> email -> bcc($bcc);
        $this -> email -> subject($title);
        $this -> email -> message($message);
        $this -> email -> send();

    }

What I have tried:

I have check similar links on stack overflow about this. But i'm unable to find any solution for this.

Link's i have checked

1.Codeigniter send email with attach file
2.Code Igniter -> attach email
3.codeigniter send pdf file as email attachment
4.How To Send Email With An Attachment In Codeigniter?
5.How to attach two or multiple files and send mail in PHP
6.Multiple e-mail attachments

I also try to put
$this-> email -> attach($file);
in my model

and also this in my sendEmail function
$this -> global_model -> sendfullmail($user, $subject, $content,$cc, '',$xls);

but the email that i receive is only blank email without content.

Edited by Reverend Jim: Moved to programming forum

2
Contributors
2
Replies
63
Views
1 Week
Discussion Span
Last Post by cereal
0

Hi,

it can depend on the path of the files, for example:

$xls  = "Sealing Report/{$date}/sealing_production.xls";
$xls1 = "Sealing Report/{$date}/sealing_assembly.xls";

the path is relative to the script position, so if the files are in the same directory of the main index.php, then change them to:

$xls  = FCPATH . "Sealing Report/{$date}/sealing_production.xls";
$xls1 = FCPATH . "Sealing Report/{$date}/sealing_assembly.xls";

the FCPATH constant is set in the main index.php file and refers to the directory of this file. Also you could serve an array list to the function, so:

$attach_list = [$xls, $xlsl];

Once you have done this, add the $attachments argument to your function and a loop to add the files to the message:

<?php

function sendfullmail($receipient, $title, $message, $cc = '', $bcc = '', $attachments = [])
{
    $this->load->library('email');

    $this->email->from($this->from, $this->fromname);
    $this->email->to($receipient);

    if('' !== $cc)
        $this->email->cc($cc);

    if('' === $bcc)
        $bcc = 'sindisystem@gmail.com';

    else
        $bcc .= ',sindisystem@gmail.com';

    $this->email->bcc($bcc);

    if(is_array($attachments) && 0 < ($c = count($attachments)))
        for($i = 0; $i < $c; $i++)
            $this->email->attach($attachments[$i]);

    elseif(is_string($attachments))
        $this->email->attach($attachments);

    $this->email->subject($title);
    $this->email->message($message);
    $this->email->set_alt_message($this->alt);
    return $this->email->send();
}

$xls  = FCPATH . "Sealing Report/{$date}/sealing_production.xls";
$xls1 = FCPATH . "Sealing Report/{$date}/sealing_assembly.xls";

// or submit a string: $attach_list = $xls;
$attach_list = [$xls, $xlsl];

sendfullmail('recipient@mail.tld', 'Hello', 'here you go', '', '', $attach_list);

If by adjusting the path it still does not work, then set send() to FALSE and add the print_debugger() method:

// You need to pass FALSE while sending in order for the email data
// to not be cleared - if that happens, print_debugger() would have
// nothing to output.
$this->email->send(FALSE);

// Will print the email headers, including the message subject and body
print $this->email->print_debugger();

From there you should see why it fails. See the documentation if you need more options:

Edited by cereal

Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.