0

我需要发布到一个 URL,我正在使用 curl 执行此操作。但问题在于我发布的 HTML 内容。我正在使用我要求发送 html 电子邮件的此页面。所以它将具有内联样式。当我 urlencode() 或 rawurlenocde() 时,这些样式属性被剥离。所以邮件看起来不正确。我怎样才能避免这种情况并按原样发布 HTML?

这是我的代码:

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

2 回答 2

0

这是一个示例,使用http_build_query()从值数组构建您的帖子数据:

<?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);
?>
于 2013-08-27T08:44:17.267 回答
-1

按照这篇文章中的POST描述进行操作: 使用 cURL 传递 $_POST 值

它应该可以解决您的问题。

于 2013-08-27T08:41:55.583 回答