gpt4 book ai didi

javascript - 使用 Angular-Seed 实现 nodemailer

转载 作者:行者123 更新时间:2023-11-28 06:11:01 24 4
gpt4 key购买 nike

我很沮丧,并且已经用尽了我认为好的谷歌搜索。我正在创建一个网络应用程序并使用 Angular Seed 作为我的起点。该项目使用http服务器。我创建了几个 View / Controller /等。我的观点之一是有联系表。我认为我可以轻松添加nodemailer,使我能够以电子邮件的形式发送表单。那是一个巨大的失败。我在网上看到很多例子告诉我将 nodemailer 代码添加到 app.js 但我不断收到错误,说 require 未定义。我不知道还能做什么。这是我正在做的事情:

app.js:

'use strict';
(function() {
// Declare app level module which depends on views, and components
var myApp = angular.module('myApp', [
'ngRoute',
'sn.skrollr',
'ng.picturefill',
'ngAnimate',
'ui.bootstrap',
'myApp.version'
]).config(["snSkrollrProvider", function(snSkrollrProvider) {
snSkrollrProvider.config = { smoothScrolling: true,};
}]);

//initialise skrollr at runtime
myApp.run(["snSkrollr", function(snSkrollr) {
snSkrollr.init();
console.log("in init ");
}]);

myApp.config(['$routeProvider', function($routeProvider) {
$routeProvider.
when("/homePage", {
templateUrl: "Views/homePage.html",
json: 'json/home.json',
controller: 'homeController'
}).
when("/ourMission", {
templateUrl: "Views/mission.html",
json: 'json/mission.json',
controller: 'ourMissionController'

}).
when("/achievements", {
templateUrl: "Views/achievements.html",
json: 'json/achievements.json',
controller: 'achievementsController'

}).
when("/whoWeAre", {
templateUrl: "Views/whoWeAre.html",
json: 'json/whoWeAre.json',
controller: 'whoWeAreController'

}).
when("/contact", {
templateUrl: "Views/contact.html",
json: 'json/contact.json',
controller: 'contactUsController'

}).
when("/globalNav", {
templateUrl: "Navigation/bandedNavTemplate.html",
controller: 'bandedNavControl',
json: 'json/globalNav.json'

}).
otherwise({redirectTo: '/homePage'})
}]);
})();

package.json:

{
"name": "C4P",
"private": true,
"version": "0.0.0",
"description": "C4P ",
"repository": "https://github.com/angular/angular-seed",
"license": "MIT",
"devDependencies": {
"bower": "^1.3.1",
"gulp": "^3.9.0",
"gulp-concat-css": "^2.2.0",
"gulp-sass": "^2.1.0",
"http-server": "^0.6.1",
"jasmine-core": "^2.3.4",
"karma": "~0.12",
"karma-chrome-launcher": "^0.1.12",
"karma-firefox-launcher": "^0.1.6",
"karma-jasmine": "^0.3.5",
"karma-junit-reporter": "^0.2.2",
"protractor": "^2.1.0",
"shelljs": "^0.2.6"
},
"scripts": {
"postinstall": "bower install",
"prestart": "npm install",
"start": "http-server -a localhost -p 8000 -c-1",
"pretest": "npm install",
"test": "karma start karma.conf.js",
"test-single-run": "karma start karma.conf.js --single-run",
"preupdate-webdriver": "npm install",
"update-webdriver": "webdriver-manager update",
"preprotractor": "npm run update-webdriver",
"protractor": "protractor e2e-tests/protractor.conf.js",
"update-index-async": "node -e \"require('shelljs/global'); sed('-i', /\\/\\/@@NG_LOADER_START@@[\\s\\S]*\\/\\/@@NG_LOADER_END@@/, '//@@NG_LOADER_START@@\\n' + sed(/sourceMappingURL=angular-loader.min.js.map/,'sourceMappingURL=bower_components/angular-loader/angular-loader.min.js.map','app/bower_components/angular-loader/angular-loader.min.js') + '\\n//@@NG_LOADER_END@@', 'app/index-async.html');\""
},
"dependencies": {
"nodemailer": "^2.3.0"
}

}

如果我尝试将以下内容添加到 app.js 中,则会收到错误:

'use strict'
var nodemailer = require("nodemailer");

var smtpTransport = nodemailer.createTransport("SMTP",{
service: "Gmail", // sets automatically host, port and connection security settings
auth: {
user: "user@gmail.com",
pass: "12346Guuhf."
}
});

smtpTransport.sendMail({ //email options
from: "Sender P <email@gmail.com>", // sender address. Must be the same as authenticated user if using Gmail.
to: "Receiver Name <receiver@email.com>", // receiver
subject: "Emailing with nodemailer", // subject
text: "Email Example with nodemailer" // body
}, function(error, response){ //callback
if(error){
console.log(error);
}else{
console.log("Message sent: " + response.message);
}

smtpTransport.close(); // shut down the connection pool, no more messages. Comment this line out to continue sending emails.

});

我是否将此代码添加到了错误的区域?

预先感谢您的任何意见。

最佳答案

首先,您应该区分客户端(您的 Angular 代码)和服务器端(您的 Node.js)代码。在您的示例中,您似乎在 Angular 应用程序中使用了 NodeJS 代码,这将不起作用。

一旦您的服务器端被托管(请参阅 here 中的说明),您就可以使用 $http (以 Angular 形式)从客户端调用它。

在服务器端,我建议您使用 Express 进行设置,以便可以处理路由(从而接受诸如 senderEmailsenderNamehtml 等。您可以在 Angular 中找到 full example(使用 $http),以及使用 NodeJS 和 Nodemailer 作为依赖项的 Express 设置。

关于javascript - 使用 Angular-Seed 实现 nodemailer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36395308/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com