30
loading...
This website collects cookies to deliver better user experience
MAIL_TRANSPORT=smtps://[email protected]:[email protected]
MAIL_FROM_NAME=WebWork
import { ConfigService } from '@nestjs/config';
import { EjsAdapter } from '@nestjs-modules/mailer/dist/adapters/ejs.adapter';
export const getMailConfig = async (
configService: ConfigService,
): Promise<any> => {
const transport = configService.get<string>('MAIL_TRANSPORT');
const mailFromName = configService.get<string>('MAIL_FROM_NAME');
const mailFromAddress = transport.split(':')[1].split('//')[1];
return {
transport,
defaults: {
from: `"${mailFromName}" <${mailFromAddress}>`,
},
template: {
adapter: new EjsAdapter(),
options: {
strict: false,
},
},
};
};
import { MailerModule } from '@nestjs-modules/mailer';
@Module({
imports: [
MailerModule.forRootAsync({
imports: [ConfigModule],
inject: [ConfigService],
useFactory: getMailConfig,
}),
],
})
<main>
Доброго дня, <%= locals.username %>!<br />
От Вас поступила регистрация на сайте post-life.<br />
Для окончания регистрации пройдите пожалуйста по
<a href="<%= urlConfirmAddress %><%= id %>">ссылке</a> .
</main>
import { MailerService } from '@nestjs-modules/mailer';
@Injectable()
export class UserService {
constructor(
// помимо всего прочего подключение сервиса отправки
private readonly mailerService: MailerService,
) {}
///
}
async sendConfirmMail(user: UserEntity) {
const urlConfirmAddress = this.configService.get<string>(
'URL_CONFIRM_ADDRESS',
);
// Отправка почты
return await this.mailerService
.sendMail({
to: user.email,
subject: 'Подтверждение регистрации',
template: join(__dirname, '/../templates', 'confirmReg'),
context: {
id: user.id,
username: user.username,
urlConfirmAddress,
},
})
.catch((e) => {
throw new HttpException(
`Ошибка работы почты: ${JSON.stringify(e)}`,
HttpStatus.UNPROCESSABLE_ENTITY,
);
});
}
{
"collection": "@nestjs/schematics",
"sourceRoot": "src",
"compilerOptions": {
"assets": [
"**/*.ejs"
],
"watchAssets": true
}
}