0

I need to post to a URL and I am doing this with curl. But the problem is with the HTML content I am posting. I am using this page which I am requesting to send an html email. So it will have inline styles. When I urlencode() or rawurlenocde() these style attribute is stripped. So the mail will not look correct. How can I avoid this and post the HTML as it is ?

This is my code :

            $mail_url  = "to=".$email->uEmail;
            $mail_url .= "&from=info@domain.com";
            $mail_url .= "&subject=".$email_campaign[0]->email_subject;
            $mail_url .= "&type=signleOffer";
            $mail_url .= "&html=".rawurlencode($email_campaign[0]->email_content);

            //open curl request to send the mail
            $ch = curl_init();
            curl_setopt($ch,CURLOPT_URL,$url);
            curl_setopt($ch,CURLOPT_POST,count(5));
            curl_setopt($ch,CURLOPT_POSTFIELDS,$mail_url);
            //execute post
            $result = curl_exec($ch);
Happy Coder
  • 4,255
  • 13
  • 75
  • 152
  • If you are using cURL **POST** correctly you should not need to use `urlencode()` or `rawurlenocde()` please show your code. – Lawrence Cherone Aug 27 '13 at 08:27
  • Areed with @l̕aͨŵƦȆ̴̟̟͙̞ͩ͌͝ƞCͭ̏ȇƇhƐȓ0nè, you shouldn't need to encode it. But even if you did decide to encode it, just use the decode function to get the html back to it's original state (rawurldecode, for example) – Malcolm Diggs Aug 27 '13 at 08:31
  • I have added the code now. But if I don't use these functions, I am loosing the styles. I think it is stripped for security or something during the POST.Rather than that if I don't use these, I am getting an empty mail as the page is using rawurldecode – Happy Coder Aug 27 '13 at 08:32

2 Answers2

0

Here is an example, use http_build_query() to build your post data from an array of values:

<?php 
//Receiver debug
if($_SERVER['REQUEST_METHOD']=='POST'){
    file_put_contents('test.POST.values.txt',print_r($_POST,true));
    /*
    Array
    (
        [to] => example@example.com
        [from] => info@domain.com
        [subject] => subject
        [type] => signleOffer
        [html] => 
        <!DOCTYPE HTML>
        <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
            <title>Mail Template</title>
            <style>.yada{color:green;}</style>
        </head>

        <body>
            <p style="color:red">Red</p>
            <p class="yada">Green</p>
        </body>
        </html>

    )
    */
    die;
}

$curl_to_post_parameters = array(
    'to'=>'example@example.com',
    'from'=>'info@domain.com',
    'subject'=>'subject',
    'type'=>'signleOffer',
    'html'=>'
    <!DOCTYPE HTML>
    <html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>Mail Template</title>
        <style>.yada{color:green;}</style>
    </head>

    <body>
        <p style="color:red">Red</p>
        <p class="yada">Green</p>
    </body>
    </html>
    '
);

$curl_options = array(
    CURLOPT_URL => "http://localhost/test.php",
    CURLOPT_POST => true,
    CURLOPT_POSTFIELDS => http_build_query( $curl_to_post_parameters ), //<<<
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HEADER => false
);

$curl = curl_init();
curl_setopt_array($curl, $curl_options);
$result = curl_exec($curl);

curl_close($curl);
?>
Lawrence Cherone
  • 46,049
  • 7
  • 62
  • 106
-1

Do a POST as described in this post: Passing $_POST values with cURL

It should solve your problem.

Community
  • 1
  • 1
Manu Manjunath
  • 6,201
  • 3
  • 32
  • 31