[Skip to main content](#vessel-content)

#   function addAttachment in onAfterOrderCreate

##

- **[Dasa](/forum/user/79122-dasa "View Dasa's Profile")**
- [![Dasa's Avatar](https://www.hikashop.com/media/kunena/core/svg/person.svg)](/forum/user/79122-dasa "View Dasa's Profile") Topic Author
- [ Away](/forum/user/79122-dasa)

Less More

- Posts: 5
- Thank you received: 0
-
-

2 years 11 months ago [\#356359](#356359) by [Dasa](/forum/user/79122-dasa "View Dasa's Profile")  *[function addAttachment in onAfterOrderCreate](/forum/31-development/907224-function-addattachment-in-onafterordercreate#356359 "-- HikaShop version -- : 5.0.0 -- Joomla version -- : 4.0.3 -- PHP version -- : 7.4.33 -- Browser(s) name and version -- : browser and version independent -- Error-message(debug-mod must be tuned on) -- : no error message  Hi, I am trying to develope Hikashop payment plugin, what will generate a payment QR code. It works properly except one issue - I am not able to add image with QR code as attachment to email. I used this:  public function onAfterOrderCreate(&amp;$order, &amp;$send&#95;email) { $qrCodeFile = JPATH&#95;ROOT . '/images/qrcode.png'; $mailer = JFactory::getMailer(); $mailer-&gt;addAttachment($qrCodeFile, 'qrcode.png'); $send&#95;email = true; } There are no error messages, everything seems to be fine, but email order confirmation comes without the attachment. Path of the file is OK, the file exists, attachments are allowed in the settings in Hikashop. could anyone give me some advice?")* was created by *[Dasa](/forum/user/79122-dasa "View Dasa's Profile")*

-- HikaShop version -- : 5.0.0
-- Joomla version -- : 4.0.3
-- PHP version -- : 7.4.33
-- Browser(s) name and version -- : browser and version independent
-- Error-message(debug-mod must be tuned on) -- : no error message

Hi, I am trying to develope Hikashop payment plugin, what will generate a payment QR code. It works properly except one issue - I am not able to add image with QR code as attachment to email. I used this:

public function onAfterOrderCreate(&amp;$order, &amp;$send_email) {
$qrCodeFile = JPATH_ROOT . '/images/qrcode.png';
$mailer = JFactory::getMailer();
$mailer-&gt;addAttachment($qrCodeFile, 'qrcode.png');
$send_email = true;
}
There are no error messages, everything seems to be fine, but email order confirmation comes without the attachment. Path of the file is OK, the file exists, attachments are allowed in the settings in Hikashop. could anyone give me some advice?

Please [Log in](/login?return=aHR0cHM6Ly93d3cuaGlrYXNob3AuY29tL2ZvcnVtL2RldmVsb3BtZW50LzkwNzIyNC1mdW5jdGlvbi1hZGRhdHRhY2htZW50LWluLW9uYWZ0ZXJvcmRlcmNyZWF0ZT90bXBsPW1hcmtkb3du) or [Create an account](/component/users/registration?Itemid=101) to join the conversation.

- **[nicolas](/forum/user/63-nicolas "View nicolas's Profile")**
- [![nicolas's Avatar](https://www.hikashop.com/media/kunena/avatars/resized/size144/users/avatar63.png?1793428657)](/forum/user/63-nicolas "View nicolas's Profile")
- [ Offline](/forum/user/63-nicolas)

Less More

- Posts: 86315
- Thank you received: 14276
-
-

2 years 11 months ago [\#356376](#356376) by [nicolas](/forum/user/63-nicolas "View nicolas's Profile")  Replied by *[nicolas](/forum/user/63-nicolas "View nicolas's Profile")* on topic *[function addAttachment in onAfterOrderCreate](/forum/31-development/907224-function-addattachment-in-onafterordercreate#356376 "-- HikaShop version -- : 5.0.0 -- Joomla version -- : 4.0.3 -- PHP version -- : 7.4.33 -- Browser(s) name and version -- : browser and version independent -- Error-message(debug-mod must be tuned on) -- : no error message  Hi, I am trying to develope Hikashop payment plugin, what will generate a payment QR code. It works properly except one issue - I am not able to add image with QR code as attachment to email. I used this:  public function onAfterOrderCreate(&amp;$order, &amp;$send&#95;email) { $qrCodeFile = JPATH&#95;ROOT . '/images/qrcode.png'; $mailer = JFactory::getMailer(); $mailer-&gt;addAttachment($qrCodeFile, 'qrcode.png'); $send&#95;email = true; } There are no error messages, everything seems to be fine, but email order confirmation comes without the attachment. Path of the file is OK, the file exists, attachments are allowed in the settings in Hikashop. could anyone give me some advice?")*

Hi,

You can't do it like this.
The line:
$mailer = JFactory::getMailer();
will instantiate a new mailer object.
The line:
$mailer-&gt;addAttachment($qrCodeFile, 'qrcode.png');
will add the attachment to this mailer object.
But then, you don't call the send function or add a receiver email address, etc to the mailer object so nothing happens.

HikaShop, will load its emails with its own instance of the mailer, different from yours.
If you want to have a plugin add attachment to the emails sent by HikaShop, you need to implement an event which gives you access to the mailer instance.

For example, HikaShop has the event onBeforeMailSend(&amp;$mail, &amp;$mailer) which you can implement. This gives you access to the mailer instance used by HikaShop so you can call the addAttachment function on it directly.
[ www.hikashop.com/support/documentation/6...tml#onBeforeMailSend](https://www.hikashop.com/support/documentation/62-hikashop-developer-documentation.html#onBeforeMailSend)

Please [Log in](/login?return=aHR0cHM6Ly93d3cuaGlrYXNob3AuY29tL2ZvcnVtL2RldmVsb3BtZW50LzkwNzIyNC1mdW5jdGlvbi1hZGRhdHRhY2htZW50LWluLW9uYWZ0ZXJvcmRlcmNyZWF0ZT90bXBsPW1hcmtkb3du) or [Create an account](/component/users/registration?Itemid=101) to join the conversation.

- **[Dasa](/forum/user/79122-dasa "View Dasa's Profile")**
- [![Dasa's Avatar](https://www.hikashop.com/media/kunena/core/svg/person.svg)](/forum/user/79122-dasa "View Dasa's Profile") Topic Author
- [ Away](/forum/user/79122-dasa)

Less More

- Posts: 5
- Thank you received: 0
-
-

2 years 11 months ago [\#356378](#356378) by [Dasa](/forum/user/79122-dasa "View Dasa's Profile")  Replied by *[Dasa](/forum/user/79122-dasa "View Dasa's Profile")* on topic *[function addAttachment in onAfterOrderCreate](/forum/31-development/907224-function-addattachment-in-onafterordercreate#356378 "-- HikaShop version -- : 5.0.0 -- Joomla version -- : 4.0.3 -- PHP version -- : 7.4.33 -- Browser(s) name and version -- : browser and version independent -- Error-message(debug-mod must be tuned on) -- : no error message  Hi, I am trying to develope Hikashop payment plugin, what will generate a payment QR code. It works properly except one issue - I am not able to add image with QR code as attachment to email. I used this:  public function onAfterOrderCreate(&amp;$order, &amp;$send&#95;email) { $qrCodeFile = JPATH&#95;ROOT . '/images/qrcode.png'; $mailer = JFactory::getMailer(); $mailer-&gt;addAttachment($qrCodeFile, 'qrcode.png'); $send&#95;email = true; } There are no error messages, everything seems to be fine, but email order confirmation comes without the attachment. Path of the file is OK, the file exists, attachments are allowed in the settings in Hikashop. could anyone give me some advice?")*

Thank you. It worked for me with the onBeforeMailSend(&amp;$mail, &amp;$mailer), but in the wrong way. The attached QRcode came from a previous order...

Please [Log in](/login?return=aHR0cHM6Ly93d3cuaGlrYXNob3AuY29tL2ZvcnVtL2RldmVsb3BtZW50LzkwNzIyNC1mdW5jdGlvbi1hZGRhdHRhY2htZW50LWluLW9uYWZ0ZXJvcmRlcmNyZWF0ZT90bXBsPW1hcmtkb3du) or [Create an account](/component/users/registration?Itemid=101) to join the conversation.

- **[nicolas](/forum/user/63-nicolas "View nicolas's Profile")**
- [![nicolas's Avatar](https://www.hikashop.com/media/kunena/avatars/resized/size144/users/avatar63.png?1793428657)](/forum/user/63-nicolas "View nicolas's Profile")
- [ Offline](/forum/user/63-nicolas)

Less More

- Posts: 86315
- Thank you received: 14276
-
-

2 years 11 months ago [\#356387](#356387) by [nicolas](/forum/user/63-nicolas "View nicolas's Profile")  Replied by *[nicolas](/forum/user/63-nicolas "View nicolas's Profile")* on topic *[function addAttachment in onAfterOrderCreate](/forum/31-development/907224-function-addattachment-in-onafterordercreate#356387 "-- HikaShop version -- : 5.0.0 -- Joomla version -- : 4.0.3 -- PHP version -- : 7.4.33 -- Browser(s) name and version -- : browser and version independent -- Error-message(debug-mod must be tuned on) -- : no error message  Hi, I am trying to develope Hikashop payment plugin, what will generate a payment QR code. It works properly except one issue - I am not able to add image with QR code as attachment to email. I used this:  public function onAfterOrderCreate(&amp;$order, &amp;$send&#95;email) { $qrCodeFile = JPATH&#95;ROOT . '/images/qrcode.png'; $mailer = JFactory::getMailer(); $mailer-&gt;addAttachment($qrCodeFile, 'qrcode.png'); $send&#95;email = true; } There are no error messages, everything seems to be fine, but email order confirmation comes without the attachment. Path of the file is OK, the file exists, attachments are allowed in the settings in Hikashop. could anyone give me some advice?")*

Hi,

Well, without knowing the code you're using to generate the QRCode I can't say much.

Please [Log in](/login?return=aHR0cHM6Ly93d3cuaGlrYXNob3AuY29tL2ZvcnVtL2RldmVsb3BtZW50LzkwNzIyNC1mdW5jdGlvbi1hZGRhdHRhY2htZW50LWluLW9uYWZ0ZXJvcmRlcmNyZWF0ZT90bXBsPW1hcmtkb3du) or [Create an account](/component/users/registration?Itemid=101) to join the conversation.

- **[Dasa](/forum/user/79122-dasa "View Dasa's Profile")**
- [![Dasa's Avatar](https://www.hikashop.com/media/kunena/core/svg/person.svg)](/forum/user/79122-dasa "View Dasa's Profile") Topic Author
- [ Away](/forum/user/79122-dasa)

Less More

- Posts: 5
- Thank you received: 0
-
-

2 years 11 months ago -  2 years 11 months ago [\#356393](#356393) by [Dasa](/forum/user/79122-dasa "View Dasa's Profile")  Replied by *[Dasa](/forum/user/79122-dasa "View Dasa's Profile")* on topic *[function addAttachment in onAfterOrderCreate](/forum/31-development/907224-function-addattachment-in-onafterordercreate#356393 "-- HikaShop version -- : 5.0.0 -- Joomla version -- : 4.0.3 -- PHP version -- : 7.4.33 -- Browser(s) name and version -- : browser and version independent -- Error-message(debug-mod must be tuned on) -- : no error message  Hi, I am trying to develope Hikashop payment plugin, what will generate a payment QR code. It works properly except one issue - I am not able to add image with QR code as attachment to email. I used this:  public function onAfterOrderCreate(&amp;$order, &amp;$send&#95;email) { $qrCodeFile = JPATH&#95;ROOT . '/images/qrcode.png'; $mailer = JFactory::getMailer(); $mailer-&gt;addAttachment($qrCodeFile, 'qrcode.png'); $send&#95;email = true; } There are no error messages, everything seems to be fine, but email order confirmation comes without the attachment. Path of the file is OK, the file exists, attachments are allowed in the settings in Hikashop. could anyone give me some advice?")*

I used simple PHP QR Code library ([ ](https://sourceforge.net/projects/phpqrcode/)[ sourceforge.net/projects/phpqrcode/](https://sourceforge.net/projects/phpqrcode/)), and this is the code generating QR code:
$account = "CZ560300000000011111111";
$variableSymbol = $order-&gt;order_number;
$amount = $order-&gt;order_full_price;

$qrCodeData = "SPD*1.0*ACC:$account*AM:$amount*CC:CZK*X-VS:$variableSymbol";

// setting path for QR code image
$filePath = JPATH_ROOT . '/images/qrcode.png';

// QR code generating
QRcode::png($qrCodeData, $filePath, QR_ECLEVEL_L, 6);

I assume that the problem occurs because the QR code is generated and the email is compiled at the same time, so the new version of the image is not attached to the email, but the old version with the same name is inserted.

Last edit: 2 years 11 months ago by [Dasa](/forum/user/79122-dasa "View Dasa's Profile").

Please [Log in](/login?return=aHR0cHM6Ly93d3cuaGlrYXNob3AuY29tL2ZvcnVtL2RldmVsb3BtZW50LzkwNzIyNC1mdW5jdGlvbi1hZGRhdHRhY2htZW50LWluLW9uYWZ0ZXJvcmRlcmNyZWF0ZT90bXBsPW1hcmtkb3du) or [Create an account](/component/users/registration?Itemid=101) to join the conversation.

- **[nicolas](/forum/user/63-nicolas "View nicolas's Profile")**
- [![nicolas's Avatar](https://www.hikashop.com/media/kunena/avatars/resized/size144/users/avatar63.png?1793428657)](/forum/user/63-nicolas "View nicolas's Profile")
- [ Offline](/forum/user/63-nicolas)

Less More

- Posts: 86315
- Thank you received: 14276
-
-

2 years 11 months ago [\#356400](#356400) by [nicolas](/forum/user/63-nicolas "View nicolas's Profile")  Replied by *[nicolas](/forum/user/63-nicolas "View nicolas's Profile")* on topic *[function addAttachment in onAfterOrderCreate](/forum/31-development/907224-function-addattachment-in-onafterordercreate#356400 "-- HikaShop version -- : 5.0.0 -- Joomla version -- : 4.0.3 -- PHP version -- : 7.4.33 -- Browser(s) name and version -- : browser and version independent -- Error-message(debug-mod must be tuned on) -- : no error message  Hi, I am trying to develope Hikashop payment plugin, what will generate a payment QR code. It works properly except one issue - I am not able to add image with QR code as attachment to email. I used this:  public function onAfterOrderCreate(&amp;$order, &amp;$send&#95;email) { $qrCodeFile = JPATH&#95;ROOT . '/images/qrcode.png'; $mailer = JFactory::getMailer(); $mailer-&gt;addAttachment($qrCodeFile, 'qrcode.png'); $send&#95;email = true; } There are no error messages, everything seems to be fine, but email order confirmation comes without the attachment. Path of the file is OK, the file exists, attachments are allowed in the settings in Hikashop. could anyone give me some advice?")*

Hi,

You could make the file path dynamic, like this:
$filePath = JPATH_ROOT . '/images/qrcode'.$order-&gt;order_id.'.png';
That way, you would make sure that the QR code used could only be the one of the order being handled.

Please [Log in](/login?return=aHR0cHM6Ly93d3cuaGlrYXNob3AuY29tL2ZvcnVtL2RldmVsb3BtZW50LzkwNzIyNC1mdW5jdGlvbi1hZGRhdHRhY2htZW50LWluLW9uYWZ0ZXJvcmRlcmNyZWF0ZT90bXBsPW1hcmtkb3du) or [Create an account](/component/users/registration?Itemid=101) to join the conversation.

- **[Dasa](/forum/user/79122-dasa "View Dasa's Profile")**
- [![Dasa's Avatar](https://www.hikashop.com/media/kunena/core/svg/person.svg)](/forum/user/79122-dasa "View Dasa's Profile") Topic Author
- [ Away](/forum/user/79122-dasa)

Less More

- Posts: 5
- Thank you received: 0
-
-

2 years 11 months ago [\#356454](#356454) by [Dasa](/forum/user/79122-dasa "View Dasa's Profile")  Replied by *[Dasa](/forum/user/79122-dasa "View Dasa's Profile")* on topic *[function addAttachment in onAfterOrderCreate](/forum/31-development/907224-function-addattachment-in-onafterordercreate#356454 "-- HikaShop version -- : 5.0.0 -- Joomla version -- : 4.0.3 -- PHP version -- : 7.4.33 -- Browser(s) name and version -- : browser and version independent -- Error-message(debug-mod must be tuned on) -- : no error message  Hi, I am trying to develope Hikashop payment plugin, what will generate a payment QR code. It works properly except one issue - I am not able to add image with QR code as attachment to email. I used this:  public function onAfterOrderCreate(&amp;$order, &amp;$send&#95;email) { $qrCodeFile = JPATH&#95;ROOT . '/images/qrcode.png'; $mailer = JFactory::getMailer(); $mailer-&gt;addAttachment($qrCodeFile, 'qrcode.png'); $send&#95;email = true; } There are no error messages, everything seems to be fine, but email order confirmation comes without the attachment. Path of the file is OK, the file exists, attachments are allowed in the settings in Hikashop. could anyone give me some advice?")*

Thank you very much for your patience, but...
The first part works correctly - it saves the image with the correct unique name, but I can't load the same image as an attachment to an email because I can't call **$order\_id** inside the function onBeforeMailSend(&amp;$mail, &amp;$mailer)

Please [Log in](/login?return=aHR0cHM6Ly93d3cuaGlrYXNob3AuY29tL2ZvcnVtL2RldmVsb3BtZW50LzkwNzIyNC1mdW5jdGlvbi1hZGRhdHRhY2htZW50LWluLW9uYWZ0ZXJvcmRlcmNyZWF0ZT90bXBsPW1hcmtkb3du) or [Create an account](/component/users/registration?Itemid=101) to join the conversation.

- **[nicolas](/forum/user/63-nicolas "View nicolas's Profile")**
- [![nicolas's Avatar](https://www.hikashop.com/media/kunena/avatars/resized/size144/users/avatar63.png?1793428657)](/forum/user/63-nicolas "View nicolas's Profile")
- [ Offline](/forum/user/63-nicolas)

Less More

- Posts: 86315
- Thank you received: 14276
-
-

2 years 11 months ago [\#356457](#356457) by [nicolas](/forum/user/63-nicolas "View nicolas's Profile")  Replied by *[nicolas](/forum/user/63-nicolas "View nicolas's Profile")* on topic *[function addAttachment in onAfterOrderCreate](/forum/31-development/907224-function-addattachment-in-onafterordercreate#356457 "-- HikaShop version -- : 5.0.0 -- Joomla version -- : 4.0.3 -- PHP version -- : 7.4.33 -- Browser(s) name and version -- : browser and version independent -- Error-message(debug-mod must be tuned on) -- : no error message  Hi, I am trying to develope Hikashop payment plugin, what will generate a payment QR code. It works properly except one issue - I am not able to add image with QR code as attachment to email. I used this:  public function onAfterOrderCreate(&amp;$order, &amp;$send&#95;email) { $qrCodeFile = JPATH&#95;ROOT . '/images/qrcode.png'; $mailer = JFactory::getMailer(); $mailer-&gt;addAttachment($qrCodeFile, 'qrcode.png'); $send&#95;email = true; } There are no error messages, everything seems to be fine, but email order confirmation comes without the attachment. Path of the file is OK, the file exists, attachments are allowed in the settings in Hikashop. could anyone give me some advice?")*

Hi,

Check the $mail variable and you'll see that you have the necessary data in there.
I would add something like that: Code:

if(empty($mail-&gt;data-&gt;order_id)) { return; // skip attachment when order data is not available } $filePath = JPATH_ROOT . '/images/qrcode'.$mail-&gt;data-&gt;order_id.'.png'; if(!file_exists($filePath)) { // generate the file } // add the file to the email $mailer-&gt;addAttachment($filePath);

Please [Log in](/login?return=aHR0cHM6Ly93d3cuaGlrYXNob3AuY29tL2ZvcnVtL2RldmVsb3BtZW50LzkwNzIyNC1mdW5jdGlvbi1hZGRhdHRhY2htZW50LWluLW9uYWZ0ZXJvcmRlcmNyZWF0ZT90bXBsPW1hcmtkb3du) or [Create an account](/component/users/registration?Itemid=101) to join the conversation.

Board Categories HikaShop- Install &amp; Update- Product &amp; Category Display- Content Creation- Sales and Orders- - Orders management- - Reports &amp; Statistics- Customers- Checkout- Payment Methods- Shipping Methods- Import &amp; Export (CSV, XLS…)- Specific Features- - Affilation- - Taxes- - Wishlist- - Vote- - Carousel &amp; Slider- - Mass Actions- - Filters &amp; Search- - Discount &amp; Coupon- - Feature Request- Integrations- Showcase- General Talk about HikaShop- Development- - HikaShop 3.0 Beta- Translation- Commercial Jobs HikaShop Components- HikaMarket- - Market: Feature requests- - Market: How to?- - Market: Bug Report- HikaSerial- - General talk about HikaSerial- - Serial: Bug Report- - Serial: How to?- - Support en Français de HikaSerial (French Support)- - HikaSubscription- HikaAuction- - Auction: Feature requests- - Auction: How to?- - Auction: Bug report Hikashop around the world- Support en Français (French Support)- Russian- - FAQ. Курс выживания с Hikashop. Частые вопросы и о- - Расширения Hikashop- - Наши сайты на Hikashop- 日本語版サポート(Japanese Support)- Thai- German Archives- Bug Report- How to?

[](/forum/topics/feed/mode-topics?format=feed&type=rss)

Time to create page: 0.126 seconds

[Powered by](/forum/credits) [Kunena Forum](https://www.kunena.org)

[](#top)
