Forum

Thread tagged as: Question, Third-party, Shop

PerchEmail API and Events

Hi,

I'm trying to send an email out upon an event fire, like so:

$Email = $API->get('Email'); $Email->set_template('emails/shop_promo.html'); $Email->set_bulk($data); $Email->senderName(PERCH_EMAIL_FROM_NAME); $Email->senderEmail(PERCH_EMAIL_FROM); $Email->recipientEmail(perch_member_get("email")); $Email->send();

Using $API->on('members.order_status_update', function(PerchSystemEvent $Event) use ($API) { etc, but get an error:

Warning: Cannot modify header information - headers already sent in /admin/core/lib/PerchUtil.class.php on line 233. I'm fairly sure it's to do with the email template render but can't figure out how to fix it. Looked into ob_start() etc but can't find a fix!

As always, help is super appreciated, thank you!

Ryan Gittings

Ryan Gittings 1 points

  • 3 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

Are you familiar with what the error means?

Ryan Gittings

Ryan Gittings 1 points
Registered Developer

From what I can gather headers are already sent, or content has already been outputted before the redirect. I narrowed it down to the email HTML using headers_sent then tried to work backwards, but that's as far as I've got.

Drew McLellan

Drew McLellan 2638 points
Perch Support

Have you checked your logs for errors?

Ryan Gittings

Ryan Gittings 1 points
Registered Developer

The only error it reports is:

[15-Nov-2017 16:40:52 UTC] PHP Warning: Cannot modify header information - headers already sent in /admin/core/lib/PerchUtil.class.php on line 233

Drew McLellan

Drew McLellan 2638 points
Perch Support

Ok. I think you're probably the best placed to debug your code - no one else has it in front of them.

Ryan Gittings

Ryan Gittings 1 points
Registered Developer

Narrowed it down, <perch:layout path="email/featured" /> which has a region to pull custom content to an email footer is causing it.

Any advice here?

Drew McLellan

Drew McLellan 2638 points
Perch Support

That's not really suitable to use in an email because it will attempt to flush the output - it's designed for HTTP responses.

If you really need to use layouts in your email template, you'll need to specify a custom layout renderer.

Ryan Gittings

Ryan Gittings 1 points
Registered Developer

As in a new perch:myrenderer?

Drew McLellan

Drew McLellan 2638 points
Perch Support

No, you'd need to configure the PerchTemplate class with a custom layout renderer. However, thinking about it more I don't think you get the opportunity to do that in this instance.

Do you really need layouts in an email template?

Ryan Gittings

Ryan Gittings 1 points
Registered Developer

Ah, okay.

It's only so my client can feature things in email footers for promotions and exclusives. It was part of the original spec so I do need them really!

Drew McLellan

Drew McLellan 2638 points
Perch Support

Presumably you could query that content and pass it into the email template in the usual way without the need for a layout.

Ryan Gittings

Ryan Gittings 1 points
Registered Developer

How can I do that for all emails sent out, even stock Perch ones?

Drew McLellan

Drew McLellan 2638 points
Perch Support

You can't. You'd need to find a different solution.

Ryan Gittings

Ryan Gittings 1 points
Registered Developer

Yeah that's my issue, is using perch:layout not possible technically, or is it just a lot of work to rework it so it doesn't cause the header issue?

perch:layout seems to most logical solution, but appreciate it's quite a niche issue.

Drew McLellan

Drew McLellan 2638 points
Perch Support

Have you confirmed if it's still an issue if you turn output flushing off? I think it's going to be one or the other.

Ryan Gittings

Ryan Gittings 1 points
Registered Developer

Is that by using define('PERCH_PROGRESSIVE_FLUSH', false);?

Drew McLellan

Drew McLellan 2638 points
Perch Support

Yes, that's correct. What was the result?

Ryan Gittings

Ryan Gittings 1 points
Registered Developer

Gives me the same error unfortunately :(

Drew McLellan

Drew McLellan 2638 points
Perch Support

Ok. I don't have a solution for that.

Ryan Gittings

Ryan Gittings 1 points
Registered Developer

Okay, thanks for your help Drew!