0
我想使用sendgrid在我的heroku網站上設置一個電子郵件聯繫頁面。我已經用幫手郵件和沒有的代碼試過了代碼。但是,兩者都不能發送電子郵件到我的電子郵件地址。我在我的heroku應用程序設置中將我的sendgrid API密鑰添加爲變量,並且還導入了用戶名和密碼。 當用戶點擊一個按鈕時,我的電子郵件過程就開始了。我對錶單元素中的帖子路徑有一個操作,我使用body parser從用戶(消息,他們的電子郵件地址和他們的消息)中檢索輸入值。在我的發佈路線中,我在我的email.js文件中調用了一個包裝來自sendgrid的代碼的函數。Sendgrid節點js不發送電子郵件。程序沒有錯誤信息
//我的html使用handlebars。
<div class = "mainDiv" id = "sectionAbout">
<h1 class="mainDivH1" id = "AboutMe">{{ ContactLi }}</h1>
<form action="/email" method="POST">
<label for="fullName">{{ formName }}</label> <br><br>
<input type="text" name="fullName">
<br>
<br>
<label for="subject">{{ formSubject }}</label> <br><br>
<input type="text" name="subject">
<br>
<br>
<label for="email">{{ formEmail }}</label> <br><br>
<input type="text" name="email">
<br>
<br>
<label for="message">{{ formMessage }}</label> <br><br>
<textarea rows="15" cols="80" type="text" name="message id="textbox"></textarea>
<br>
<br>
<button id="but" type="submit">Submit</button>
</form>
</div>
//爲POST航線的代碼
var email = require('../mail/email');
router.post('/email', function(req, res){
var name = req.body.fullName;
var emSubject = req.body.subject;
var senderMail = req.body.email;
var message = name + " " + req.body.message;
var myEmail = '[email protected]';
email.send(emSubject, senderMail, message, function(){
res.redirect('/contact');
});
});
//email.js
var helper = require('sendgrid').mail
var sg = require('sendgrid')(process.env.SENDGRID_API_KEY);
var email = {
send: function(emSubject, senderMail, message, callback){
var from_email = new helper.Email(senderMail);
var to_email = new helper.Email('[email protected]');
var subject = emSubject;
var content = new helper.Content("text/plain", message);
var mail = new helper.Mail(from_email, subject, to_email, content);
var request = sg.emptyRequest({
method: 'POST',
path: '/v3/mail/send',
body: mail.toJSON()
});
console.dir("request");
console.dir(JSON.stringify(request, null, 4));
sg.API(request, function(error, response) {
if(error){
console.log(error);
}
console.log(response.statusCode)
console.log(response.body)
console.log(response.headers)
callback(error);
});
}
}
module.exports = email;
沒有錯誤記錄?在sendgrid儀表板中顯示嗎? –