How To Attach A File To Email While Placing An Order?

I need to attach a file while sending email after placing order. The file should be attached as attachment. How to attach a file to email while placing an order?

I create a file while placing the order by using “fn_place_order”. I guess I should use “fn_order_notification” in fn.cart.php but I am not sure the way I must follow?

I found the way. I need to add this code



'attachments' => array(
'myfile.pdf' => $file_location
),




into “Mailer::sendMail” in “fn_order_notification” function in fn.cart.php



I don't want to change the code directly by adding this into the codes. There is going to be incorrect. I just want to add this when placing an order.



How to add “attachments” parameters in Mailer::sendMail ?

hi,



please check your PM.



best regards,

WSA team

I want to add


'attachments' => array(
'myfile.pdf' => $file_location
),




into “Mailer::sendMail” array which is in if ($notify_user == true) { } in fn_order_notification function without changing core files.

Is there a way to do this?



Actually, what I want is just adding a file while sending the email. I don't want to send a new email.

You can use the 'send_mail_pre' hook to adjust any parameters or anything in the $mailer object.

[quote name='tbirnseth' timestamp='1436463675' post='222304']

You can use the 'send_mail_pre' hook to adjust any parameters or anything in the $mailer object.

[/quote]



Thanks so much. I fixed the issue.

I have experiencing a problem. I use “send_mail_pre” in my function and can add attachment array to the email. I am trying this on localhost (wampserver). It works. But when I upload the addon to the live site I can NOT add the attachment array to mail. So, the mail is going without attachment. What might be cause this?



File location that I add to attachment is like this: /home/bxxxxxx/public_html/terms/10-07-2015/myfile_name.pdf

Many possibilities. What does your PHP error_log show? Are you seeing a “File not found” error? Otherwise I can only assume that you did not register the hook. You might try adding some debug code to your hook function to insure it is being invoked.

When I operates the funcion it is being sent but the error_log at localhost I see this:



[11-Jul-2015 11:38:06 UTC] PHP Deprecated: Function set_magic_quotes_runtime() is deprecated in C:\wamp\www\app\lib\other\phpmailer\class.phpmailer.php on line 1531
[11-Jul-2015 11:38:06 UTC] PHP Deprecated: Function set_magic_quotes_runtime() is deprecated in C:\wamp\www\app\lib\other\phpmailer\class.phpmailer.php on line 1535




When I try the addon at live website it doesn't give any errors or notice in error_log, the email is sent but the attachment is not being attached to email that is sent.



Here is the funcion that I use:



function fn_my_addon_send_mail_pre($mailer, $params, $area, $lang_code){


if( Registry::get('addons.my_addon.addfile') == 'Y' && $area == 'C') {

if (!empty($params['data']['order_info'])) {
$file= fn_my_addon_format_name($params['data']['order_info']['order_id'].'_'.$params['data']['order_info']['firstname'].'_'.$params['data']['order_info']['lastname'].'_'.htmlentities(date("d_m_Y")));
$location = Registry::get('config.dir.root') . '/terms/'.date("d-m-Y").'/'.$file.".pdf";



if(file_exists($location)) {
$params['attachments']= array(
__('thefilename').'.pdf' => $location
);
}

}

}

}




I am checking the file in if(file_exists($location)) { }. The file is OK.





PS: the hook is registered in init.php.

I have checked app/Tygh/Mailer.php and add fn_print_die($params[‘attachments’]) before the code below and tested at live website


<br />
if (!empty($params['attachments'])) {<br />
			foreach ($params['attachments'] as $name => $file) {<br />
				$mailer->AddAttachment($file, $name);<br />
			}<br />
		}<br />

```<br />
<br />
and see that nothing is in attachment array <img src="upload://n4syhXZrRhsStKvmS4jT3Mp2S3k.png" class="bbc_emoticon" alt=":("> But it works at localhost.

Let's back up a little. What version of cs-cart are you running and what version of PHP? You shouldn't have a file called 'class.phpmailer.php' in V4. So it sounds like you are on V2 or V3 and running a version of PHP above 5.3 (which is not technically supported).



Please clarify.

[quote name='tbirnseth' timestamp='1436644965' post='222574']

Let's back up a little. What version of cs-cart are you running and what version of PHP? You shouldn't have a file called 'class.phpmailer.php' in V4. So it sounds like you are on V2 or V3 and running a version of PHP above 5.3 (which is not technically supported).



Please clarify.

[/quote]



PHP ver: 5.4

CS cart ver: 4.2.4 (Multivendor)



I had upgraded from 4.2.1 to 4.2.4 by uploading the files over the old files. So, by overwriting on old files. But default cs cart install package ver of 4.2.4 includes “class.phpmailer” in app\lib\other\phpmailer.



BTW, today I install my addon to an another live website which version is 4.3.3, the addon works fine there.

So you're all set now?

[quote name='tbirnseth' timestamp='1436650493' post='222586']

So you're all set now?

[/quote]

This the configuration of my website I have been using for months.