user2129623
user2129623

Reputation: 2257

send mail with html and image in body

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 = "[email protected]";
$mg_reply_to_email = "[email protected]";

$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 <' . '[email protected]' . '>',
                'to'        => '[email protected]',
                '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

Upvotes: 2

Views: 2021

Answers (1)

Edgar Froes
Edgar Froes

Reputation: 778

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.

Upvotes: 3

Related Questions