PHP Attaching an image to an email

前端 未结 6 813
滥情空心
滥情空心 2020-11-28 13:26

Is there a way to attach an image to an html formatted email message created in PHP?

We need to ensure that a corporate logo is on emails sent to clients who may not

6条回答
  •  眼角桃花
    2020-11-28 13:36

    Try the PEAR Mail_Mime package, which can embed images for you.

    You need to use the addHTMLImage() method and pass a content id (cid), which is a unique string of text you will also use in your img's src attribute as a cid: URL. For example:

    include('Mail.php');
    include "Mail/mime.php";
    
    
    $crlf = "\r\n";
    $hdrs = array( 
            'From' => 'foo@bar.org', 
            'Subject' => 'Mail_mime test message' 
            ); 
    
    $mime = new Mail_mime($crlf); 
    
    //attach our image with a unique content id
    $cid="mycidstring";
    $mime->addHTMLImage("/path/to/myimage.gif", "image/gif", "", true, $cid);
    
    //now we can use the content id in our message
    $html = '';
    $text = 'Plain text version of email';
    
    $mime->setTXTBody($text);
    $mime->setHTMLBody($html); 
    
    $body = $mime->get();
    $hdrs = $mime->headers($hdrs);
    
    $mail =& Mail::factory('mail');
    $mail->send('person@somewhere.org', $hdrs, $body);
    

提交回复
热议问题