Nodemailer smtp nodejs question

Twirelex

Banned - selling outside of the marketplace.
Joined
Jul 11, 2019
Messages
206
Reaction score
113
I am not a professional coder so I want know if is possible to change IP addresses after certain amount of mails have been sent using nodemailer in nodejs
 
In theory it shouldn't be too complicated, pending on what exactly you mean by changing IP address. The following should be a semi-functioning example of a way to rotate proxies after a threshold of successful messages are sent. You'll need to bring your own proxies of course, and handle any authentication they may need.

JavaScript:
//PSEUDO-CODE

var currentProxyAddress = 'https://someproxy'
var listOfProxyAddresses = [
    'https://someproxy1',
    'https://someproxy2',
    'https://someproxy3',
    'and so on',
]
const changeProxyAfterMessagesSent = 25 //Or whatever number you want
var messagesSentOnCurrentProxy = 0

function getTransporter() {
    if (messagesSentOnCurrentProxy >= changeProxyAfterMessagesSent) {
        //Make sure whatever function you write here doesn't choose the currently active proxy!
        currentProxyAddress = someFunctionToGetARandomProxyFromTheList()
        messagesSentOnCurrentProxy = 0
    }
    
    return nodemailer.createTransport({
        host: 'smtp.example.com',
        port: 465,
        secure: true,
        proxy: currentProxyAddress
    });
}

...

//When you want to send a mail:
getTransporter().sendMail(someOptions).then(() => {
    //if success,
    messagesSentOnCurrentProxy++
})

//Using async await might be a better approach to avoid synchronization issues
//Current code may potentially have an inaccurate messagesSentOnCurrentProxy (but probably not *too* inaccurate)
 
In theory it shouldn't be too complicated, pending on what exactly you mean by changing IP address. The following should be a semi-functioning example of a way to rotate proxies after a threshold of successful messages are sent. You'll need to bring your own proxies of course, and handle any authentication they may need.

JavaScript:
//PSEUDO-CODE

var currentProxyAddress = 'https://someproxy'
var listOfProxyAddresses = [
    'https://someproxy1',
    'https://someproxy2',
    'https://someproxy3',
    'and so on',
]
const changeProxyAfterMessagesSent = 25 //Or whatever number you want
var messagesSentOnCurrentProxy = 0

function getTransporter() {
    if (messagesSentOnCurrentProxy >= changeProxyAfterMessagesSent) {
        //Make sure whatever function you write here doesn't choose the currently active proxy!
        currentProxyAddress = someFunctionToGetARandomProxyFromTheList()
        messagesSentOnCurrentProxy = 0
    }
   
    return nodemailer.createTransport({
        host: 'smtp.example.com',
        port: 465,
        secure: true,
        proxy: currentProxyAddress
    });
}

...

//When you want to send a mail:
getTransporter().sendMail(someOptions).then(() => {
    //if success,
    messagesSentOnCurrentProxy++
})

//Using async await might be a better approach to avoid synchronization issues
//Current code may potentially have an inaccurate messagesSentOnCurrentProxy (but probably not *too* inaccurate)
Thanks. Will look into this
 
Back
Top