1,105,332 Community Members

Attach a pdf file to an email from outside the root folder

Member Avatar
cgull
Junior Poster
123 posts since Dec 2009
Reputation Points: 25 [?]
Q&As Helped to Solve: 5 [?]
Skill Endorsements: 2 [?]
 
0
 

Hello,

I am developing a site with Codeigniter 2.1.3.

I am creating a pdf file with mpdf and I save the file in a folder outside the root folder.
So my folders look like this:
c:/mysite/uploads/mypdffile.pdf
c:/mysite/public_html

I looked at all kind of tutorials and forum posts about the php header function, but I can't understand how to use it to get the file.

I am using: $mail->AddAttachment($mypdf) but how do I get the file if it is in a folder outside the root folder?

Can someone please help?

Thank you

Member Avatar
cgull
Junior Poster
123 posts since Dec 2009
Reputation Points: 25 [?]
Q&As Helped to Solve: 5 [?]
Skill Endorsements: 2 [?]
 
0
 

Sorry, maybe I should add some of my code here:
In my controller I have this code:

$filename = 'mypdf.pdf';
$pdfFilePath = "../uploads/$filename";

if(file_exists($pdfFilePath) == FALSE)
{
    ini_set('memory_limit','128M');
    $html = $this->load->view('email/invoice', $data, true); // render the view into HTML

    $this->load->library('pdf');
    $pdf = $this->pdf->load();
    $pdf->SetFooter($_SERVER['HTTP_HOST'].'|{PAGENO}|'.date(DATE_RFC822)); 
    $pdf->logo = file_get_contents(site_url('assets/img/logo.png'));
    $pdf->WriteHTML($html); // write the HTML into the PDF
    $pdf->Output($pdfFilePath, 'F'); // save to file because we can
}

$subject = 'Your order with Highland Coffee Roastery';
$body = 'email/send_order';
$emailToName = $data['user']->first_name . ' ' . $data['user']->last_name;

$attach = $pdfFilePath;
$emailSent = sendEmail('info@mysite.co.za', 'My Site', 'noreply@mysite.co.za', 'NoReply', $data['user']->email, $emailToName, $subject, $body, $data, $attach);

In my send email helper:

function sendEmail($fromEmail, $fromName, $replyTo, $replyToName, $emailTo, $emailToName, $subject, $body, $bodyData, $attach='')
{
    require_once APPPATH."libraries/phpmailer/class.phpmailer.php";
    $mail = new PHPMailer();
    $CI =& get_instance();

    $body             = $CI->load->view($body, $bodyData, TRUE);    $mail->IsSMTP(); // telling the class to use SMTP
    $mail->SMTPDebug  = 0;// enables SMTP debug information (for testing)
    $mail->IsHTML(true);

    if(ENVIRONMENT == 'development')
    {
        $mail->SMTPAuth   = true;                  // enable SMTP authentication
        $mail->SMTPSecure = "ssl";                 // sets the prefix to the servier
        $mail->Host       = 'smtp.gmail.com'; // SMTP server
        $mail->Port       = 465;                    // set the SMTP port for the GMAIL server
        $mail->Username   = "myemail@gmail.com";     // GMAIL username
        $mail->Password   = "password";            // GMAIL password
    }

    $mail->SetFrom($fromEmail, $fromName);
    $mail->AddReplyTo($replyTo, $replyToName);
    $mail->Subject    = $subject;
    $mail->AltBody    = "To view the message, please use an HTML compatible email viewer!";     // optional, comment out and test
    $mail->MsgHTML($body);

    $address = $emailToName . ' ' . $emailTo; //"recipient address@someadress.com";
    $mail->AddAddress($emailTo, $emailToName);

    $logo = 'logo_small.png';
    $logoUrl = 'assets/img/logo_small.png';

    $mail->AddEmbeddedImage($logoUrl, 'logo', $logo);
    if($attach != '')
        $mail->AddAttachment($attach);

    if($mail->Send())
        return TRUE;
    else
        dump($mail->ErrorInfo);
}

The file is not being attached as the server can't find it.

Hope you understand what I am trying to ask.... ;)

Member Avatar
cereal
Posting Virtuoso
1,710 posts since Aug 2007
Reputation Points: 461 [?]
Q&As Helped to Solve: 345 [?]
Skill Endorsements: 44 [?]
 
0
 

Set an absolute path, absolute to the system not to the web root, so change this:

$pdfFilePath = "../uploads/$filename";

to:

$pdfFilePath = "c:/mysite/uploads/$filename";
Member Avatar
cgull
Junior Poster
123 posts since Dec 2009
Reputation Points: 25 [?]
Q&As Helped to Solve: 5 [?]
Skill Endorsements: 2 [?]
 
0
 

Thanks, but that won't work on the live server?

Member Avatar
cereal
Posting Virtuoso
1,710 posts since Aug 2007
Reputation Points: 461 [?]
Q&As Helped to Solve: 345 [?]
Skill Endorsements: 44 [?]
 
1
 

On live server you can use the Document Root to get an idea of the correct path, for example:

echo $_SERVER['DOCUMENT_ROOT'];

returns the server root, if this is something like /var/www/mysite/public_html/ and your upload directory is at the same level of public_html, then you can set:

$pdfFilePath = "/var/www/mysite/uploads/$filename";
You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: