【问题标题】:send mail with html and image in body在正文中发送带有 html 和图像的邮件
【发布时间】:2014-01-24 17:02:07
【问题描述】:

我正在尝试发送格式为 html 格式并包含图像的邮件。图片在我的服务器上,我在 html 代码中给出了它的路径。问题是,电子邮件仅作为文本发送,不包含图像或任何格式。

代码:

    <link href="http://getbootstrap.com/dist/css/bootstrap.css" rel="stylesheet">
<?php

$mg_api = 'key-3ax6xnjp29jd6ere4gc373sgvjxteol0';
$mg_version = 'api.mailgun.net/v2/';
$mg_domain = "samples.mailgun.org";
$mg_from_email = "ping@test.com";
$mg_reply_to_email = "ping@test.com";

$mg_message_url = "https://".$mg_version.$mg_domain."/messages";


$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);

curl_setopt ($ch, CURLOPT_MAXREDIRS, 3);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, false);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_VERBOSE, 0);
curl_setopt ($ch, CURLOPT_HEADER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);

curl_setopt($ch, CURLOPT_USERPWD, 'api:' . $mg_api);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

curl_setopt($ch, CURLOPT_POST, true);
//curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
curl_setopt($ch, CURLOPT_HEADER, false);

//curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_URL, $mg_message_url);
curl_setopt($ch, CURLOPT_POSTFIELDS,
        array(  'from'      => 'Test <' . 'Test_it@yahoo.com' . '>',
                'to'        => 'test.it@gmail.com',
                'h:Reply-To'=>  ' <' . $mg_reply_to_email . '>',
                'subject'   => 'Thanks for you interest ...',
                'html'      => '<html> <body> <strong> Welcome </strong> <img src="logobeta.png" class="img-responsive" alt="Responsive image"> <div id="footer">
            <div class="container">
                <div class="collapse navbar-collapse">
                    <ul class="nav navbar-nav">
                        <li class="active"><a href="#">What is facebook?</a></li>
                        <li><a href="#about">How does it work?</a></li>
                        <li><a href="#contact">Feedback</a></li>
                        <li><a href="#contact">Contact us</a></li>
                    </ul>
                </div><!--/.nav-collapse -->
            </div>
        </div>
 </body> </html>'
            ));
$result = curl_exec($ch);
curl_close($ch);
$res = json_decode($result,TRUE);
print_r($res);

?>

phpmailer 更新: 在 phpmailer 中,我收到了 Mailer Error: SMTP connect() failed. 错误。

我的问题应该是Hostusernamepassword。它在谈论哪些凭据?

$mail->isSMTP();                                      // Set mailer to use SMTP
$mail->Host = 'smtp1.example.com;smtp2.example.com';  // Specify main and backup server
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = 'jswan';                            // SMTP username
$mail->Password = 'secret';                           // SMTP password
$mail->SMTPSecure = 'tls';                            // Enable encryption, 'ssl' also accepted

【问题讨论】:

标签: php html email


【解决方案1】:

也许您的问题出在图像源中。尽量不要在邮件本身中托管图片,而是在您的网站中托管图片,并将图片的 src 属性指向您的网站/喜欢的图片托管商,如下所示:

<img src="http://www.yoursite.com/images/logobeta.png" ...other properties here... />

您也可以对其他文件执行此操作,因此您可以从用户邮箱中提取一些重量并放入您的文件服务器中,而且您可以随时更改服务器中的图像文件以更改所有发送的邮寄图片,如果您犯了一些错误或想要提高质量,甚至处理受版权保护的图片问题。

Obs.:记得将您的文件托管在公共服务器中,以便每个人都可以看到它们。

我只建议您将 CSS 引用保留在邮件中,在顶部的标签内,以避免出现一些视觉问题。


更新

再次阅读您的问题,我认为您的邮件发送的是纯文本内容,而不是 HTML。

这是一篇非常有趣的文章,可以帮助您解决邮件编码中的常见错误。

What are some common HTML email coding mistakes?

此外,为了发送 HTML 邮件(而不是纯文本),您必须在 Multipart/Alternative MIME 中明确其格式。请参阅this article 了解总体思路并将其更新为您的 PHP 代码。

【讨论】:

  • 谢谢,它有帮助。但是因为我使用了一些使用引导 css 类的 html 格式。而且我在顶部添加了指向该css文件的链接` getbootstrap.com/dist/css/bootstrap.css" rel="stylesheet">` 即使在格式不存在的邮件中?有什么线索吗?
  • 内联添加大的 css 代码是不可行的。我看了你的文章链接
  • 复制您在 bootstrap.css 文件中使用的类并将其粘贴到邮件中的
  • 您可以随时合成您的 CSS 类以满足您的最低需求。去除阴影并用边框伪造它们,这样您就可以保留邮件的身份和样式。一定要去掉渐变、阴影、悬停效果等较重的装饰属性,并使用填充、边框、背景颜色等简单的元素。这就是你想要的,它是有效的。
  • 我不知道你是否可以在 PHP 中做到这一点,但是当我进行大量邮件订阅时,我在特定页面中有一个 HTML 模板,非常符合 HTML 邮件标准,并且非常轻巧,其中包含所有 CSS。然后我确实在我的 C# 代码中阅读了整个页面,将它放入一个变量中并将其发送到我的 HTML 消息正文属性中。它就像一个魅力,并且更易于编辑,您只需更改您的模板及其背后的逻辑以填充某些属性或其他内容。
猜你喜欢
  • 2021-06-12
  • 2014-12-10
  • 2012-04-07
  • 2012-01-24
  • 2011-08-27
  • 1970-01-01
  • 2013-01-05
  • 2013-11-11
  • 2018-05-05
相关资源
最近更新 更多