Code Space Services

You can contact us at:

Buddhanagar
Kathmandu, Nepal
9818500185 /
Sun-Thurs 10:00am-6:00pm
[email protected]
24 X 7 online support
ShareThis:
Comments
Send Email From Multiple SMTP in WordPress
Send Email From Multiple SMTP in WordPress © Code Space Services

Send Email From Multiple SMTP in WordPress

Sending a transactional email or newsletter to the members has always been the problem if you are in low cost shared hosting. You can now use the multiple SMTP servers to send emails breaking the limit of the shared server for your WordPress blog without using any plugin and it is also considered to be the safest and secured method of sending a message either it is a transactional email or newsletter.

In this tutorial, we will edit two files to make the system randomly pick the SMTP server on each refresh so that all the free servers limitation can be ignored as well.

We have also created a post regarding the list of the free SMTP server in this site. You can access the free list here: Free SMTP Relay Server List. Though the list is not complete, we are testing all the services individually and you can also help us grow the list if we are missing some awesome server.

Tags : How To , Tutorial , WordPress , SMTP

Expertise Level: Basic

Here are the list of items required to complete this tutorial.

  • SMTP Server account
  • Hosting
  • FTP Client (Filezilla preferred)
  • Text Editor (Notepad++ preferred)
1

Open functions.php

Open functions.php

Open your FTP client and access the theme folder of your WordPress Blog which is located in wp-content/themes/YouR Active Theme. Right-click in the functions.php and click select View/Edit.

Ads

Sponsor
2

Add code in functions.php

Add code in functions.php

Add these code at the end of functions.php file using your code editor.

add_action( 'phpmailer_init', 'send_smtp_email' );
function send_smtp_email( $phpmailer ) {
    $phpmailer->isSMTP();
    $phpmailer->Host       = SMTP_HOST;
    $phpmailer->SMTPAuth   = SMTP_AUTH;
    $phpmailer->Port       = SMTP_PORT;
    $phpmailer->Username   = SMTP_USER;
    $phpmailer->Password   = SMTP_PASS;
    $phpmailer->SMTPSecure = SMTP_SECURE;
    $phpmailer->From       = SMTP_FROM;
    $phpmailer->FromName   = SMTP_NAME;
}

Save the file and upload it to the server.

3

Open wp-config.php

Open wp-config.php

Access the root of the site where you will find the wp-config.php file. Right-click in the wp-config.php and click select View/Edit.

Ads

Sponsor
4

Add code in wp-config.php

Add code in wp-config.php

Add the line of codes in your wp-config.php just before /* That's all, stop editing! Happy blogging. */

 $servers = [
    [
        'host'   => 'smtp.example1.com',    // Address of the SMTP server.
        'port'     => '587',    // Port of the SMTP server. Options may be '25', '465', '587' or '2525'.
        'secure'   => 'tls',    // encryption to use on the SMTP connection. Valid options are '', 'ssl' or 'tls'.

        'authentication_required'   => 'true',    // set this to 'false' if your SMTP server does not require authentication
        // If 'authentication_required' above is set to 'true', the following credentials will be required
        'name'   => 'Your Name', // SMTP From name
        'username' => '[email protected]', // Username to use for SMTP authentication
        'password' => 'password', // Password to use for SMTP authentication
    ],
   [
        'host'   => 'smtp.example2.com',    // Address of the SMTP server.
        'port'     => '465',    // Port of the SMTP server. Options may be '25', '465', '587' or '2525'.
        'secure'   => 'ssl',    // encryption to use on the SMTP connection. Valid options are '', 'ssl' or 'tls'.
        'authentication_required'   => 'true',    // set this to 'false' if your SMTP server does not require authentication
        // If 'authentication_required' above is set to 'true', the following credentials will be required
        'name'   => 'Your Name', // SMTP From name
        'username' => '[email protected]', // Username to use for SMTP authentication
        'password' => 'password', // Password to use for SMTP authentication

    ],

    [
        'host'   => 'smtp.example3.com',    // Address of the SMTP server.
        'port'     => '2525',    // Port of the SMTP server. Options may be '25', '465', '587' or '2525'.
        'secure'   => '',    // encryption to use on the SMTP connection. Valid options are '', 'ssl' or 'tls'.
        'authentication_required'   => 'true',    // set this to 'false' if your SMTP server does not require authentication
        // If 'authentication_required' above is set to 'true', the following credentials will be required
        'name'   => 'Your Name', // SMTP From name
        'username' => '[email protected]', // Username to use for SMTP authentication
        'password' => 'password', // Password to use for SMTP authentication
    ],
];
$server = $servers[array_rand($servers)]; //Picks a random server (or however you want to select a server)
define( 'SMTP_FROM',   '[email protected]' );

define( 'SMTP_TO',   '[email protected],[email protected],[email protected]' );             // SMTP From email address. This can be static. 
define( 'SMTP_USER',   $server['username']);                // Username to use for SMTP authentication
define( 'SMTP_PASS',   $server['password']);                   // Password to use for SMTP authentication
define( 'SMTP_HOST',   $server['host']);                    // The hostname of the mail server
define( 'SMTP_NAME',   $server['name']);                    // SMTP From name
define( 'SMTP_PORT',   $server['port']);                    // SMTP port number
define( 'SMTP_SECURE', $server['secure']);                  // Encryption system
define( 'SMTP_AUTH',   $server['authentication_required']); // Use SMTP authentication

Save the file and upload it to the server.

Now you can add any number of SMTP servers to send mail.

Notes:
Please edit the SMTP credentials in the code above. You can add any number of SMTP server by copying and pasting the code between the second square bracket [ to ],.
Buddhanagar
Kathmandu, Nepal
9818500185
Sun-Thurs 10:00am-6:00pm
[email protected]
24 X 7 online support