Support » Fixing WordPress » pragmatic way to setup SMTP settings programmatically with the wp_mail function?

  • say_hello

    (@say_hello)


    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', 'wpse9498170_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 the happiest man on earth – i love WordPress for its flexibility.
    and its worldwide community šŸ˜‰

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator Steve Stern

    (@sterndata)

    Support Team Volunteer

    What I’d do in your situation is to look at the code of several of the SMTP plugins and extract what I need into my own plugin.

    It appears you’re currently copying code from Stack Exchange. If you have a question about something from there, it’s probably best to ask in the thread there.

    say_hello

    (@say_hello)

    hello dear Steve,

    thx for the quick reply. ATM i want to dive into the basic-process of getting the first steps done with the wp_mail function.

    you write:

    What Iā€™d do in your situation is to look at the code of several of the SMTP plugins and extract what I need into my own plugin.

    i will do so. Many thanks for your hint. You have helped me so often – im glad!!!

    I will try to find a pathway. Many thanks for the answer…

    Many thanks for your support.
    i am very glad to be here at this great place.

    Have a great day
    regards

Viewing 2 replies - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.