Italian (Italy)
Knowledge Base  >  Vik Restaurants  >  Hooks  >  E-Mail  >  Before Send

do_action( 'vikrestaurants_before_send_mail', Mail $mailmixed $transporter, object $options )

Fires before VikRestaurants tries to deliver an e-mail.


Description

It is possible to use this hook to alter the mail that is going to be sent as well as the configuration of the transporter.

Here it is also possible to throw an exception to abort the mail delivery. In this case, you might want to avoid breaking the process, thing that you can accomplish by using the code below before throwing the error.

$options->set('silent', true);

Parameters

$mail

(Mail)  Encapsulates the e-mail information. This class is part of the E4J\VikRestaurants\Mail namespace.

$transporter

(mixed)  The instance used by the CMS to dispatch e-mail notifications.

$options

(object)  A configuration registry. Only the following attributes are used in this context.

  • silent (bool) - True to ignore any thrown exception and go ahead without breaking the process (false by default).
  • admin (bool) - True to enqueue a system message in case of errors (false by default). Applies only in case of silent delivery.

Example

The example below explains how to alter the mailing information.

/**
 * Fires before delivering an e-mail.
 * 
 * @param  Mail    $mail         The e-mail information.
 * @param  mixed   $transporter  The e-mail transporter.
 * @param  object  $options      The configuration registry.
 * 
 * @since  1.3
 */
add_action('vikrestaurants_before_send_mail', function($mail, $transporter, $options) {
    // overwrite the sender e-mail and name
    $mail->setSender('noreply@domain.com', 'John Smith');

    // register a new recipient e-mail address
    $mail->addRecipient('secretary@domain.com', 'Secretary');

    // overwrite the reply-to email address
    $mail->setReplyTo('info@domain.com');

    // prepend something to the existing subject
    $mail->setSubject('Company Name - ' . $mail->getSubject());

    // add a footer to the body
    $mail->setBody($mail->getBody() . "\n<p style=\"text-align: center;\">All rights reserved.</p>");
    // and force it to use HTML tags
    $mail->setHtml(true);

    // include an attachment for the recipients
    $mail->addAttachment(ABSPATH . 'wp-content/uploads/vikrestaurants/documents/file.doc');
}, 10, 3);

Changelog

Version Description
1.3 Introduced.
Ultimo aggiornamento: 2023-12-20
Utile?
Potrebbe interessarti: