2017-12-15 20 views
1

내 웹 사이트의 경우 사용자에게 이메일을 보내려고합니다. 나는 nodeJS로 메일을 보내기위한 sendgrid 공급자를 사용했다. 나는 nodeJS에 익숙하지 않다. 나는 그들의 페이지에있는 명령에 따라 명령 프롬프트를 통해 메일을 보냈다. 하지만 실제 서버에 구현하는 방법은 없습니다. sendgrid을 통해 이메일을 보내려면 패키지를 설치해야합니다. 그리고 특정 이벤트에 이메일을 보내고 싶습니다.실제 서버 (SendGrid)를 통해 이메일을 보내는 방법

function goMail() 
{ 
    const sgMail = require('@sendgrid/mail'); 
sgMail.setApiKey(MY_API_KEY); 
const msg = { 
    to: '********[email protected]', 
    from: '12345***@gmail.com', 
    subject: 'Sending with SendGrid is Fun', 
    text: 'and easy to do anywhere, even with Node.js', 
    html: '<strong>and easy to do anywhere, even with Node.js</strong>', 
}; 
sgMail.send(msg); 

} 

HTML은 :

<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <title>Bootstrap Example</title> 
    <meta charset="utf-8"> 
    <meta name="viewport" content="width=device-width, initial-scale=1"> 
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> 
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> 
    <script src="index.js"></script> 
</head> 
<body> 

<div class="container"> 
    <h2>Vertical (basic) form</h2> 
    <form> 
    <div class="form-group"> 
     <label for="email">Email:</label> 
     <input type="email" class="form-control" id="email" placeholder="Enter email" name="email"> 
    </div> 
    <div class="form-group"> 
     <label for="pwd">Password:</label> 
     <input type="password" class="form-control" id="pwd" placeholder="Enter password" name="pwd"> 
    </div> 
    <div class="checkbox"> 
     <label><input type="checkbox" name="remember"> Remember me</label> 
    </div> 
    <button type="submit" class="btn btn-default" onclick="goMail();">Submit</button> 
    </form> 
</div> 

</body> 
</html> 

가 실제 서버에서 작업을 수행하는 방법을 브리핑을 설명해주십시오.

답변

0

index.js 파일을 HTML에 직접 포함시키고 Nodejs 기능을 호출 할 수 없습니다. 대신 Nodejs에서 Express 프레임 워크를 사용하고 jQuery를 사용하여 양식 데이터를 게시해야한다. 아래 링크에 제공된 안내를 따르십시오. https://www.tutorialspoint.com/nodejs/nodejs_express_framework.htm

이 답변을 통해 올바른 방향으로 안내하고 문제를 해결할 수 있기를 바랍니다.

+0

감사합니다. firebase webapp – Badhusha

+1

@Badhusha에서 이메일을 보내기위한 다른 자습서를 제공해 주시겠습니까? https://firebase.google.com/docs/functions/http-events –

0

당신은 사용자

app.get('/sendEmail', (req, res) => { 
    let sendgrid = require('sendgrid'); 
    let SG = sendgrid('your sendgridKey'); 
    let request = SG.emptyRequest(); 
    request.method = 'POST'; 
    request.path = '/v3/mail/send'; 
    request.body = (new sendgrid.mail.Mail(
    new sendgrid.mail.Email(req.body.from), 
    req.body.subject, // subject 
    new sendgrid.mail.Email(req.body.to), 
    new sendgrid.mail.Content('text/html', 'your html') 
    )).toJSON(); 

    SG.API(request, (err, response)=> { 



    if (response.statusCode >= 200 && response.statusCode < 300) { 
      res.send(response); 
     }); 
}); 

또는 라우터의 외부에서이 기능을 정의 할 수 있습니다에게 이메일을 보내기위한 API를 만들기위한 서버 코드가 다음 단지 app.js 파일에 다음 경로를 추가 구현 한 경우 이 기능을 어떤 경로에서든 미들웨어로 부를 수 있습니다.