English (United Kingdom)
Knowledge Base  >  Vik Booking  >  For Developers  >  Manipulate Email Messages

apply_filters( 'vikbooking_before_send_booking_mail', string $recipient, array $bookingVBOMailWrapper $mail_obj )

Fires before sending an email message to the administrator or to the guest/customer.


Description

Plugins can use this filter to manipulate at runtime the whole message object before the email message actually gets sent.

It is possible to manipulate the email messages for the administrator, as well as the ones for the guest. Thanks to the mail wrapper object native of Vik Booking, it is possible to manipulate literally anything, from the subject to attachments, BCC addresses, content etc..


Parameters

$recipient

(string)  Identifies the type of recipient. Possible values are "admin", "guest", "customer" or an email address.

$booking

(array)  The associative array containing the booking information for which the email message is being sent.

$mail_obj

(object)  The instance of the VBOMailWrapper object indicating the email message. Its methods should be used for manipulation.

This is how the hook will fire. In order to manipulate (modify) data about the email message (subject, content, recipient, BCC, attachments etc..) it is necessary to use the methods of the provided mail wrapper object.


Example

The example below shows how to quickly manipulate the email message depending on who is the recipient, either the guest or the property manager (administrator).

/**
 * Triggers an action to let plugins manipulate the email message before it gets sent.
 * 
 * @param   string  $recipient  the string that identifies the recipient type. Possible values are:
 *                              admin, guest, customer or an email address.
 * @param   array   $booking    the booking array for which the message is being sent.
 * @param   object  $mail_obj   an instance of the VBOMailWrapper object to manipulate the message.
 * 
 * @see     VBOMailWrapper to get a list of the available methods to manipulate the message.
 */
add_filter('vikbooking_before_send_booking_mail', function($recipient, $booking, $mail_obj)
{
    // determine who is the recipient
    if (stripos($recipient, 'admin') !== false) {
        // the email message is for the administrator of the website (the property manager)

        // access the current mail subject
        $subject = $mail_obj->getSubject();

        // if you need to manipulate the subject for the admin, do it like this (do nothing otherwise):
        $mail_obj->setSubject($subject . ' modified for the Admin');
    } else {
        // the email message is for the guest, or for a custom email address of a guest/customer

        // access the current mail subject
        $subject = $mail_obj->getSubject();

        // if you need to manipulate the subject for the admin, do it like this (do nothing otherwise):
        $mail_obj->setSubject($subject . ' modified for the guest');
    }

    // if you need to manipulate the message depending on the booking details, debug the array $booking to see its content.

    // another useful method for manipulating the content is:
    $content = $mail_obj->getContent();
    $mail_obj->setContent($content);

    // get a list (array) of attachments
    $attachments = $mail_obj->getAttachments();
    // the method setAttachments() would override the file(s) to be attached

    // new BCC addresses could be set through setBcc()

    // since this is an object, there is no need to return anything. The methods invoked will manipulate the email message.
}, 10, 3);

Changelog

Version Description
1.5 Introduced.

Last Update: 2023-09-08
Helpful?
100% of people found this helpful.
This site uses cookies. By continuing to browse you accept their use. Further information