Skip to content

Instantly share code, notes, and snippets.

@gr2m
Last active January 28, 2021 15:10
Show Gist options
  • Save gr2m/5463552 to your computer and use it in GitHub Desktop.
Save gr2m/5463552 to your computer and use it in GitHub Desktop.
Imagine you could send emails with JavaScript, multipart, and with attachments?! How would the code look like? This is what I came up with. Forks & comments much appreciated! #nobackend #dreamcode
// send text email
sendEmail({
subject: "Hello, World!",
text: "This mail has been sent from the frontend",
to: "joe@exam.pl"
})
// send multipart text / html email
sendEmail({
subject: "Hello, World!",
text: "This mail has been sent from the frontend",
html: "<p>This mail has been sent from the frontend</p>",
to: "joe@exam.pl"
})
// send multipart with attachment
sendEmail({
subject: "Hello, World!",
text: "This mail has been sent from the frontend",
html: "<p>This mail has been sent from the frontend</p>",
to: "gregor@martynus.net",
attachments: [
convert( document.body ).to("screenshot.png"),
{ filename: "info.text", data: "Some info about the page"}
]
})
@gr2m
Copy link
Author

gr2m commented Jun 23, 2013

you don't need success / error callbackes, you can just do

sendEmail(params).done(onsuccess).fail(onerror);

@robcolburn
Copy link

In some ways, you could see this as a browser-enhancement of mailto: links. I believe external e-mail apps do provide the level of inter-app communication necessary. At the level, you're still forcing the user to click send on their client (outlook, gmail) which enforces a level of security and spam-control.

Where it gets hard is truly seamless email sending, then the e-mail client or browser-based smtp needs to trust the browser app and the website to not be a malicious website, and be a real person. Only then, would callbacks be realistic - otherwise, it's fire and forget.

@qgustavor
Copy link

Why a function allowing request permissions from a browser? It simplifies the current "stack of infobars/notifications" problem using a simple dialog and also the "security" problem allowing, in this case, the webpage use the user's e-mail client, only if allowed.

requestPermissionsFor(permissions, [callback]) // returns promisse: callback is the same as .then()

// example: email client
requestPermissionsFor('email notifications').fail(function (error) {
    // a error occurred, example: incognito mode is enabled
}).done(function (permissions) {
    // permissions = {email: true, notifications: false}

    // alerts the user that the website can send emails but it
    // will not show any notifications of new received emails
});

@orbitbot
Copy link

orbitbot commented Jul 8, 2014

This might be slightly offtopic, since I stubled upon this discussion from the noBackend website while searching for resources on offline applications, but services like Mailgun ( http://www.mailgun.com/ , no affiliation) offer more or less this exact API already. API usage is through HTTP requests, which easily could be wrapped for example with Angular to something that can be used as you discuss above.

@gr2m
Copy link
Author

gr2m commented Jan 19, 2015

I understand the security concerns, but please understand that this is out of scope here. Think of it as a wrapper to what ever is used behind the curtain to securely send multipart emails. If http requests to a backend are used, then it's the back-end's job to make this secure. The front-end just expresses the intent to do so, and if the server denies is, the error will be transparently passed to the returned promise.

@alexandrevicenzi
Copy link

Using CORS it will prevent sending emails from not allowed pages. But it's difficult to find a way that solves everything. 😢

@SB00362446
Copy link

Can I deliver OBIEE report as Email attachment to all Users belong to particular Role instead of individual User ? using OBIEE 11.1.7.x. Any help appreciated.

@sudharsan203
Copy link

How can i use this,Will u please help me out here

@Mackenzie-Frey
Copy link

@sudharsan203, It says #dreamcode at the top. Based upon this and other research I've completed, I don't believe this is possible.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment