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.

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

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!