Forum
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!
Are you familiar with what the error means?
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.Have you checked your logs for errors?
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
Ok. I think you're probably the best placed to debug your code - no one else has it in front of them.
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?
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.
As in a new
perch:myrenderer
?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?
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!
Presumably you could query that content and pass it into the email template in the usual way without the need for a layout.
How can I do that for all emails sent out, even stock Perch ones?
You can't. You'd need to find a different solution.
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.
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.
Is that by using
define('PERCH_PROGRESSIVE_FLUSH', false);
?Yes, that's correct. What was the result?
Gives me the same error unfortunately :(
Ok. I don't have a solution for that.
Okay, thanks for your help Drew!