【问题标题】:Embed image in email body in Jenkins pipeline在 Jenkins 管道的电子邮件正文中嵌入图像
【发布时间】:2017-11-22 10:45:57
【问题描述】:

我需要通过管道从 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 工作区的正确图像路径, 我试图搜索相关的解决方案,但徒劳无功。

【问题讨论】:

  • 你找到在 Jenkins email-ext 中嵌入图片的方法了吗?
  • 您找到解决方案了吗?我也有类似的问题
  • @Dinesh 请在下面找到我的解决方案,我遇到了同样的问题。

标签: html jenkins jenkins-pipeline jenkins-plugins jenkins-email-ext


【解决方案1】:

您可以使用前面正确描述的图像的 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'>

【讨论】:

    【解决方案2】:

    您需要将图像转换为 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】:

      您不能添加 .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的链接

      【讨论】:

        【解决方案4】:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-02-16
          • 2016-09-07
          • 2018-07-05
          • 1970-01-01
          • 1970-01-01
          • 2019-12-12
          • 2013-08-31
          • 1970-01-01
          相关资源
          最近更新 更多