title: How to create an api that sends mails using node and mongodb
body: <p>Am trying to create an app that sends mails.
  It gets the user input(To, Subject, Message) onClick of the form button sends a mail, and store that mail on mongodb </p>
  
  <p>front end</p>
  
  <pre><code>&lt;form&gt;
  &lt;span&gt;
  to :&lt;input type='text' &gt;
  &lt;/span&gt;
  
  cc :&lt;input type='text' &gt;
  &lt;/span&gt;
  
  bcc :&lt;input type='text' &gt;
  &lt;/span&gt;
  
  &lt;span&gt;
  subject :&lt;input type='text' &gt;
  &lt;/span&gt;
  
  &lt;span&gt;
  message :&lt;input type='text' &gt;
  &lt;/span&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>Back end</p>
  
  <pre><code>to = 'sender@gmail.com',
  cc = 'sender@gmail.com',
  bcc = 'sender@gmail.com',
  subject = 'A project proposal',
  message = 'the body of your mail',
  etc...
  </code></pre>
  
  <ol>
  <li><p>Schema</p>
  
  <p>const mongoose = require('mongoose');</p>
  
  <p>const UserSchema = new mongoose.Schema({</p>
  
  <p>to: {
      type: String,
    },
    cc: {
      type: String,
    },
    bcc: {
      type: String,
    },
    bcc: {
      type: String,
    },
    subject: {
      type: String,
    },
    message: {
      type: String,
    },
    attachment: {
      type: String,
    },
    date: {
      type: Date,
      default: Date.now
    },</p>
  
  <p>});</p>
  
  <p>const Mail = mongoose.model('Mail', UserSchema);</p>
  
  <p>module.exports = Mail;</p></li>
  </ol>
  
  <p>A.p.i </p>
  
  <pre><code>const Mail = require('../models/Mail');
  
  // Home Page
  router.get('/', forwardAuthenticated, (req, res) =&gt; res.render('home'));
  
  // Mail
  router.get('/mail', ensureAuthenticated, (req, res) =&gt;
    res.render('mail', {
      user: req.user,
      mail: req.mail
  
    })
  );
  
  router.post('/mail', (req, res) =&gt; {
    const { to, cc, bcc, subject, message, attachment, account } = req.body;
    let errors = [];
  
    if (!name || !subject || !message || !account) {
      errors.push({ msg: 'Please enter all fields' });
    }
  
    if (errors.length &gt; 0) {
      res.render('register', {
        errors,
        name,
        subject,
        message,
        account
      });
    } else {
      const newMail = new Mail({
        to,
        cc,
        bcc,
        subject,
        message,
        attachment,
        account
      });
  
      newMail
        .save()
        .then(mail =&gt; {
          req.flash(
            'success_msg',
            'mail sent'
          );
  
        })
        .catch(err =&gt; console.log(err));
  
    }
  })
  module.exports = router;
  </code></pre>
  
  <p>how do i go about it here?</p>