Skip to content
Snippets Groups Projects
email.service.ts 935 B
Newer Older
  • Learn to ignore specific revisions
  • ncastejon's avatar
    ncastejon committed
    import { Injectable, Logger } from '@nestjs/common';
    
    import * as amqp from 'amqplib/callback_api';
    import { Email } from './email';
    
    @Injectable()
    export class EmailService {
    
      send(email: Email) {
    
        const rabbitmqUrl = 'amqp://user:password123@0.0.0.0:5672';
    
        const mailerQueue = 'portail-data-send-email';
    
        // Connect to rabbitmq
        amqp.connect(rabbitmqUrl, (err, conn) => {
    
    ncastejon's avatar
    ncastejon committed
          if (err != null) { Logger.log(err); }
    
          // Create a communication channel
          conn.createChannel((error, ch) => {
    
    
    ncastejon's avatar
    ncastejon committed
            if (error != null) Logger.log(error);
    
            // Stringify and bufferise message
            const buffer = Buffer.from(JSON.stringify(email));
    
            ch.assertQueue(mailerQueue, { durable: true });
    
            ch.sendToQueue(mailerQueue, buffer);
    
    
    ncastejon's avatar
    ncastejon committed
            Logger.log(`sent to queue ${mailerQueue}: ${JSON.stringify(email)}`);
    
    
          });
    
          setTimeout(() => {
            conn.close();
          }, 500);
        });
      }
    
    }