Nodemailer with Docker

馋奶兔 提交于 2019-12-24 01:59:06

问题


I'm trying to send some emails from a docker container running express through register365.

This is the code used

export class Emailer {
transporter: nodemailer.Transporter;
constructor() {
    this.transporter = nodemailer.createTransport(smtpTransport({
        host: 'smtp.reg365.net',
        auth: {
            user: 'myuser',
            pass: mypassword'
        }
    }));
}

public async sendEmail(to,body) {
    try {
        return await this.transporter.sendMail({to,from: '"TEST" <user@myuser.ie>',text: body, subject: ' WE NEED THE CONTENT AND DESIGN OF THIS EMAIL!!!!'});
    }
    catch(error) {
        console.log('Email error');
        console.dir(error);
    }

}
}

That's working all fine if I run the express with npm start but If I run it with docker it'll fail with this error Error: Connection closed

It only fails using smtp.reg.356.net, if I use Gmail it'll work perfectly

This is the docker file I'm using

FROM  node:8

RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app

RUN npm install -g nodemon

COPY package.json /usr/src/app/
RUN npm install

COPY ./dist /usr/src/app/dist

EXPOSE 3005
EXPOSE 25
CMD [ "npm", "start" ]

Many thanks.

EDIT: As requested, running telnet smtp.reg365.net 25 returns this telnet: could not resolve smtp.reg.356.net/25: Name or service not known

Output of cat /etc/resolv.conf on the host machine

domain Hitronhub.home
nameserver 89.101.160.5
nameserver 89.101.160.4

On the docker container

search hitronhub.home
nameserver 127.0.0.11
options ndots:0

回答1:


Create a file /etc/docker/daemon.json

{
"dns": ["89.101.160.5", "89.101.160.4"]
}

Restart the docker service and try again and see if this works for you.

You are probably on office network which has its own DNS servers that you should be using. So you need to tell the Docker daemon which DNS server its containers should be using. That is what is creating the issue. The daemon.json file can be used to change the daemon configuration.



来源:https://stackoverflow.com/questions/45370428/nodemailer-with-docker

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!