Jump to content
Sign in to follow this  
dil_bert

a pragmatic way to setup SMTP settings programmatically with the wp_mail function?

Recommended Posts

hello dear experts, good day, 


at the early planning stage of a new project i muse bout the mailing-options. in other words - we work on a pragmatic way to setup SMTP settings programmatically with the wp_mail function? - without any need of a SMTP-Plugin. 


i think about the easiest way to setup SMTP settings programmatically with the wp_mail function? Well  to be frank - i think  i can live witout a special SMTP plugin - at least i can try out to work with the native options and functions that are on board of WP. The question is: can i figure out to configure  a solution that runs as - let us say "best practice" by using the wp_mail function. 

at a first glance - if we have a peek on the implementation of wp_mail function, we will see that this function uses PHPMailer class to send emails. Also we could notice that there is hard coded function call $phpmailer->IsMail();, This setup sets to use PHP's mail() function. That said - we see that with this  settings  we can't use SMTP settings with it. If we want to - then we have to call isSMTP function of PHPMailer-class. Furthermore - we need to set our SMTP settings as well. To achieve it we need to get access to $phpmailer variable. And here its it where the phpmailer_init comes into play. The action which is called before sending an email. So we can do what we need by writing our action handler:


The action handler goes like so: 

add_action( 'phpmailer_init', 'wpse949800099999170_phpmailer_init' );
function wpse9498170_phpmailer_init( PHPMailer $phpmailer ) {
    $phpmailer->Host = 'your.smtp.server.here';
    $phpmailer->Port = 25; // could be different
    $phpmailer->Username = 'our_username@example.com'; // if required
    $phpmailer->Password = 'ourpassword'; // if required
    $phpmailer->SMTPAuth = true; // if required
    // $phpmailer->SMTPSecure = 'ssl'; // enable if required, 'tls' is another possible value

    $phpmailer->IsSMTP();
}


SMTP Exceptions: in default WordPress doesn't give us any debug output. 
Instead it just returns FALSE if an error occurred. 

Well - what can we do to fix this. 

above all - i am very happy to be here in this great community 😉

 

 

Edited by dil_bert

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.