Jump to content

Send email with attachment


Recommended Posts

Dear All!

 

This is the situation:

 

For every order whose status is changed to the custom status "Confirmed" an email should leave to the supplier with an xml file in attach (it contains addresses, product details etc).

 

Now, I implemented a function in Order.php of which I show you the relevant parts:

 

/* Attachment File */

// Attachment location

$file_name = $order_id.".xml";

$path = "../orders/";

 

// Read the file content

$file = $path.$file_name;

$file_size = filesize($file);

$handle = fopen($file, "r");

$content = fread($handle, $file_size);

fclose($handle);

$content = chunk_split(base64_encode($content));

 

...

 

Mail::Send(

5,

'new_order',

'New Order',

array(),

'[email protected]',

'Provider',

NULL,

'My ePortal',

array('{content}' => $content,'{mime}' => 'application/xml', '{name}' => $file),

NULL,

_PS_MAIL_DIR_

 

);

 

 

The email is sent correctly to [email protected] but WITHOUT the attachment.

 

Could anyone tell me what I am doing wrong here?

 

Thank you very much

 

Best Regards

 

Henrik

Link to comment
Share on other sites

Dear Statictic,

 

Thanks for your help, but the solution was another:

 

For some reason you cannot call the Mail::Send defining the $fileAttachment the way I did:

 

array('{content}' => $content,'{mime}' => 'application/xml', '{name}' => $file),

 

Instead you should populate the array before and use it in the call to Mail::Send like this:

 

$attach['content'] = $content;

$attach['name'] = $file;

$attach['mime'] = 'application/xml';

 

then:

 

Mail::Send(

5,

'new_order',

'New Order',

array(),

'[email protected]',

'Provider',

NULL,

'My ePortal',

$attach,

NULL,

_PS_MAIL_DIR_,

false

);

 

Thanks again.

 

All the best

 

Henrik

  • Like 1
Link to comment
Share on other sites

  • 2 years later...
  • 7 months later...

Good afternoon, I found that in this post they are trying to send email with attached files. My problem is, but only when I want to attach more than one file to my email. My code below works perfectly for a single file. I would like to know how I can modify my code to insert more than one file into the email. Thanks for any tips. Thank you.

 

if(!isset($_FILES) or !($_FILES > 0)) {

 $this->_error .= $this->displayError($this->l('Invalid file'));

}

else

{

 for($i=0; $i < count($_FILES['FILE']['name']); $i++)

{

 $explode = explode('.', $_FILES['FILE']['name'][$i]);

 $ext = end($explode);

 if(!in_array($ext, array('pdf','doc', 'docx', 'rar', 'zip')) )

{

 $this->_error .= $this->displayError($this->l('Invalid file extension'));

}

else {

 $ftype[] = $_FILES['FILE']['type'][$i];

 $fname[] = $_FILES['FILE']['name'][$i];

 $ftmp[] = $_FILES['FILE']['tmp_name'][$i];

 $fsize[] = $_FILES['FILE']['size'][$i];

}

}

}

 $files = $fname;

 $file_attachment = array();

 for($x=0;$x<count($files);$x++)

 {

 $tmpFilePath[$x] = $ftmp[$x];

 $newFilePath[$x] = dirname(__FILE__ ).'/import/' . $files[$x];

 move_uploaded_file($tmpFilePath[$x], $newFilePath[$x]);

 $file = fopen($newFilePath[$x],"r");

 $data = fread($file,$fsize[$x]);

 fclose($file); $data = $data;

 $file_attachment['name'] = $files[$x];

 $file_attachment['mime'] = 'application/pdf';

 $file_attachment['content'] = $data;

 }

//$file_attachment = array('content' => $data, 'mime' => 'application/pdf', 'name' => $files);

//var_dump($file_attachment);exit;

$res = Mail::Send(

$this->context->language->id,

'recibos',

$subject,

array('{message}' => $msg_html, $template_vars = false,),

$to, $to_name, $from, $from_name,

$file_attachment,

null,

$this->local_path.'mails/', null, null, $bcc, null

);

Edited by SilviaPimenta (see edit history)
Link to comment
Share on other sites

×
×
  • Create New...