Jump to content

Sending email on page update


fuzendesign
 Share

Recommended Posts

I want to send an email when a page is published or updated. Specifically a template (temp_order_detail). For now, I just want to get the page-update code working. Why does the following code not send an email? I’ve tried using a from address of both my PW admin address and one tied to the domain the site resides on. Neither addresses work. Is there a whitelist in Processwire for valid emails to send from?

I have this code in site > ready.php file:

wire()->addHookAfter('Pages(template=temp_order_detail)::published', function(HookEvent $event) {
    // Get the object the event occurred on, if needed
    $pages = $event->object;

    // Get values of arguments sent to hook (if needed)
    $page = $event->arguments(0); // your code: $page = $event->arguments[0];

        $mail->new();
				$mail->subject("Your order ".$page->title."has been submitted...");
				$mail->to("testemail@gmail.com");
				$mail->from("emailaccount@mydomain.com");

				$mail = bodyHTML("<html><body><p>Your delivery order has been created.</p><p>Blah blah blah ".$page->title."</p></body></html>");
				
				$mail->send();

});

 

Link to comment
Share on other sites

Are you sure that's valid (marked in bold)?

wire()->addHookAfter('Pages(template=temp_order_detail)::published', function(HookEvent $event) { // }

I go with:

wire()->addHookAfter('Page::published', function(HookEvent $event) { // }

See here: https://github.com/processwire/processwire/blob/d78276e2c265f6b70384a13eb4febd4811a1db77/wire/core/Pages.php#L2197

Link to comment
Share on other sites

Assuming I go with your code, just to test and using PW docs code—without specifying a template—my email still doesn’t fire on even the most basic example:

(My understanding is that this should fire on any page being published on the site. I want to narrow it down to just a specific template, but reducing things to the most basic, can’t figure why this won't work…and yes, I tried various combinations of this code and other for the last few hours.)

wire()->addHookAfter('Page::published', function(HookEvent $event) {
    // Get the object the event occurred on, if needed
    $pages = $event->object;

    // Get values of arguments sent to hook (if needed)
    $page = $event->arguments(0); // your code: $page = $event->arguments[0];

        $message = $mail->new();
        $message->subject('Hello world')
            ->to('externalemail@gmail.com')
            ->from('email@mysitedomain.com')
            ->body('Hello there big world')
            ->bodyHTML('<h2>Hello there big world</h2>');
        $numSent = $message->send();

});

 

Link to comment
Share on other sites

22 minutes ago, wbmnfktr said:

Are you sure that's valid (marked in bold)?

wire()->addHookAfter('Pages(template=temp_order_detail)::published', function(HookEvent $event) { // }

I could have sworn I grabbed that from code Ryan or an experienced developer here had use in answering someone’s question.

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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...