【问题标题】:Send HTML in email via PHP通过 PHP 在电子邮件中发送 HTML
【发布时间】:2012-06-29 15:17:48
【问题描述】:

如何使用 PHP 发送带有图片的 HTML 格式的电子邮件?

我想要一个包含一些设置和 HTML 输出的页面,通过电子邮件发送到一个地址。我该怎么办?

主要问题是附加文件。我该怎么做?

【问题讨论】:

  • 您的问题是发送HTML 代码吗?还是一般使用 PHP 发送电子邮件?

标签: php html email send


【解决方案1】:

这很简单,把图片留在服务器上,然后将 PHP + CSS 发送给他们......

$to = 'bob@example.com';

$subject = 'Website Change Reqest';

$headers = "From: " . strip_tags($_POST['req-email']) . "\r\n";
$headers .= "Reply-To: ". strip_tags($_POST['req-email']) . "\r\n";
$headers .= "CC: susan@example.com\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=UTF-8\r\n";

$message = '<p><strong>This is strong text</strong> while this is not.</p>';


mail($to, $subject, $message, $headers);

正是这一行告诉寄件人和收件人,电子邮件包含(希望)格式良好的 HTML,它需要解释:

$headers .= "Content-Type: text/html; charset=UTF-8\r\n";

这是我得到信息的链接.. (link...)

不过,你需要安全……

【讨论】:

  • html文件需要的css格式、图片等js等文件呢?
  • @UmerHayat 同意了,但我们都必须从某个地方开始,即使只是学习如何使用 Google 的东西......
  • @Abadis - CSS 可以通过任何一种方式完成,嵌入到文件中,或者像往常一样使用 HTML 文件在外部完成。图像可以包含我认为(我从未做过)或留在服务器上并链接到。如果您不确定它们是如何工作的,请查找 HTML 图像:P
  • 切勿使用 htmlheadbody 元素,否则您将在实际上遵循 (X)HTML 标准的基于 Web 的电子邮件系统上造成绝对混乱。另外 if 发送 HTML 电子邮件 never 发送大于 h2 的标头级别,因为您的电子邮件主题(在适当的系统上)将在页面上的单个 h1 元素中.如果这没有意义,请记住 Google 试图将您的页面解释为只有一个主要标题(例如“我们赢得了战争!”)和许多较小标题(例如“被打扰的驼鹿拯救的小猫”)的报纸。
  • 此部分“$message .=”中有错字。这个变量之前没有声明过。
【解决方案2】:

您需要使用图像的绝对路径对 html 进行编码。通过绝对路径意味着您必须将图像上传到服务器中,并且在图像的src 属性中您必须提供像这样&lt;img src="http://yourdomain.com/images/example.jpg"&gt; 这样的直接路径。

以下是供您参考的 PHP 代码:- 取自 http://www.php.net/manual/en/function.mail.php

<?php
// multiple recipients
$to  = 'aidan@example.com' . ', '; // note the comma
$to .= 'wez@example.com';

// subject
$subject = 'Birthday Reminders for August';

// message
$message = '
  <p>Here are the birthdays upcoming in August!</p>
';

// To send HTML mail, the Content-type header must be set
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=UTF-8' . "\r\n";

// Additional headers
$headers .= 'To: Mary <mary@example.com>, Kelly <kelly@example.com>' . "\r\n";
$headers .= 'From: Birthday Reminder <birthday@example.com>' . "\r\n";


// Mail it
mail($to, $subject, $message, $headers);
?>

【讨论】:

  • 你的问题回答了我的问题,谢谢你的详尽!
【解决方案3】:

我有这个代码,它可以完美地运行在我的网站上

  public function forgotpassword($pass,$name,$to)
    {
        $body ="<table  width=100% border=0><tr><td>";
        $body .= "<img width=200 src='";
        $body .= $this->imageUrl();
        $body .="'></img></td><td style=position:absolute;left:350;top:60;><h2><font color = #346699>PMS Pvt Ltd.</font><h2></td></tr>";
        $body .='<tr><td colspan=2><br/><br/><br/><strong>Dear '.$name.',</strong></td></tr>';
        $body .= '<tr><td colspan=2><br/><font size=3>As per Your request we send Your Password.</font><br/><br/>Password is : <b>'.$pass.'</b></td></tr>';
        $body .= '<tr><td colspan=2><br/>If you have any questions, please feel free to contact us at:<br/><a href="mailto:support@pms.com" target="_blank">support@pms.com</a></td></tr>';
        $body .= '<tr><td colspan=2><br/><br/>Best regards,<br>The PMS Team.</td></tr></table>';
        $subject = "Forgot Password";
        $this->sendmail($body,$to,$subject);
    }

邮件功能

   function sendmail($body,$to,$subject)
        {
            //require_once 'init.php';


            $from='testing@gmail.com';      
            $headersfrom='';
            $headersfrom .= 'MIME-Version: 1.0' . "\r\n";
            $headersfrom .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
            $headersfrom .= 'From: '.$from.' '. "\r\n";
            mail($to,$subject,$body,$headersfrom);
        }

image url 函数用于如果你想改变你只在一个函数中改变的图像我有很多邮件功能,比如忘记密码在那里创建用户,因为我使用 image url 函数你可以直接设置路径。

function imageUrl()
    {
        return "http://".$_SERVER['SERVER_NAME'].substr($_SERVER['SCRIPT_NAME'], 0, strrpos($_SERVER['SCRIPT_NAME'], "/")+1)."images/capacity.jpg";
    }

【讨论】:

  • 谢谢,这正是我需要的
  • @Jalpesh 那么对于 CSS 文件,您会做同样的事情吗?引用绝对路径,它应该可以工作,对吧?
  • @FernandoSilva 你必须设置绝对路径,还必须将类或 id 设置为元素,这样你的 css 才能工作
  • @Jalpesh 认为我必须尝试其他方法,因为 Hotmail 删除了所有浮动 CSS 规则,Gmail 只是清除了我的课程。不确定我是否遗漏了一些东西,但我已经为数据准备了 HTML,而 css 负责布局。我正在使用 PHPMailer 类来发送电子邮件,但我找不到任何关于由它引起的这种行为的参考,但我不确定。有什么想法吗?顺便说一句,使用内联样式正确显示图像。
【解决方案4】:

发送 html 电子邮件与使用 php 发送普通电子邮件没有太大区别。需要添加的是 php mail() 函数的 header 参数中的内容类型。这是一个例子。

<?php
    $to = "toEmail@domain.com";
    $subject = "HTML email";
    $message = "
    <html>
    <head>
    <title>HTML email</title>
    </head>
    <body>
    <p>A table as email</p>
    <table>
    <tr>
    <th>Firstname</th>
    <th>Lastname</th>
    </tr>
    <tr>
    <td>Fname</td>
    <td>Sname</td>
    </tr>
    </table>
    </body>
    </html>
    ";
    // Always set content-type when sending HTML email
    $headers = "MIME-Version: 1.0" . "\r\n";
    $headers .= "Content-type:text/html;charset=UTF-8" . "\r\b";
    $headers .= 'From: name' . "\r\n";
    mail($to,$subject,$message,$headers);
?>

您也可以查看here 了解 w3schools 的更详细说明

【讨论】:

    【解决方案5】:

    您可以通过 PHP 轻松发送带有 HTML 内容的电子邮件。使用以下脚本。

    <?php
    $to = 'user@example.com';
    $subject = "Send HTML Email Using PHP";
    
    $htmlContent = '
    <html>
    <body>
        <h1>Send HTML Email Using PHP</h1>
        <p>This is a HTMl email using PHP by CodexWorld</p>
    </body>
    </html>';
    
    // Set content-type header for sending HTML email
    $headers = "MIME-Version: 1.0" . "\r\n";
    $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
    
    // Additional headers
    $headers .= 'From: CodexWorld<info@codexworld.com>' . "\r\n";
    $headers .= 'Cc: welcome@example.com' . "\r\n";
    $headers .= 'Bcc: welcome2@example.com' . "\r\n";
    
    // Send email
    if(mail($to,$subject,$htmlContent,$headers)):
        $successMsg = 'Email has sent successfully.';
    else:
        $errorMsg = 'Email sending fail.';
    endif;
    ?>
    

    源代码和现场演示可以从这里找到 - Send Beautiful HTML Email using PHP

    【讨论】:

      【解决方案6】:

      最简单的方法可能是使用 Zend Framework 或任何其他框架,如 CakePHP 或 Symphony。

      您也可以使用标准的mail 函数来做到这一点,但您需要更多关于如何附加图片的知识。

      或者,只需将图像托管在服务器上,而不是附加它们。发送 HTML 邮件记录在 mail 函数文档中。

      【讨论】:

      • @gonatee 取决于您想要的质量水平。除了最基本的情况外,公认的答案并不适用于所有情况。如果您只需要基本案例,那么使用mail() 就可以了。如果您需要更可靠的解决方案或有复杂的情况或需求,最好依赖已经通过数十个 RFC 文档和多年错误修复的框架或库。如果您能找到一个专门的库或框架让您挑选您想要的功能,那么就没有理由依赖我们自己来解决所有问题。
      【解决方案7】:

      使用 PHPMailer,

      要发送 HTML 邮件,您只需设置 $mail->isHTML(),您可以使用 HTML 标签设置正文

      这是一个写得很好的教程:

      rohitashv.wordpress.com/2013/01/19/how-to-send-mail-using-php/

      【讨论】:

        【解决方案8】:

        诀窍是在构建 html 正文部分时知道 Image mime 部分的内容 id。 归结为制作img标签

        https://github.com/horde/horde/blob/master/kronolith/lib/Kronolith.php

        查看函数 buildMimeMessage 以获得一个工作示例。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-01-05
          • 1970-01-01
          • 1970-01-01
          • 2022-10-23
          • 1970-01-01
          • 1970-01-01
          • 2012-07-03
          相关资源
          最近更新 更多