发送带有html和图像的邮件

时间:2014-01-24 17:02:07

标签: php html email

我正在尝试发送格式为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

1 个答案:

答案 0 :(得分:3)

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

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

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

Obs。:请记住在公共服务器上托管您的文件,以便每个人都能看到它们。

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


<强>更新

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

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

What are some common HTML email coding mistakes?

此外,为了发送HTML邮件(而不是PlainText),您必须在 Multipart / Alternative MIME 中显示其格式。请参阅this article以获取一般概念并将其更新为您的PHP代码。

相关问题