typescript 打字稿/Javascript:带有主题、正文和链接数组的mailto

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/50637901/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 05:27:26  来源:igfitidea点击:

Typescript/Javascript: mailto with subject, body and array of links

javascriptangulartypescript

提问by Anna

I work on Angular 4. On click of hyperlink I have to open outlook, in the mail I will have to send 4 links. So I have planned to call a mailtofrom my typescript file. My code is

我在 Angular 4 上工作。单击超链接时,我必须打开 Outlook,在邮件中我必须发送 4 个链接。所以我计划mailto从我的打字稿文件中调用 a 。我的代码是

    <span (click)="mailMe()">Mail me the links</span>


    var links= ["link1.com", "link2.com", "link3.com"];
    mailMe(){
        console.log("111111");
        var mail = document.createElement("a");
        console.log("22222");
        mail.href = "mailto:[email protected]?subject=files&body=Hi";
        mail.click();
    }

I am able to call the function but mail is not popping up. In console 111111is getting printed but 22222is not getting printed. Where did I go wrong? OR is there a way I can send the array of links from HTML itself?

我可以调用该函数,但没有弹出邮件。在控制台111111中正在打印但未22222打印。我哪里做错了?或者有没有办法可以从 HTML 本身发送链接数组?

回答by Pranay Rana

you want to achieve like this

你想达到这样的

<a href="mailto:[email protected]?Subject=Hello&body=links:  %0D 
         http://link1.com  %0D http://link1.com " target="_top">Send Mail</a>

in angular you can do it like this , in html

在 angular 你可以这样做,在 html 中

<a [href]="emailstring" target="_top"></a>

in ts file do like this

在 ts 文件中这样做

 emailstring= "mailto:[email protected]?Subject=Hello&body=links:  %0D 
             http://link1.com  %0D http://link1.com";

Haven't tested with angular but check it with pure html. and its working on chrome.

尚未使用 angular 进行测试,但使用纯 html 进行检查。及其在 chrome 上的工作。

回答by Niladri

You can achieve this in IE with a simple window.location.hrefas IE has some weird behavior with mailto.Here I am using the same <span>from your code with the linksarray.

您可以在 IE 中使用一个简单的window.location.href方法来实现这一点,因为 IE 有一些奇怪的行为。在mailto这里,我使用<span>links数组相同的代码。

Example code for IE :

IE 的示例代码:

import { Component} from '@angular/core';

@Component({
  selector: 'app-root',
  template: `
     <span (click)="mailMe()">Mail me the links on (click)</span>
  `
})
export class AppComponent {
  name = 'test';
  links : any[]= ["link1.com", "link2.com", "link3.com"];

  mailText:string = "";

  mailMe(){
    this.mailText = "mailto:[email protected]+?subject=files&body="+this.links.join(" ,"); // add the links to body
    window.location.href = this.mailText;
  }

}

The below example might not work in IE but it's tested in Chrome. Here i have used anchor tag and set the hrefattribute in typescript.

下面的示例可能无法在 IE 中运行,但已在 Chrome 中进行了测试。在这里,我使用了锚标记并href在打字稿中设置了属性。

Example for Chrome and others

Chrome 和其他示例

import { Component,OnInit } from '@angular/core';

@Component({
  selector: 'app-root',
  template: `
     <a [href]="mailText">Mail me the links</a> <br>
  `
})
export class AppComponent implements OnInit {
  name = 'test';
  links : any[]= ["link1.com", "link2.com", "link3.com"];

  mailText:string = "";


  ngOnInit(){
    this.mailText = "mailto:[email protected]+?subject=files&body="+this.links.join(" ,");
  }

}

Here is a working demo : https://stackblitz.com/edit/attribute-binding-1-7wncwf

这是一个工作演示:https: //stackblitz.com/edit/attribute-binding-1-7wncwf

回答by Ani

Here is the simple HTML JavaScript code. This will help you to write your own.

这是简单的 HTML JavaScript 代码。这将帮助您编写自己的程序。

<!DOCTYPE html>
<html>
<head>
<script>
    function mailMe (mail) // <--- element on which you need to apply click
    {
        mail = document.createElement("a");
        mail.href = "mailto:[email protected]?subject=files&body=Hi";
        mail.click();
    }
</script>
</head>
<body>
    <span onClick = "mailMe(this);" >  <!-- pass it from here -->
         Mail me the links
    </span >
</body>
</html>