2

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
user2129623
  • 2,167
  • 3
  • 35
  • 64

1 Answers1

3

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.

Edgar Froes
  • 768
  • 8
  • 20
  • thanks, it helped. But as I have used some html formatting which uses bootstrap css class. And I have added link to that css file on top ` ` even in mail that formatting does not persist? any clue for this? – user2129623 Jan 24 '14 at 18:28
  • adding big css code inline is not feasible. I read your article link – user2129623 Jan 24 '14 at 18:31
  • Copy the classes you used in the bootstrap.css file and paste it into the – Edgar Froes Jan 24 '14 at 18:31
  • 1
    You can always synthesize your CSS classes for the minimal you need. Removing like shadows and faking them with borders, so you can preserve your mails identity and styling. Be sure to remove heavier and adornment properties like gradients, shadows, hover effects, etc, and use simple elements like padding, border, background-color, etc. That's how you want to do it, it's effective. – Edgar Froes Jan 24 '14 at 18:35
  • 1
    I don't know if you can do that in PHP, but when i did a massive mail subscription, i had a template in HTML in a specific page, very formatted to the HTML Mail standards, and very light, with all the CSS in it. Then i did read the whole page in my C# code, put it into a variable and sent it in my HTML message body property. It worked like a charm and is a lot more editable, you'll only make changes to your template and the logic behind it to populate with certain properties or whatever. – Edgar Froes Jan 24 '14 at 18:41