Emailing from Javascript

2019-09-01 05:30发布

问题:

I know this topic has been covered from all sorts of angles but I'm not sure I've seen the answer to my specific question.

I am writing a prototype web page in HTML5, CSS, and Javascript. I want the user to be able to browse to a file to attach then attach that file to an email I send to a specific email address I've setup. I don't want the user's default email program to pop up.

I have addresses to SMTP servers within my companies intranet but I don't have access to a server at the moment to put server-side code on. On the web page the user will know email is involved but I don't want them to have to do a thing other than select the file and click a button with no other screens popping up.

I know how to use mailto in order to send an email but I don't think you can use that with attachments.

Is there anyway to do this without having to mess with the server side? Is knowing an smtp server address that I have permissions on enough? Again, this is a prototype but possibly the basis for a new feature on a product.

Thanks!

回答1:

Is there anyway to do this without having to mess with the server side?

Nope, not unless the target browser has exposes an SMTP client object. (And I don't think any do. That would make spam botnets quite a bit easier to build...)

You need to do this on the server.



回答2:

Email cant be sent just by client side . You must either use a mailto link if you want it on client side but that's not best way . Instead you should send a request to server and do server side processing .If you have SMTP credential you can send email with server side.



回答3:

I recently wrote a plugin for PostageApp that lets you send emails through jQuery. Basically, you just have to feed it your API key along with the message payload and you can send it through. With PostageApp, you can add your SMTP servers to the app and send through them.

Usage looks like this:

<script type="text/javascript" src="postage.js"></script>

<script type="text/javascript">

    $(function() {
        $('p').postage({ 
            apiKey: "API KEY HERE", 
            recipients: "recipient@email.com", 
            template: "TEMPLATE_SLUG" });
      });

</script>

I didn't publish the plugin because well... it never made sense for production usage because your API key was exposed and that's not a good thing. However, it is decent for prototyping (I use it!) and I would be happy to share.

(Full Disclosure: I am the Product Manager of PostageApp.)