将图像嵌入电子邮件正文jenkins管道中

时间:2017-11-22 10:45:57

标签: html email jenkins jenkins-pipeline

我需要在电子邮件中添加一个图像作为电子邮件正文,而不是作为附件,从Jenkins通过管道添加。 我在Jenkins管道中使用emailext插件,下面是我正在使用的代码。

emailext (
          subject: "test email",
          body: """
          <html>
          <body>
          <p>please find attached score: Job '${env.JOB_NAME}':</p>
          <p> The last commit was by ${last_commit_user} </p>
          <p>Please check jenkins console at "</p> 
          <p> For detailed report on this analysis, visit "</p>
          </body>
          </html>
          """,
          to: email_recipients,
          attachmentsPattern: '${BUILD_NUMBER}.jpg'
)

我不想使用“attachmentsPattern”,它是附件, 我试过用,

body: """ 
<html>
<img src="image_name.jpg" >
</html>
"""

这只是我的电子邮件中的蓝框,我提供了相对于我的Jenkins工作区的正确图像路径, 我试图搜索相关的解决方案,但徒劳无功

请告知

5 个答案:

答案 0 :(得分:2)

您可以按照前面所述正确使用图像的base64字符串来执行此操作,也可以将图像作为附件添加到电子邮件文本中,然后在img src属性中进行引用。

直接在管道中创建html文件(将是电子邮件中的html正文)的简单方法

 sh "echo '<b>Job Name: </b>${env.JOB_NAME}<br />' > mail.html"
 sh "echo '<b>Execution Result: </b>${currentBuild.currentResult}<br />' >> mail.html"
 sh "echo '<b>Build Number: </b> ${env.BUILD_NUMBER}<br />' >> mail.html"
 sh "echo '<b>Build URL: </b> ${env.BUILD_URL}<br />' >> mail.html"  
 sh "echo '<img src='cid:sample.jpg' alt='hello'>' >> mail.html"

1。)在电子邮件文本中添加附件

   emailext attachmentsPattern: 'sample.jpg', 
   body: '${FILE,path="mail.html"}',
   to: "${emailRecipientsList}",
   recipientProviders: [[$class: 'DevelopersRecipientProvider'], [$class: 'RequesterRecipientProvider']],
   subject: "Jenkins Build ${currentBuild.currentResult}: Job ${env.JOB_NAME}",
   mimeType: 'text/html' 

2。)引用img src属性中附件中的图片

<img src='cid:sample.jpg' alt='hello'>

答案 1 :(得分:1)

您不能添加 .png 格式的图像。相反,您可以在 html 数据中添加编码的 base64 格式的图像。

要将您的实际图像转换为 base64,请在此处使用 LINK。 需要更改来源。

body:"""
<html>
<img src="data:image/png;base64<BASE64_ENCODED_IMAGE>" >
</html>
"""

在上面的代码中'BASE64_ENCODED_IMAGE'是从上面的链接获得的base64转换代码。

请查看 Embed Base64-Encoded Images Inline In HTML

的链接

答案 2 :(得分:0)

您需要将图像转换为Base64。我用python做的。

import base64

base64Img = ''

with open("image.png", "rb") as imageFile:
    base64Img = base64.b64encode(imageFile.read())

with open("image.html", "wb+") as writer:
    writer.write('<img src="data:image/png;base64,'.encode())
    writer.write(base64Img)
    writer.write('">'.encode())

这将写入文件“ image.html”。然后,您可以将此文件追加到中。在我的管道中,我这样做是这样的:

${FILE,path="image.html"}

答案 3 :(得分:-1)

将图像文件推送到jenkins作业的工作空间目录中。
转到工作区,然后单击图像以获取URL。
img src="IMAGE_URL"放在电子邮件模板中。

答案 4 :(得分:-2)

要将图像包含在您的Jenkins扩展电子邮件中,您必须:

  • 将图像放入以下Jenkins目录:$JENKINS_HOME/war/images/
  • 然后重新启动Jenkins服务

您可以将图像YOUR_IMAGE.jpg插入groovy-html.template中。请注意,${rooturl}static/e59dfe28/images是常量。

<p>Some HTML...</p>
<img src="${rooturl}static/e59dfe28/images/YOUR_IMAGE.jpg"/>
<p>Some HTML...</p>
相关问题