How to embed images in email

后端 未结 6 1552
感情败类
感情败类 2020-11-22 11:57

I need to embed an image in e-mail. How do I do it?

I do not want to use third party tool, nor am I interested in language specific answer (but it is PHP, in case yo

6条回答
  •  死守一世寂寞
    2020-11-22 12:37

    As you are aware, everything passed as email message has to be textualized.

    • You must create an email with a multipart/mime message.
    • If you're adding a physical image, the image must be base 64 encoded and assigned a Content-ID (cid). If it's an URL, then the tag is sufficient (the url of the image must be linked to a Source ID).

    A Typical email example will look like this:

    From: foo1atbar.net
    To: foo2atbar.net
    Subject: A simple example
    Mime-Version: 1.0
    Content-Type: multipart/related; boundary="boundary-example"; type="text/html"
    
    --boundary-example
    Content-Type: text/html; charset="US-ASCII"
    
    ... text of the HTML document, which might contain a URI
    referencing a resource in another body part, for example
    through a statement such as:
    IETF logo
    
    --boundary-example
    Content-Location: CID:somethingatelse ; this header is disregarded
    Content-ID: 
    Content-Type: IMAGE/GIF
    Content-Transfer-Encoding: BASE64
    
    R0lGODlhGAGgAPEAAP/////ZRaCgoAAAACH+PUNv
    cHlyaWdodCAoQykgMTk5LiBVbmF1dGhvcml6ZWQgZHV
    wbGljYXRpb24gcHJvaGliaXRlZC4A etc...
    
    --boundary-example--
    

    As you can see, the Content-ID: ID is matched to the at SRC="cid:foo4atfoo1atbar.net". That way, the client browser will render your image as a content and not as an attachement.

    Hope this helps.

提交回复
热议问题