Skip to content

Instantly share code, notes, and snippets.

@atomjoy
Last active September 23, 2024 10:49
Show Gist options
  • Save atomjoy/0696794c17b3bfa12da5df49e15553c0 to your computer and use it in GitHub Desktop.
Save atomjoy/0696794c17b3bfa12da5df49e15553c0 to your computer and use it in GitHub Desktop.
How to set Gmail smtp settings in WordPress.
<?php
// Create 16-digit password for google account
// https://myaccount.google.com/apppasswords
// Ustawienia SMTP email: wp-config.php
define('SMTP_username', '[email protected]'); // wpisz swój adres e-mail dla WordPress
define('SMTP_password', 'twoje-haslo'); // tu podaje swoje hasło Gmail
define('SMTP_server', 'smtp.gmail.com'); // tu podaj swój host serwera poczty
define('SMTP_FROM', '[email protected]'); // wpisz swój adres e-mail dla WordPress
define('SMTP_NAME', 'Biuro'); // tu podaj np. swoje imie
define('SMTP_PORT', '587'); // tu podaj numer portu np. 465 dla SSL lun 587 dla tls
define('SMTP_SECURE', 'tls'); // Szyfrowanie SSL lub TLS
define('SMTP_AUTH', true); // Uwierzytelnienie (true|false)
define('SMTP_DEBUG',0); // dla debugowania błędów 0/1/2
// Php mailer settings: functions.php
function my_phpmailer_smtp( $phpmailer ) {
$phpmailer->isSMTP();
$phpmailer->CharSet = "utf-8";
$phpmailer->Host = SMTP_server;
$phpmailer->Username = SMTP_username;
$phpmailer->Password = SMTP_password;
$phpmailer->SMTPAuth = SMTP_AUTH;
$phpmailer->Port = SMTP_PORT;
$phpmailer->SMTPSecure = SMTP_SECURE;
$phpmailer->From = SMTP_FROM;
$phpmailer->FromName = SMTP_NAME;
// $phpmailer->SetFrom(get_option('admin_email'), 'Administrator');
}
add_action('phpmailer_init', 'my_phpmailer_smtp');
// Send email: functions.php
function subscribeUser($to) {
$subject = 'Subscription confirmation.';
$headers = [
'Content-Type: text/html; charset=UTF-8',
// 'Reply-To: ' . get_option('admin_email'),
// Custom sender or get from PHPmailer settimgs
// 'From: Newsletter <' . get_option('admin_email') . '>',
];
// Html body
$body = 'Confirm your email address! <a href="' . site_url('/') . 'confirm/email/' . $to . '">Confirm Email</a>';
// Or html template
// ob_start();
// // include 'templates/emails/confirm-html.php';
// include get_template_directory() . '/templates/emails/confirm-html.php';
// $body = ob_get_clean();
// Send email
wp_mail($to, $subject, $body, $headers);
}
// Catch wp_mail errors
add_action('wp_mail_failed', function ($error) {
wp_send_json_error('Send mail error.', 401);
// wp_die("<pre>".print_r($error, true)."</pre>");
});
// Force html email wp_mail
add_filter('wp_mail_content_type', function () {
return "text/html";
});
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Confirm</title>
<style>
.btn {
color: #2244ff;
font-size: 20px;
text-decoration: none;
border: 1px solid #2244ff;
padding: 10px 25px;
border-radius: 10px;
}
</style>
</head>
<body>
<h1>Welcome!</h1>
<p>Confirm your email address.</p>
<center>
<a href="<?php echo site_url('/') . 'confirm/email/' . $to; ?>" class="btn">Confirm Email</a>
</center>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment