Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
[WordPress] An example of how to specify a custom SMTP server when using the wp_mail function.
<?php
class Acme_Email {
public function __construct() {
add_action( 'phpmailer_init', array( $this, 'configure_smtp' ) );
add_filter( 'wp_mail_content_type', create_function( '', 'return "text/html";' ) );
add_filter( 'wp_mail_from', array( $this, 'from_email' ) );
add_filter( 'wp_mail_from_name', array( $this, 'from_name' ) );
}
/**
* @param string $message The HTML document to send as the message.
*/
public function send( $message ) {
wp_mail( 'everyone@acme.com', 'Your Weekly Newsletter', $message );
}
/**
* @return string The email address from which the newsletter is being sent.
*/
public function from_email() {
return 'newsletter@tubular.com';
}
/**
* @return string The name from which the newsletter is being sent.
*/
public function from_name() {
return "Your Bodacious Newsletter";
}
/**
* @param PHPMailer $phpmailer A reference to the current instance of PHP Mailer
*/
private function configure_smtp( $phpmailer ){
$phpmailer->isSMTP();
$phpmailer->Host = 'smtp.acme-server.com';
$phpmailer->SMTPAuth = TRUE;
$phpmailer->Port = 25;
$phpmailer->Username = 'server@acme.com';
$phpmailer->Password = 'acme-acne';
$phpmailer->SMTPSecure = FALSE;
}
}
<?php
class Acme_Email {
public function __construct() {
add_action( 'phpmailer_init', array( $this, 'configure_smtp' ) );
add_filter( 'wp_mail_content_type', create_function( '', 'return "text/html";' ) );
add_filter( 'wp_mail_from', array( $this, 'from_email' ) );
add_filter( 'wp_mail_from_name', array( $this, 'from_name' ) );
}
}
<?php
/**
* @return string The email address from which the newsletter is being sent.
*/
public function from_email() {
return 'newsletter@tubular.com';
}
/**
* @return string The name from which the newsletter is being sent.
*/
public function from_name() {
return "Your Bodacious Newsletter";
}
/**
* @param PHPMailer $phpmailer A reference to the current instance of PHP Mailer
*/
private function configure_smtp( $phpmailer ){
$phpmailer->isSMTP();
$phpmailer->Host = 'smtp.acme-server.com';
$phpmailer->SMTPAuth = TRUE;
$phpmailer->Port = 25;
$phpmailer->Username = 'server@acme.com';
$phpmailer->Password = 'acme-acne';
$phpmailer->SMTPSecure = FALSE;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.