php [已关闭]

6ioyuze2  于 5个月前  发布在  PHP
关注(0)|答案(1)|浏览(42)

已关闭。此问题需要更多focused。目前不接受回答。
**要改进此问题吗?**更新此问题,使其仅针对editing this post的一个问题。

8天前关闭
Improve this question
我不太擅长PHP,我正在尝试使用Postmark App发送一封测试邮件......他们给了我这个curl示例......我正在尝试将其转化为PHP函数

curl "https://api.postmarkapp.com/email" \
  -X POST \
  -H "Accept: application/json" \
  -H "Content-Type: application/json" \
  -H "X-Postmark-Server-Token: TOKEN_HERE" \
  -d '{
        "From": "FROM EMAIL",
        "To": "TO EMAIL",
        "Subject": "Hello from Postmark",
        "HtmlBody": "<strong>Hello</strong> dear Postmark user.",
        "MessageStream": "outbound"
      }'

字符串
我想把它变成一个函数,这样我就可以这样调用它了

rccMail("FROM EMAIL", "TO EMAIL");


有人能帮帮我吗?拜托。

nnt7mjpx

nnt7mjpx1#

不要忘记更换TOKEN_HERE

function rccMail($from, $to, $subject, $htmlBody){    
    // Set up the data to be sent in the request
    $data = [
        "From" => $from,
        "To" => $to,
        "Subject" => $subject,
        "HtmlBody" => $htmlBody,
        "MessageStream" => "outbound"
    ];

    // Initialize a cURL session
    $curl = curl_init("https://api.postmarkapp.com/email");

    // Set the cURL options
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
    curl_setopt($curl, CURLOPT_HTTPHEADER, [
        "Accept: application/json",
        "Content-Type: application/json",
        "X-Postmark-Server-Token: TOKEN_HERE"
    ]);

    // Execute the cURL request and capture the response
    $response = curl_exec($curl);

    // Check for errors
    if(curl_errno($curl)){
        echo 'Curl error: ' . curl_error($curl);
    }

    // Close the cURL session
    curl_close($curl);

    // Output the response
    return $response;
}

字符串

相关问题