WordPress Plugin Development: Override Default SMTP

I want to override the default SMTP server settings provided by wordpress and have to use the same SMTP settings at multiple places in the plugin to send mails.

The new SMTP server details would be provided by the user, through a form on wp-admin

Read More

While googling about it, I came accross phpMailer, and class-smtp.php

The possible solutions that I could think of
1. Create a global object for phpmailer class, and use it accross the plugin
2. Override the default wordpress SMTP server settings
3. Save the settings entered by the user in database and retrieve it while creating a PHPmailer object wherever I have to send mail.

The problem I am facing with the above solutions are..

1st Solution. I am not able to figure out how to achieve it.
2nd Solution: I could not find any resource on wordpress codex, that could explain on how to override default smtp settings.
3rd Solution: is not productive enough.

Also, I am trying to create a standalone plugin, so cannot create a dependency on any third party plugin. Though I have tried going through the source code of wp-smtp, but could not figure out how to use the same settings at multiple places.

I am using Tom Mcfarlin’s WordPress Boilerplate Plugin (link), in order to create a standard plugin file structure, so if someone can explain me a solution using the boilerplate, it would be really be useful and efficient.

EDITS:
I am uploading the file structure for better understanding.

enter image description here

This is the form
enter image description here

The form values are succesfully retrieved at class-atf-admin.php
enter image description here

I need to create a global variable in class-atf-admin.php, where I’ll set the values received from the form and use it at files shown in the above figure.

Related posts

Leave a Reply

1 comment

  1. Mkay, I am going to have a crack at option 1 (not 100% on the phpmailer naming though)

    In your main plugin file, Based on this example code

    <?php    
    require_once 'phpmailer/PHPMailerAutoload.php';
    
    $mail             = new PHPMailer();
    
    $body             = file_get_contents('contents.html');
    
    $mail->IsSMTP(); // telling the class to use SMTP
    $mail->Host       = "mail.yourdomain.com"; // SMTP server
    $mail->SMTPDebug  = 2;                     // enables SMTP debug information (for testing)
                                               // 1 = errors and messages
                                               // 2 = messages only
    $mail->SMTPAuth   = true;                  // enable SMTP authentication
    $mail->Host       = "mail.yourdomain.com"; // sets the SMTP server
    $mail->Port       = 26;                    // set the SMTP port for the GMAIL server
    $mail->Username   = "yourname@yourdomain"; // SMTP account username
    $mail->Password   = "yourpassword";        // SMTP account password
    
    $mail->SetFrom('name@yourdomain.com', 'First Last');
    
    $mail->AddReplyTo("name@yourdomain.com","First Last");
    
    $mail->Subject    = "PHPMailer Test Subject via smtp, basic with authentication";
    
    $mail->MsgHTML($body);
    
    $address = "whoto@otherdomain.com";
    $mail->AddAddress($address, "John Doe");
    
    $mail->AddAttachment("images/phpmailer.gif");      // attachment
    $mail->AddAttachment("images/phpmailer_mini.gif"); // attachment
    
    if(!$mail->Send()) {
      echo "Mailer Error: " . $mail->ErrorInfo;
    } else {
      echo "Message sent!";
    }
    }
    

    For the variables from the users end, add the above to a shortcode and make insert the smtp details via shortcode perhaps. eg

    [sendMail host="mail.yourdomain.com" port="26" username="yourname@yourdomain" password="yourpassword"]