Skip to content

Instantly share code, notes, and snippets.

@mahircoding
Last active May 30, 2025 17:19
Show Gist options
  • Select an option

  • Save mahircoding/580e39ca4c16cc78091bc797c0ab95d9 to your computer and use it in GitHub Desktop.

Select an option

Save mahircoding/580e39ca4c16cc78091bc797c0ab95d9 to your computer and use it in GitHub Desktop.
send mail with api mailtrap
<?php
function kirim($v, $x, $y, $z) {
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://send.api.mailtrap.io/api/send",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode([
'to' => [
[
'email' => $v,
'name' => $x
]
],
'from' => [
'email' => 'no_reply@brizpress.com', // Replace with your actual email
'name' => 'Jasa Trade'
],
'reply_to' => [
'email' => 'reply@brizpress.com', // Replace with your actual email
'name' => 'Reply'
],
'headers' => [
'X-Message-Source' => 'dev.mydomain.com' // Replace with your actual domain web
],
'subject' => $y,
'text' => $z,
'html' => $z,
]),
CURLOPT_HTTPHEADER => [
"Accept: application/json",
"Authorization: Bearer c271242f455e87fxxxxxxxxx", // Replace with your actual API token
"Content-Type: application/json"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
}
?>
@mahircoding
Copy link
Author

mahircoding commented May 30, 2025

<?php

function kirim($v, $x, $y, $z) {
    // Build payload dynamically
    $payload = [
        "Recipients" => [
            "To" => [
                "$x <$v>"
            ]
        ],
        "Content" => [
            "Body" => [
                [
                    "ContentType" => "HTML",
                    "Content" => $z
                ]
            ],
            "From" => "Jasa Trade <admin@sanghyangnusantara.com>",
            "ReplyTo" => "Jasa Trade <admin@sanghyangnusantara.com>",
            "Subject" => $y
        ],
        "Options" => new stdClass() // Required for empty object
    ];

    // cURL setup
    $curl = curl_init();

    curl_setopt_array($curl, [
        CURLOPT_URL => "https://api.elasticemail.com/v4/emails/transactional",
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING => "",
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 30,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => "POST",
        CURLOPT_POSTFIELDS => json_encode($payload),
        CURLOPT_HTTPHEADER => [
            "Content-Type: application/json",
            "X-ElasticEmail-ApiKey: "
        ],
    ]);

    // Execute request
    $response = curl_exec($curl);
    $err = curl_error($curl);
    curl_close($curl);

    // Output result
    if ($err) {
        echo "cURL Error #:" . $err;
    } else {
        echo $response;
    }
}

?>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment