Wordpress Plugin Development: Overriding Default SMTP

I want to override the default SMTP server settings provided by wordpress and they need to use the same SMTP settings in several places in the plugin to send emails.

New SMTP server details will be provided by the user through the form on wp-admin

At the time I was looking for this article I came to phpMailer and class-smtp.php

Possible solutions I could think of 1. Create a global object for the phpmailer class and use it by the plugin
 2. Override the default SMTP server settings of Wordpress
 3. Save the user input in the database and retrieve it when creating the PHPmailer object wherever I have not sent mail.

The problem I am facing with the above solutions is as follows.

1st solution. I cannot figure out how to achieve this.
2nd solution. I couldn't find any resource in the wordpress codex that could explain how to override the default smtp settings.
3rd solution: not productive enough.

Also, I am trying to create a standalone plugin, so I cannot create a dependency on any third party plugin. Although I tried to use the wp-smtp source code , I couldn't figure out how to use the same settings in multiple places.

I am using Tom Mcfarlin Wordpress Boilerplate Plugin ( link ) to create a standard plugin file structure, so if someone can explain me a solution using a template it would be really helpful and efficient.

EDIT:
I download the file structure for better understanding.
enter image description here

This is the form enter image description here

Form values ​​are retrieved successfully in class-atf-admin.php enter image description here

I need to create a global variable in class-atf-admin.php where I will set the values ​​received from the form and use them in the files shown in the above picture.

+3


source to share


1 answer


Mkay, I will have a crack in option 1 (but not 100% on phpmailer naming)

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 variables from end users, add the above code in the shortcode and maybe insert smtp data through the shortcode. eg,

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

+2


source







All Articles