[SOLVED] Send email with attached photo - El Forum - 07-27-2010
[eluser]patak[/eluser]
Hi all!
Could you please help me with this issue.
I have a form with a text inputs and with 5 file inputs.
I get stuck on attachments. If the code is without Code: $this->email->attach()
it works fine but with attemtp to attach file I get an error Code: Unable to send email using PHP mail(). Your server might not be configured to send mail using this method.
Here is my controller
Code: <?php
class Soutez extends Controller {
function Soutez()
{
parent::Controller();
}
function index()
{
//some valiadation on text inputs
if ($this->form_validation->run() == FALSE)
{
//something wrong during validation
$this->load->view('formular');
}
else
{
$form_data['jmeno'] = $_POST['jmeno'];
$form_data['prijmeni'] = $_POST['prijmeni'];
$form_data['tel'] = $_POST['tel'];
$form_data['email'] = $_POST['email'];
$form_data['mesto_obec'] = $_POST['mesto_obec'];
$form_data['produkt'] = $_POST['produkt'];
$form_data['souhlas'] = $_POST['souhlas'];
//save photos to tmp folder
$i=1;
while ($i <= 5)
{
if(isset($_FILES['foto'.$i]['tmp_name']) && is_uploaded_file($_FILES['foto'.$i]['tmp_name']))
{
$config['allowed_types'] = 'gif|GIF|jpg|JPG|png|PNG';
$config['max_size'] = 1024;
$config['max_width'] = 1280;
$config['max_height'] = 1024;
$config['remove_spaces'] = TRUE;
$field_name = 'foto'.$i;
$img_success = $this->_handle_image_upload($config, $field_name, &$img_status);
}
$i++;
}
$message = $form_data['jmeno'] . "\r\n"
. $form_data['prijmeni'] . "\r\n"
. $form_data['tel'] . "\r\n"
. $form_data['email'] . "\r\n"
. $form_data['mesto_obec'] . "\r\n"
. $form_data['produkt'];
//send email with form data
$this->email->clear();
$this->email->from('[email protected]', 'FROM');
$this->email->to('[email protected]');
$this->email->subject('SUBJECT');
$this->email->message($message);
$this->email->attach($img_status['full_path']);
$this->email->send();
//confirmation page
$this->load->view('odeslano');
}
}
function _handle_image_upload($config, $field_name, $status)
{
$base_config = array();
$base_config['upload_path'] = 'tmp/';
$config = array_merge($base_config, $config);
$this->load->library('upload', $config);
$this->upload->initialize($config);
if ($this->upload->do_upload($field_name))
{
$status = $this->upload->data();
return true;
}
else
{
$status = $this->upload->display_errors();
return false;
}
}
}
/* End of file soutez.php */
/* Location: ./system/application/controllers/soutez.php */ and here is part of an config file
Code: $config['protocol'] = 'smtp';
$config['smtp_host'] = 'mail.server.com';
$config['smtp_port'] = 587; //given by provider
$config['smtp_timeout'] = 30;
$config['smtp_user'] = 'user';
$config['smtp_pass'] = 'pass';
$config['charset'] = 'utf-8';
$config['mailtype'] = 'text';
$config['wordwrap'] = TRUE;
$config['newline'] = "\r\n";
$config['crlf'] = "\r\n";
Could anyone look into my code and help me? I get stuck.
Thanks in advance!
Jan
------
I solved it by moving email config into controller and add Code: $this->email->initialize($config);
before Code: $this->email->clear();
|