Within an HTML file I would like to use the library nodemailer to send emails. To do this with nodejs I simply put var nodemailer = require('nodemailer') at the top of the script component of my HTML file however require is not a valid function in HTML. When I try to do this I get the error ReferenceError: require is not defined. I would like to know how to use this library from HTML, or if this is not possible, what a good alternative is so that I can send emails from an HTML file.
You can't use Nodemailer from an HTML file.
Browsers can't connect to the SMTP servers required to send mail.
Some libraries designed for use with Node.js can be persuaded to work in a browser, although you’d usually need a tool like Webpack to replace the node module system. This can only work if the library doesn’t depend on any APIs provided by Node.js but not browsers.
Browsers provide no API for sending email (beyond the exceptionally limited mailto: URI scheme which will create a new email for a user to send in their default email client (if they have a default email client)). Nor do they provide any API for direct socket creation which could be used to build such an API.
The library you have found will not run in the browser.
If you want to send an email you need to do it from a server. You can make an HTTP request (either by submitting a form of using XMLHttpRequest or fetch) to get the data from the browser to the server.
If you write your server using Node.js (likely with the Express.js framework) then you can use the library you found.
require and nodemailer are for "nodejs" (server side javascript), so you can't use nodejs functionality in the browser.
check out this link
Related
Is creating an email application possible with angular?
I tried implementing nodejs script but it's not working due to using nodejs script inside angular ts file, giving me the following
Error: Module not found: Error: Can't resolve 'imap'
Any help or guide would be much appreciated.
Thanks
You cannot use modules which depend on Node.js features from a browser. The imap module depends on such a feature: it requires the ability to make raw network requests.
The block here is at a lower-level than Angular.
You could replace direct IMAP (and SMTP) access with a web service since browsers support HTTP and WebSockets.
You can’t make IMAP/SMTP connections directly from the browser, but you could use something like EmailEngine as the backend for any email related actions.
So I am currently in the process of learning the new Javascript Cloud SDK. Of course there is also a package for attachments and document info records but I am still facing some problems.
So mainly I just want to get an attachment which is attached to a document info record and safe it to my local file system. I am working with the JS Cloud SDK so I am working with a Node application.
When working with the API directly (testing via Postman) I can get the media_src of the attachment simply by adding '$value' to the request path. When I try to access this URL outside of Postman with a simple Node https.get request I get a SAML 2.0 Error (SAML2 Service not accessible). I guess that is because I cannot access those URLs via browser and therefore I should use the SDK for that.
So the final problem I am facing is that I cannot find anything about getting the file itself in the JSDoc of the SDK.
Same goes also for creating an attachment. Should I use the 'builder()' method for that and pass a JSON object or how does a POST or PUT request work with that SDK? I cannot find any blogs etc. because they are only doing simple 'Hello World' programms or GET some data.
thanks for reaching out to us!
Currently, we do not support OData media streams in the JS SDK's VDM yet.
If this functionality is critical for you, you can consider using the Java version of the SDK. Alternatively, you can open an issue here.
Regarding the SAML error I cannot comment, since I don't how your Postman is configured or how your system is setup.
Is it possible to access blpapi from javascript running in the client's browser?
I wish to access bloomberg's API from javascript running in the client's browser, in the assumption that the client has an open bloomberg session and therefore bbcomm is running.
Conceptually, this would be the same as accessing the blpapi on the client side from python.
However, all existing solutions I found in js appear to be server-side:
blpapi-node (node-based)
blpapi-httm (creates a server where to post http request)
blpapi-react (cannot make this module work)
Now you can access Bloomberg data natively in JavaScript via Web AppPortal. This allows you to write web based applications that run inside LP Components.
To learn more about Web AppPortal, go to MYAP 5
To download the SDK, please type SDK -> select SDK -> AppPortal Web SDK -> click Install.
This is obsolete, see Mourad Barakat's answer above
Conversation with Bloomberg Support confirms this is not possible**
BB say they don't support javascript access, and that one solution is to use their Server API to use the authentication of the client (who has a bb terminal open) to query data and return it to the client
An alternative hack
An alternative hack is to create an executable mini-server that the client downloads and launches, and that offers an http interface to get data to the webapp.
For example, this could be done in Flask in Python, and in fact it has already been done by blpapi-web (excluding the executable part, for which you can use PyInstaller and py2exe for Windows and py2app for Mac)
I have a simple html page being served by express on a node.js server and in the client side javascript i want to access some server side variables. I tried googling around for a solution but didn't see a straight forward solution without using some sort of templating engine.
So if i need to access a variable on the server from a client side JS served from the same host, what do i need to do?
To access the variable on the client, you'll need to expose it inside of a script tag, there are a few node modules that can help you with it (ex: express-state) https://www.npmjs.com/package/express-state, but you'll need a templating engine to generate the html required for it. You can use a variety of templating engines when working with express. If you use express generators straight out of the box it should come with jade and you can use different options on the generators to use other templating engines. See here for express generator options http://expressjs.com/en/starter/generator.html
Create an API to expose your variable:
app.get('/myvar', function(req, res){
res.send(varYouWantToSend);
});
Then in the client-side make a call for that API with a http GET request. The url for the API would be www.yoursite.com/myvar in this example.
You could try using this lib:
https://github.com/siriusastrebe/syc
and if you want to implement yourself look into Ajax. Jquery have a pretty simple ajax wrapper (http://api.jquery.com/jquery.ajax/). offcourse you need to open corresponding urls on the server to return / set the values.
Try socket.io http://socket.io/ it uses websockets on modern browsers and gracefully degradate to older technologies like AJAX with older browsers, leaving user experience the same.
Check out details here Web Socket support in Node.js/Socket.io for older browser
I'm trying to make a program that can be hosted by many peoples, like an app.
The app use a REST API, so I must authenticate with Oauth,
and because anyone should be able to host the program, the redirect URI cannot be static.
Further, I don't want to use any server-side processing, which means only javascript for me.
Is it even possible to make a secure and working solution with non-static redirect URI,
and only using javascript, to work in a normal webbrowser?
So you use the information provided in the request to your app to indicate the URL for your app. For instance, if the request came to http://example.com/path/to/app and you knew in your app that /to/app was part of your routing infrastructure, then the path to your app is http://example.com/path/.
That is how I would determine it, using a serverside language.
Using a javascript library, which would be loaded from the server, I would either determine it like the above, or I would just hard code it on the generation of the javascript file (when you tell people where to download the javascript, it can use a form that requires their web address first).