想在邮件正文PHP中添加图片

时间:2014-09-16 12:33:23

标签: php email yii

这是我用来在邮件正文中发送图像的代码

    $message2 .= '<div style = "color: white; padding: 5px;"></div></div>';

    # Open a file
    $url = realpath(Yii::app()->theme->basePath) . "/images/Male-icon.png";
    $file = fopen($url, "r");
    if ($file == false) {
        echo "Error in opening file";
        exit();
    }
    # Read the file into a variable
    $size = filesize($url);
    $content = fread($file, $size);

    # encode the data for safe transit
    # and insert \r\n after every 76 chars.
    $encoded_content = chunk_split(base64_encode($content));

    # Get a random 32 bit number using time() as seed.
    $num = md5(time());

    # Define the main headers.
    $header = "From:xyz@somedomain.com\r\n";
    $header .= "MIME-Version: 1.0\r\n";
    $header .= "Content-Type: multipart/mixed; ";
    $header .= "boundary=$num\r\n";
    $header .= "--$num\r\n";

    # Define the message section
    $header .= "Content-Type: text/html\r\n";
    $header .= "Content-Transfer-Encoding:8bit\r\n\n";
    $header .= "$message2\r\n";
    $header .= "--$num\r\n";

    # Define the attachment section
    //$header .= "Content-Type:  multipart/mixed; ";
    $header .= "Content-Type:  image/jpeg; ";
    $header .= "name=\"Male-icon.png\"\r\n";
    $header .= "Content-Transfer-Encoding:base64\r\n";
    $header .= "Content-Disposition:attachment; ";
    $header .= "filename=\Male-icon.png\"\r\n\n";
    $header .= "$encoded_content\r\n";
    $header .= "--$num--";

    # Send email now
    $retval = mail($to, $subject, "", $header);
    if ($retval == true) {
        echo "Message sent successfully...";
    } else {
        echo "Message could not be sent...";
    }

我在邮件正文中使用简单的邮件功能发送图像。帮助解决我的问题。在gmail中,它添加了下划线自动图像无法查看。指导我如何在邮件正文中发送图像。

0 个答案:

没有答案