$errors[] = sprintf( /* translators: %1$s - namespaced class name, %2$s - invalid email. */
esc_html__( '%1$s Invalid email address %2$s.', 'wpforms-lite' ),
'[WPForms\Emails\Mailer]',
if ( empty( $this->get_subject() ) ) {
$errors[] = sprintf( /* translators: %s - namespaced class name. */
esc_html__( '%s Empty subject line.', 'wpforms-lite' ),
'[WPForms\Emails\Mailer]'
if ( empty( $this->get_message() ) ) {
$errors[] = sprintf( /* translators: %s - namespaced class name. */
esc_html__( '%s Empty message.', 'wpforms-lite' ),
'[WPForms\Emails\Mailer]'
* Log given email errors.
* @param array $errors Errors to log.
protected function log_errors( $errors ): void {
if ( empty( $errors ) || ! is_array( $errors ) ) {
foreach ( $errors as $error ) {
'to_email' => $this->to_email,
'subject' => $this->subject,
'message' => wp_trim_words( $this->get_message() ),
if ( ! did_action( 'init' ) && ! did_action( 'admin_init' ) ) {
_doing_it_wrong( __FUNCTION__, esc_html__( 'You cannot send emails with WPForms\Emails\Mailer until init/admin_init has been reached.', 'wpforms-lite' ), null );
// Don't send anything if emails have been disabled.
if ( $this->is_email_disabled() ) {
$errors = $this->get_errors();
$this->log_errors( $errors );
* Add filters / actions before the email is sent.
public function send_before(): void { // phpcs:ignore WPForms.PHP.HooksMethod.InvalidPlaceForAddingHooks
// phpcs:ignore WPForms.Comments.PHPDocHooks.RequiredHookDocumentation
do_action( 'wpforms_emails_mailer_send_before', $this );
add_filter( 'wp_mail_from', [ $this, 'get_from_address' ] );
add_filter( 'wp_mail_from_name', [ $this, 'get_from_name' ] );
add_filter( 'wp_mail_content_type', [ $this, 'get_content_type' ] );
* Remove filters / actions after the email is sent.
public function send_after(): void { // phpcs:ignore WPForms.PHP.HooksMethod.InvalidPlaceForAddingHooks
// phpcs:ignore WPForms.Comments.PHPDocHooks.RequiredHookDocumentation
do_action( 'wpforms_emails_mailer_send_after', $this );
remove_filter( 'wp_mail_from', [ $this, 'get_from_address' ] );
remove_filter( 'wp_mail_from_name', [ $this, 'get_from_name' ] );
remove_filter( 'wp_mail_content_type', [ $this, 'get_content_type' ] );