Welcome Guest, Not a member yet? Register   Sign In
Cannot attach to email
#1

[eluser]Wario[/eluser]
I searched the forum for an answer and couldn't find one, so I apologize if this has already been solved.

I've been following this tutorial for CI: net.tutsplus.com, and I cannot get the email class to attach a simple file to the sent email. It appears in my inbox ok but with no attachment. Am I missing something in the config? I'm not sure why it's not working unless there is something else I need to set up in my php.ini file. I'm still a beginner in PHP so I could have overlooked something.

Code:
<?php
/**
* SENDS EMAIL WITH GMAIL
*/
class Email extends Controller
{
    function __construct()
    {
        parent::Controller();
    }
    
    function index()
    {
        $this->load->library('email');
        $this->email->set_newline("\r\n");
        
        $this->email->from('[email protected]', 'test');
        $this->email->to('[email protected]');
        $this->email->subject('This is a test email');
        $this->email->message('It is working! GREAT!');
        
        $path = $this->config->item('server_root');
        
        $file = FCPATH . 'attachments/yourInfo.txt';
        
        if(file_exists($file))
        {
            echo "File exists. Path ok: " . $file;
        }
        else
        {
            echo "Cannot find file: " . $file;
        }
        
        if($this->email->attach($file))
        {
            echo "File was attached: " . $file;
        }
        else
        {
            show_error($this->email->print_debugger());
        }
        
        if($this->email->send())
        {
            echo 'Your email was sent!';
            echo $file;
        }
        else
        {
            show_error($this->email->print_debugger());
        }
    }
}
?>

I created a new config file for email in put it in my application/config folder:

Code:
<?php if( ! defined('BASEPATH')) exit('No direct script access allowed');

$config['protocol']  = 'smtp';
$config['smtp_host'] = 'ssl://smtp.googlemail.com';
$config['smtp_port'] = 465;
$config['smtp_user'] = '[email protected]';
$config['smtp_pass'] = 'password';

Thanks for your help.
#2

[eluser]Wario[/eluser]
Outside of the system directory there is another folder called attachments, and in that directory there is a sample file call yourInfo.txt. The file_exists() test works, but for some reason that I can seem to get it will not attach the file in the email.
#3

[eluser]Unknown[/eluser]
Try to put a trailing slash

$file = FCPATH . 'attachments/yourInfo.txt';

instead

$file = FCPATH . '/attachments/yourInfo.txt';

hope this will help you




Theme © iAndrew 2016 - Forum software by © MyBB