Skip to content

Instantly share code, notes, and snippets.

@mahircoding
Last active May 30, 2025 17:19
Show Gist options
  • Save mahircoding/580e39ca4c16cc78091bc797c0ab95d9 to your computer and use it in GitHub Desktop.
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' => '[email protected]', // Replace with your actual email
'name' => 'Jasa Trade'
],
'reply_to' => [
'email' => '[email protected]', // 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) {

$mail = new PHPMailer(); 
$mail->IsSMTP(); 
$mail->SMTPDebug = 2; 
$mail->SMTPAuth = true; 
$mail->SMTPSecure = 'ssl'; 
$mail->Host = "smtp.elasticemail.com";
$mail->Port = 465; 
$mail->Username = "332D0E54628454F9510C56CB7E31C54352264A02E9611ADCF43215562EF65437xxxxxxxxxxxxx"; 
$mail->Password = "332D0E54628454F9510C56CB7E31C54352264A02E9611ADCF43215562EF65437xxxxxxxxxxxxx";
$webmaster_email = "[email protected]";
$email= $v;
$name = $x; 
$mail->From = $webmaster_email;
$mail->FromName = "Jasa Trade";
$mail->AddAddress($email,$name);
$mail->AddReplyTo($webmaster_email, $mail->FromName);
$mail->WordWrap = 50;
$mail->AddAttachment("file.jpg");
$mail->IsHTML(true);
$mail->Subject = $y;
$mail->Body = $z;
$mail->Send();

}
?>

@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 <[email protected]>",
            "ReplyTo" => "Jasa Trade <[email protected]>",
            "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