Reputation: 3167
Currently I am facing a problem to overwrite the body content of the mail_2
in the hook wpcf7_before_send_mail
.
I am trying to rewrite a wpcf7 shortcode to a more proper presentation of the content with some html modifications.
The problem is that I can't overwrite the mail body. After I rewrite the body and print directly the result it seems to be back as the default body. When I print my modification directly, I see the modifications as I want them to be.
Example of code:
add_action("wpcf7_before_send_mail", "cf7_wpcf7_custom", 99, 1);
# Before sending email
function cf7_wpcf7_custom($properties, $contact_form_obj) {
$submission = WPCF7_Submission::get_instance();
$wpcf7 = WPCF7_ContactForm::get_current();
$wpcf7->mail_2['body'] = str_replace('[checkbox-1]', '..test..', $wpcf7->mail_2['body']);
// No changes appearing
var_dump($wpcf7->mail_2['body']);
exit;
}
Upvotes: 2
Views: 3627
Reputation: 111
I was able to change Email 1 and 2 with following code in functions.php and add some self inserted code:
add_action( 'wpcf7_before_send_mail', 'wpcf7_add_couponnr_to_mail_body' );
function wpcf7_add_couponnr_to_mail_body($contact_form){
// get mail property
$mail = $contact_form->prop( 'mail' ); // returns array with mail values
$mail2 = $contact_form->prop( 'mail_2' ); // returns array with mail values
$couponnr = ...some insert code...;
// add date (or other content) to email body
$mail['body'] = str_replace("%%%COUPONNR%%%", $couponnr, $mail['body']);
$mail2['body'] = str_replace("%%%COUPONNR%%%", $couponnr, $mail2['body']);
// set mail property with changed value(s)
$contact_form->set_properties( array( 'mail' => $mail ) );
$contact_form->set_properties( array( 'mail_2' => $mail2 ) );
}
Upvotes: 1
Reputation: 3167
The wpcf7_before_send_mail
hook is performed before the additional mail is set. So it will be overwritten.
We can modify the mail body by using the filter wpcf7_additional_mail
.
For example:
add_filter('wpcf7_additional_mail', 'customize_mail_2', 10, 2);
function customize_mail_2($additional_mail, $contact_form) {
$submission = WPCF7_Submission::get_instance();
$wpcf7 = WPCF7_ContactForm::get_current();
$additional_mail['mail_2']['body'] = '..do your replacement stuff';
return $additional_mail;
}
Upvotes: 2