douzhi1937 2014-01-24 17:02
浏览 175
已采纳

发送带有html和图像的邮件

I am trying to send an mail formatted as html formatting and containing an image. The image is on my server and I have given the path to it in the html code. The problem is, the email is sent as text only and does not contain the image or any formatting.

The code:

    <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);

?>

Update for phpmailer: In phpmailer I got Mailer Error: SMTP connect() failed. error.

My proble is what should be Host, username, password. It is talking about which credentials?

$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条回答 默认 最新

  • douye4254 2014-01-24 17:15
    关注

    Maybe your problem is in the image source. Try not to hosting the image in the mail itself, but in your website and point the src property of your image to your site/favorite images host, like below:

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

    You can do that with other files too, so you take some weight from the users mailbox and put into your files server, and a plus to that is that you can change the image file in the server anytime to change all the sent mails images, if you made some mistake or want to improve the quality, or even handle copyrighted images issues.

    Obs.: remember to host your files in a public server so everyone can see them.

    I only recommend that you keep the CSS references inside the mail, within a tag at the top to avoid some visual issues.


    UPDATE

    Reading again your question, i think that your mail is sending PlainText content, not HTML.

    Here is a very interesting article that can help you with common mistakes in mail coding.

    What are some common HTML email coding mistakes?

    Also, in order to send HTML mail (and not PlainText), you must explicit it's format in Multipart/Alternative MIME. Refer to this article to get the general idea and update it to your PHP code.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序