Skip to content

PHP API, Hooks & Troubleshooting

Resolve WireMailPHPMailer via $this->wire()->mail->new() (recommended) or $this->wire()->modules->get('WireMailPHPMailer').

ProcessWire\WireMailPHPMailer Method Reference

Section titled “ProcessWire\WireMailPHPMailer Method Reference”
Method Signature Description
init $mail->init(): void Initializes default mail properties and module state.
to $mail->to(string|array|null $email = null, ?string $name = null): static Sets recipient(s); passing null clears to, cc, and bcc.
cc $mail->cc(string|array|null $email = null, ?string $name = null): static Sets or clears CC recipient(s).
bcc $mail->bcc(string|array|null $email = null, ?string $name = null): static Sets or clears BCC recipient(s).
attachment $mail->attachment(string|array|null $value, string $filename = ''): static Attaches validated local file(s); rejects wrappers, null bytes, .., symlinks, and site/config*.php.
set $mail->set(string $key, mixed $value): static Sets standard WireMail keys, cc, bcc, attachments, or PHPMailer overrides.
get $mail->get(string $key): mixed Retrieves a stored property (cc, bcc, attachments, or parent WireMail key).
getArray $mail->getArray(): array Returns all mail properties including cc and bcc.
setMailer $mail->setMailer(?PHPMailer $mailer): static Injects a custom PHPMailer instance or test double.
getInstance $mail->getInstance(bool $initialize = true, bool $exceptions = false): PHPMailer Builds or resets and configures a PHPMailer v7 instance from module config.
redactDsnCredentials WireMailPHPMailer::redactDsnCredentials(string $text): string Scrubs smtp:// / smtps:// userinfo credentials from log/error strings.
___send $mail->send(): int Hookable send method; returns delivered recipient count (0 on failure or $config->demo), resets state in finally.

Totoglu\WireMailPHPMailer\OAuthProvider Methods

Section titled “Totoglu\WireMailPHPMailer\OAuthProvider Methods”
  • label(): string
  • packageName(): string
  • providerClass(): string
  • authorizationOptions(): array
  • isInstalled(): bool
  • hasRequiredCredentials(string $clientId, string $clientSecret): bool
  • createProvider(array $config): ?object

Hookable Methods (WireMailPHPMailer::___*)

Section titled “Hookable Methods (WireMailPHPMailer::___*)”
Hook Arguments & Return Timing What It Does
WireMailPHPMailer::send (): int Before / After Dispatches the prepared email via PHPMailer and returns the delivered recipient count (0 on failure or $config->demo). Hook Before to inject headers or reroute staging recipients, or After to log delivery outcomes.

Inject Global Headers & Log Delivery Outcomes

Section titled “Inject Global Headers & Log Delivery Outcomes”
site/ready.php
<?php
declare(strict_types=1);
namespace ProcessWire;
$wire->addHookBefore('WireMailPHPMailer::send', static function (HookEvent $event): void {
/** @var WireMailPHPMailer $mail */
$mail = $event->object;
$mail->header('X-Environment', 'production');
});
$wire->addHookAfter('WireMailPHPMailer::send', static function (HookEvent $event): void {
/** @var WireMailPHPMailer $mail */
$mail = $event->object;
$sentCount = (int) $event->return;
$event->wire()->log->save(
'mail-delivery',
sprintf('Subject "%s" delivered to %d recipient(s).', (string) $mail->subject, $sentCount)
);
});

Symptom: $mail->send() returns 0 with no SMTP network activity.

Fix:

  1. Check whether $config->demo is true in site/config.php (WireMailPHPMailer blocks delivery in demo mode).
  2. Confirm at least one valid to() recipient and from() address are configured.
  3. Inspect your PHP error_log (default Debugoutput) for the redacted SMTP error message.

Symptom: An expected PDF or CSV attachment is missing from the delivered email.

Fix: Verify the file exists on disk, is not a symlink (!is_link($path)), contains no .. segments or stream wrappers, and does not match site/config*.php.

Next steps