2017-12-15 17 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ファイルに次のルートを追加し、サーバーコードを実装している場合どのような経路でもこの関数をミドルウェアとして呼び出すことができます。

関連する問題