Skip to content

Commit

Permalink
Setup custom SMTP credentails by default
Browse files Browse the repository at this point in the history
  • Loading branch information
vinkla committed Oct 9, 2023
1 parent e1b0773 commit e8a0d26
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 38 deletions.
8 changes: 8 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ DB_USER=root
DB_PASSWORD=
DB_TABLE_PREFIX=wp_

MAIL_HOST=127.0.0.1
MAIL_PORT=1025
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
MAIL_FROM_ADDRESS="hello@example.com"
MAIL_FROM_NAME="Example"

# https://vinkla.github.io/salts/
AUTH_KEY=
SECURE_AUTH_KEY=
Expand Down
47 changes: 9 additions & 38 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,49 +189,20 @@ npm run build

## Mail

To configure custom SMTP credentials for sending emails in your WordPlate application, add the following code to your `functions.php` file.

```php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;

// Register SMTP email with HTML support.
add_action('phpmailer_init', function (PHPMailer $mail) {
$mail->isSMTP();
$mail->SMTPAutoTLS = false;
$mail->SMTPAuth = env('MAIL_USERNAME') && env('MAIL_PASSWORD');
$mail->SMTPDebug = env('WP_DEBUG') ? SMTP::DEBUG_SERVER : SMTP::DEBUG_OFF;
$mail->SMTPSecure = env('MAIL_ENCRYPTION', 'tls');
$mail->Debugoutput = 'error_log';
$mail->Host = env('MAIL_HOST');
$mail->Port = env('MAIL_PORT', 587);
$mail->Username = env('MAIL_USERNAME');
$mail->Password = env('MAIL_PASSWORD');
return $mail;
});

add_filter('wp_mail_content_type', fn () => 'text/html');
add_filter('wp_mail_from_name', fn () => env('MAIL_FROM_NAME', 'Example'));
add_filter('wp_mail_from', fn () => env('MAIL_FROM_ADDRESS', 'hello@example.com'));
```

Next, you'll need to add the necessary environment variables to your `.env` file:

```
MAIL_FROM_ADDRESS=
MAIL_FROM_NAME=
MAIL_HOST=
MAIL_PASSWORD=
MAIL_PORT=
MAIL_USERNAME=
```

If you're using a local email service like [Mailpit](https://github.com/axllent/mailpit), you may need to disable encryption by setting the `MAIL_ENCRYPTION` environment variable to `null`:
To set up custom SMTP credentials for sending emails in your WordPlate application, you need to configure the required environment variables in your `.env` file.

```
MAIL_HOST=127.0.0.1
MAIL_PORT=1025
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
MAIL_FROM_ADDRESS="hello@example.com"
MAIL_FROM_NAME="Example"
```

If you're using a local email service like [Mailpit](https://github.com/axllent/mailpit), you need to disable encryption by setting the `MAIL_ENCRYPTION` environment variable to `null`.

## FAQ

<details>
Expand Down
21 changes: 21 additions & 0 deletions public/themes/wordplate/functions.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<?php

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;

// Register theme defaults.
add_action('after_setup_theme', function () {
show_admin_bar(false);
Expand Down Expand Up @@ -90,3 +93,21 @@
implode(';', $styles)
);
});

// Setup custom SMTP credentials.
add_action('phpmailer_init', function (PHPMailer $mailer) {
$mailer->isSMTP();
$mailer->SMTPAutoTLS = false;
$mailer->SMTPAuth = env('MAIL_USERNAME') && env('MAIL_PASSWORD');
$mailer->SMTPDebug = env('WP_DEBUG') ? SMTP::DEBUG_SERVER : SMTP::DEBUG_OFF;
$mailer->SMTPSecure = env('MAIL_ENCRYPTION', 'tls');
$mailer->Debugoutput = 'error_log';
$mailer->Host = env('MAIL_HOST');
$mailer->Port = env('MAIL_PORT', 587);
$mailer->Username = env('MAIL_USERNAME');
$mailer->Password = env('MAIL_PASSWORD');
return $mailer;
});

add_filter('wp_mail_from', fn () => env('MAIL_FROM_ADDRESS', 'hello@example.com'));
add_filter('wp_mail_from_name', fn () => env('MAIL_FROM_NAME', 'Example'));

0 comments on commit e8a0d26

Please sign in to comment.