使用href在电子邮件正文中添加图像

时间:2016-07-20 07:25:57

标签: html

我的页面中有一个链接,一旦点击它就会打开电子邮件客户端。 我的代码如下:

  <a href="mailto:?subject=Testing&body=AttachImage">

我的问题是如何在电子邮件正文中添加图片。我试过图像src但它不起作用。有什么建议吗?

3 个答案:

答案 0 :(得分:0)

<a>标记内添加该图片。

<a href="mailto:?subject=Testing">
    <img src="your image path">
 </a>

希望它会有所帮助。

答案 1 :(得分:0)

它正在工作,你可能没有定义img宽度和高度。

&#13;
&#13;
img{
  width:100px;
  height:100px;
}
&#13;
 <a href="mailto:?subject=Testing&body=AttachImage">
<img src = "https://source.unsplash.com/random">
</a>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

假设您的图像定义如下:

<img id="myimage" src="http://www.myserver.com/images/myimage"></img>

此处编码的等效base64是您希望通过邮件发送的:

<img id="b64" src=""></img>

您可以使用base64编码形式,例如:

function getBase64Image() {
    //return LOGO;
  var dataURL = '';
  try {
    var img = document.getElementById('myimage');
    // Create an empty canvas element
    var canvas = document.createElement("canvas");
    canvas.width = img.width;
    canvas.height = img.height;

    // Copy the image contents to the canvas
    var ctx = canvas.getContext("2d");
    ctx.drawImage(img, 0, 0);

    // Get the data-URL formatted image
    // Firefox supports PNG and JPEG. You could check img.src to guess the
    // original format, but be aware the using "image/jpg" will re-encode the image.
    dataURL = canvas.toDataURL("image/jpg");
  }
  catch(e) {
    console.log('Could not retrieve Base64 image with userAgent : ' + navigator.userAgent + ' \nException : ' + e);
  }
  return dataURL;

}

var base64Image = getBase64Image();

然后设置你的img src:

document["b64"].src = base64Image ;