Jump to content

Attachments to E-mails ?


Recommended Posts

Hello,

i was wondering if it is possible to add a Attachment to a Email? For example i want to add a PDF file which is saved on my server to the order confirmation email. And i also want to attach the Invoice.pdf to the Payment confirmation email...

Can you please tell me how i can do this?

many thanks, chris

Link to comment
Share on other sites

PrestaShop already attaches the shop's logo to the email. The code to do that is on line 94 of classes/Mail.php (in PrestaShop v1.3.2):

$templateVars['{shop_logo}'] = (file_exists(_PS_IMG_DIR_.'logo.jpg')) ? $message->attach(new Swift_Message_Image(new Swift_File(_PS_IMG_DIR_.'logo.jpg'))) : '';



I'm sure there's a way to add a PDF in a similiar way.

Link to comment
Share on other sites

yes looks good but i think this code needs to be modified a little... and i dont want to include the file to every email just to the order confirmation...

could it work like this? Or do i have to write the css variable 'attachment1' in another file or something?

$templateVars['{attachment1}'] = (file_exists(_PS_IMG_DIR_.'attachment1.pdf')) ? $message->attach(new Swift_Message_Image(new Swift_File(_PS_IMG_DIR_.'attachment1.pdf'))) : '';

Link to comment
Share on other sites

It isn't stored, it is generated. If you aren't already storing it somewhere, you will have to get the generated one.

if ($template == 'order_conf')
  $message->attach(new Swift_Message_Attachment(file_get_contents("/pdf-invoice.php?id_order=" . $templateVars['{id_order}']), "invoice.pdf", "application/pdf"));



You will also need to change lines 324 of classes/PaymentModule.php from:

$data = array(                    



to:

$data = array(    
  '{id_order}' => $order->id,                



This should generate the PDF invoice for order and attach it to the message.

Link to comment
Share on other sites

Ok perfect i try this later. But what if the PDF file was already generated? Will this one be used then or will both have the same invoice number?

I found another code in a German Prestashop forum to attach files to E-mails. But i think this will not work for the pdf invoice as this code will not generate the invoice...

Mail.php Line 137:

      // Germanized 18.10.2010 attach pdf: http://www.homepage-community.de/index.php?topic=1623.0
     if(strpos($template,'order_conf') !== false 
       && file_exists($templatePath.$iso.'/terms.pdf')){
       $message->attach(new Swift_Message_Attachment(new Swift_File($templatePath.$iso.'/terms.pdf')));        
     }



What do you think about this code will it work fine?

Link to comment
Share on other sites

That will work if you want to send a single PDF file from your mails directory to all your customers, but not if you want to send a different invoice for each order. You will need to use my code above to do that. You don't to worry about generating the PDF invoice multiple times. It won't cause the invoice number to increment. It will just use the same invoice number.

Link to comment
Share on other sites

  • 2 weeks later...

@rocky

I need this code also to add the pdf into the mail order_conf.

But in your post with this code:

if ($template == 'order_conf')
  $message->attach(new Swift_Message_Attachment(file_get_contents("/pdf-invoice.php?id_order=" . $templateVars['{id_order}']), "invoice.pdf", "application/pdf"));



You tell not where I need to place that code...

Can you tell me that..

Link to comment
Share on other sites

I have no idea. I didn't actually test that code. It should logically work. The file_get_contents function call should get the PDF invoice, then the Swift_Message_Attachment object should save it as a PDF called invoice.pdf, then $message->attach should attach it to the email. I can't think of why it wouldn't work.

Link to comment
Share on other sites

  • 2 months later...

Hi Rocky,

I would like to do the same but I installed the one page checkout module. As far as I understand, the module creates new classes/mail.php and classes/paymentmodule.php files. The original files were renamed mail.php.opbak and paymentmodule.php.opbak.

The new files that were created by the module look completely different and I cannot even find the lines where I should include the code you posted.
Do you know whether it's possible to attach the invoice to emails with the one page checkout module installed. If so, do I need a different code and where do I have to paste it?

I don't know if you have access to the one page checkout code of mail.php and paymentmodule.php files. Let me know if not and I will paste it here.

It would be amazing if you could help me here.

Link to comment
Share on other sites

  • 2 weeks later...
  • 2 weeks later...

Rocky,

I deleted the if ($template == 'order_conf') part and i am getting invoices on all attachments which is a good sign.
There may be an issue with the if statement, is there even a template called order_conf?

Also the attachments seem to be corrupt, i cant open them :-(

Link to comment
Share on other sites

  • 2 weeks later...
  • 1 year later...
  • 4 months later...

I managed to include a static pdf with this code:

 

#file classes/Mail.php, line 270 (before "Send Mail" comment)
if ($template == 'en/order_conf')
   $message->attach(new Swift_Message_Attachment(file_get_contents("/full-path/home/example/public_html/attachment.pdf"), "attachment.pdf", "application/pdf"));

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

  • 1 month later...
  • 1 month later...

This is the line of code I am using, and a pdf gets sent, but it seems corrupt and cant open it:

 

$message->attach(new Swift_Message_Attachment(file_get_contents("/pdf-invoice.php?id_order=" . $templateVars['{id_order}']), "invoice.pdf", "application/pdf"));

 

Its probably because the pdf-invoice.php bit actually needs the customers login session in order for it to work.

 

if (strpos($template, 'payment' || strpos($template, 'shipped')){

$id_factura = $templateVars['{id_order}'];

$orden = new Order($id_factura);

$factura['content'] = PDF::invoice($orden, 'S');

$factura['name'] = $id_factura.'.pdf';

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

$message->attach(new Swift_Message_Attachment($factura['content'], $factura['name'], $factura['mime']));

}

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

  • 1 month later...
  • 5 months later...

hello there,

 

i want to attach a file (example.jpg) in mailalerts  module. i tried with your code. it does not work :(. please help me out with this. 

 

thanks  in advance.

#mailalerts.php line 386
if (file_exists(dirname(__FILE__).'/mails/'.$iso.'/'.$template.'.txt') &&
			file_exists(dirname(__FILE__).'/mails/'.$iso.'/'.$template.'.html')){
			$template->attach(new Swift_Message_Attachment(file_get_contents("path/images/logo.png"), "logo.png", "image/png"));
				Mail::Send(
					$id_lang,
					$template,
					sprintf(Mail::l('New order - #%06d', $id_lang), $order->id),
					$template_vars,
					explode(self::__MA_MAIL_DELIMITOR__, $this->_merchant_mails),
					null,
					$configuration['PS_SHOP_EMAIL'],
					$configuration['PS_SHOP_NAME'],
					null,
					null,
					dirname(__FILE__).'/mails/'
				);
		}
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...