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

PHPMailer使用Gmail发邮件到Yahoo 确认按钮无法点击问题求解

问题产生原因
  • 雅虎邮箱的HTML渲染兼容规则更严格,不支持<a>标签嵌套<button>标签的写法,Gmail对这类不规范写法容错率高可以正常识别,但雅虎会直接屏蔽该结构的交互能力,甚至剥离相关标签。
  • 代码中CSS属性书写错误,按钮样式里写的pointer: cursor是无效属性,正确应为cursor: pointer,雅虎对错误CSS的容错率低,会导致样式和交互失效。
  • 当前跳转链接使用localhost本地地址,即使交互正常,雅虎收件方也无法访问你的本地服务完成验证,属于隐性问题。
修复方案
  1. 取消<a>和<button>的嵌套结构,直接用<a>标签模拟按钮样式,这是核心修复逻辑
  2. 修正错误的CSS属性,替换本地localhost链接为你的服务公网域名/IP
  3. 给<a>标签增加display: inline-block、text-decoration: none等兼容属性,保证样式在各邮箱端正常渲染

修改后的核心代码示例

$sentTo = $email;
$message = "Confirm registration";
$mail = new PHPMailer;

//$mail->SMTPDebug = 2;

$mail->isSMTP();                                      // Set mailer to use SMTP
$mail->Host = 'smtp.gmail.com';                       // Specify main and backup server
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = 'myusername';                      // SMTP username
$mail->Password = 'password';                       // SMTP password
$mail->SMTPSecure = 'tls';                            // Enable encryption, 'ssl' also accepted
$mail->Port = 587;                                    //Set the SMTP port number - 587 for authenticated TLS
$mail->setFrom('myusername@gmail.com', 'WEBSITE');     //Set who the message is to be sent from
$mail->addAddress($sentTo);                           // Add a recipient
$mail->WordWrap = 50;                                 // Set word wrap to 50 characters
$mail->isHTML(true);                                  // Set email format to HTML

$mail->Subject = "Travel Smart - Confirm Registration - ";
// 此处为修改后的HTML内容
$mail->Body    = "<div style='padding: 10px; border-radius: 10px; height: auto; background-image:linear-gradient(to right, rgba(287, 202, 192), rgba(142, 65, 98));opacity:0.8;'><h2 style='font-family:cursive'> WELCOME! </h2> <br> <a href='https://替换为你的公网域名/log.php?unique_id=$ran_id' style='display:inline-block;color:white; background:black; cursor: pointer; padding: 10px; border-radius: 20px;text-decoration:none;'>". $message."</a><br></div>";
$mail->AltBody = $message;

if(!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
    exit;
}

内容的提问来源于stack exchange,提问作者deniz_info

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.24 20:36:03