簡體   English   中英

一旦部署在heroku上,如何通過帶有角和節點的表單輸入發送電子郵件?

[英]How to send an email via form input with angular and node once deployed on heroku?

我在heroku上部署了一個應用程序,前端為角,后端為node(express)。 在聯系頁面上,我有一個表單,一旦按下發送按鈕,該表單將通過電子郵件發送,然后通過HTTP將信息發送到節點,以便用戶輸入通過節點功能傳遞-我正在使用SendGrid插件發送電子郵件的電子郵件過程。 我嘗試了很多次,但是我無法使它正常工作,也找不到帶有角度的教程。 它以前在localhost上與nodemailer一起使用,但是一旦部署,我就無法弄清楚。 非常感謝您的寶貴時間。

節點代碼app.js

const express = require('express');
const path = require('path');
const bodyParser = require('body-parser');
const sgMail = require('@sendgrid/mail');


const port = process.env.PORT || 3000;

const app = express();

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));


app.use(express.static(path.join(__dirname, 'client')));

app.get('*', (req, res) => {
  res.sendFile(path.join(__dirname, 'client/index.html'));
});

app.post('/client-contact', (req, res) => {

const clientMsg = `
<p> Email sent from your portfolio website</p>
<h3>Contact details</h3>
<ul>
    <li>Name ${req.body.name}</li>
    <li>Email: ${req.body.email}</li>
</ul>
<h3>Message</h3>
<p>${req.body.message}</p>
`;

sgMail.setApiKey(process.env.SENDGRID_API_KEY);
const msg = {
  to: 'mrwanzein@outlook.com',
  from: 'mrwanzein@outlook.com',
  subject: 'Angular portfolio form user response',
  html: clientMsg,
};

sgMail.send(msg);

});


app.listen(port, () => {
  console.log(`Server is on on port ${port}`);
});

該函數用於注冊用戶輸入,然后將其發送到節點(並處理一些驗證,感興趣的是最后一行)contact.component.ts

sendMail() {
  var name = <HTMLInputElement>document.getElementById('inputName'),
    email = <HTMLInputElement>document.getElementById('inputEmail'),
    msg = <HTMLInputElement>document.getElementById('inputMsg');

  var obj = {
    name: name.value,
    email: email.value,
    message: msg.value
  } 

  let validateEmail = () => {
    const re = /[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?
        ^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])/;
    return re.test(email.value);
  }

  if(!name.value || !email.value || !msg.value || !validateEmail()) {    
    this.bool = true;
  } else {
    this.bool = false;

    setTimeout(() => {
      window.location.reload()
     }, 1500);

    return this.http.post('https://mrwanzein.herokuapp.com/client-contact', obj).subscribe();
  }
}

您需要為您的應用程序配置SMTP服務器。

最簡單的方法是使用heroku插件,例如mailgun

我修復了它,基本上代碼沒問題,我不得不刪除sgMail.send(msg)周圍的res.send(),並且忘記了ng build(angular)來更新HTTP地址。 我正在推送對倉庫的更改,而沒有缺少更新的代碼,哦,親愛的...:P

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM