You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用PHP Mail发送带图片的HTML邮件时第二封邮件无法发送

PHP mail()发送带图片的HTML邮件问题

测试代码(可正常运行)

<?php
// (A) MAIL SETTINGS
$mailTo = "emad.zedan@itisco.com.sa";
$mailSubject = "Test Mail With Image";
// (B) MAIL MESSAGE
// HOST THE IMAGE ON YOUR OWN SERVER!
// ALSO REMEMBER TO PROVIDE THE DIRECT LINK JUST-IN-CASE
$img = "https://emad-zedan.com/email/logo.png";
$mailBody = "<img src=".$img."><br>";
// (C) HEADER - HTML MAIL
$mailHead = implode("\r\n", [
      "MIME-Version: 1.0",
      "Content-type: text/html; charset=utf-8",
      "From: " . "uxui@emad-zedan.com"
]);
// (D) SEND
echo mail($mailTo, $mailSubject, $mailBody, $mailHead)
? "OK" : "ERROR" ;

?>

业务代码(第二封确认邮件无法发送)

$email_subject = 'ITIS Global Website Message- ' . $firstname . ' ' . $lastname;
$email_message = 'ITIS Global - Website Get A Quote Message:' .
            '<br/>Full Name: ' . $firstname . ' ' . $lastname.
            '<br />International Phone: ' . $internationalphone .
            '<br />Email: ' . $email .
            '<br />Country: ' . $country .
            '<br />Company: ' . $company .
            '<br />Position: ' . $position .
            '<br />Solution: ' . $solution;

$img1 = "https://emad-zedan.com/email/logo.png";
$img2 = "https://emad-zedan.com/email/in.png";
$img3 = "https://emad-zedan.com/email/f.png";
$img4 = "https://emad-zedan.com/email/t.png";
$img5 = "https://emad-zedan.com/email/y.png";


$email_message_confirmation = "<div>" .
                    "<div style='font-size:20px;padding:10px 0px;'>Thank you for contacting us!</div><br />" .
                    "<div>We will responed to you on the given contact information as soon as possible, We collected this infromtion from you:</div>" .
                    "<table border='0'>" .
                    "<tr width='150'>" .
                    "<td><strong>Full Name: </strong></td>" .
                    "<td>" . $firstname . "&nbsp;" . $lastname . "</td>" .
                    "</tr>" .
                    "<tr width='150'>" .
                    "<td><strong>International Phone: </strong></td>" .
                    "<td>" . $internationalphone . "</td>" .
                    "</tr>" .
                    "<tr width='150'>" .
                    "<td><strong>Email: </strong></td>" .
                    "<td>" . $email . "</td>" .
                    "</tr>" .
                    "<tr width='150'>" .
                    "<td><strong>Country: </strong></td>" .
                    "<td>" . $country . "</td>" .
                    "</tr>" .
                    "<tr width='150'>" .
                    "<td><strong>Company: </strong></td>" .
                    "<td>" . $company . "</td>" .
                    "</tr>" .
                    "<tr width='150'>" .
                    "<td><strong>Position: </strong></td>" .
                    "<td>" . $position . "</td>" .
                    "</tr>" .
                    "<tr width='150'>" .
                    "<td><strong>Solution: </strong></td>" .
                    "<td>" . $solution . "</td>" .
                    "</tr>" .
                    "<tr>" .
                    "<td colspan='2'><br /><strong>For more information, please use the following contact information:</strong>" .
                    "<br />Email: <a href='mailto:uxui@emad-zedan.com'>uxui@emad-zedan.com</a>" .
                    "<br />Office: Building No. 6427, Prince Muhammad Ibn Saad Ibn Abdulaziz Road, Riyadh, KSA" .
                    "<br />Phone: +92 000 9407" .
                    "<br />Mobile: +962 77 050 2845" .
                    "<br />Website: <a href='https://itisco.com.sa' target='_blank'>itisco.com.sa</a>" .
                    "<br />" .
                    "<br /><a href='https://itisco.com.sa' target='_blank'><img src=".$img1." width='374' height='73' /></a>" .
                                "<br /><br /><a href='#' target='_blank'><img src=".$img2." width='25' height='25'></a>" .
                                "&nbsp;&nbsp;&nbsp;<a href='#' target='_blank'><img src=".$img3." width='25' height='25'></a>" .
                                "&nbsp;&nbsp;&nbsp;<a href='#' target='_blank'><img src=".$img4." width='25' height='25'></a>" .
                                "&nbsp;&nbsp;&nbsp;<a href='#' target='_blank'><img src=".$img5." width='25' height='25'></a>" .
                                "</td>" .
                                "</tr>" .
                                "</table>" .
                                "</div>";

// The content-type header must be set when sending HTML email
$headers1 = "MIME-Version: 1.0" . "\r\n";
$headers1 .= "Content-Type: text/html;Charset=utf-8" . "\r\n";
$headers1 .= "From: " . "uxui@emad-zedan.com";
mail("uxui@emad-zedan.com", $email_subject, $email_message, $headers1);

$mailHead = implode("\r\n", [
    "MIME-Version: 1.0",
    "Content-Type: text/html;Charset=utf-8",
    "From: " . "uxui@emad-zedan.com"
]);

echo mail($email, "ITIS Global", $email_message_confirmation, $mailHead) ? "OK" : "ERROR";

问题排查与解决方案

1. 修复字符串拼接与内容错误

  • 确认图片URL拼接的语法正确性,对图片URL做转义处理,避免特殊字符破坏HTML结构:
    "<img src='".htmlspecialchars($img1)."' width='374' height='73' />"
    
  • 修正邮件内容中的拼写错误:responed改为respond,infromtion改为information,减少触发垃圾邮件过滤的风险。

2. 规范邮件头部格式

  • 统一Content-Type的写法,确保charset前有空格:
    "Content-Type: text/html; charset=utf-8"
    
  • 添加Reply-To头部,提升邮件可信度:
    $mailHead = implode("\r\n", [
        "MIME-Version: 1.0",
        "Content-Type: text/html; charset=utf-8",
        "From: uxui@emad-zedan.com",
        "Reply-To: uxui@emad-zedan.com"
    ]);
    

3. 规避垃圾邮件检测与发送限制

  • 在发送第二封邮件前添加短暂延迟,避免服务器因短时间内发送多封邮件而限制:
    sleep(2); // 延迟2秒再发送确认邮件
    echo mail($email, "ITIS Global", $email_message_confirmation, $mailHead) ? "OK" : "ERROR";
    
  • 简化邮件内容,移除冗余格式,或先发送仅含单张图片的简化版确认邮件,排查是否是内容长度或图片数量导致的问题。

4. 开启调试与日志记录

  • 捕获mail()函数的错误并记录日志,便于定位具体问题:
    $success = mail($email, "ITIS Global", $email_message_confirmation, $mailHead);
    if (!$success) {
        error_log("Failed to send confirmation email to: ".$email);
        echo "ERROR";
    } else {
        echo "OK";
    }
    

内容的提问来源于stack exchange,提问作者Emad Morris Zedan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.15 03:12:03