Was this helpful?
<?php
use Mailtrap\Helper\ResponseHelper;
use Mailtrap\MailtrapClient;
use Mailtrap\Mime\MailtrapEmail;
use Symfony\Component\Mime\Address;
require __DIR__ . '/vendor/autoload.php';
$mailtrap = MailtrapClient::initSendingEmails(
apiKey: getenv('MAILTRAP_API_KEY') // your API key here https://mailtrap.io/settings/api-tokens
);
$email = (new MailtrapEmail())
->from(new Address('sender@example.com'))
->to(new Address('recipient@example.com'))
->subject('Hello from Mailtrap PHP')
->text('Plain text body');
$response = $mailtrap->send($email);
// Access response body as array (helper optional)
var_dump(ResponseHelper::toArray($response));require 'mailtrap'
# Create mail object
mail = Mailtrap::Mail.from_content(
from: { email: 'mailtrap@example.com', name: 'Mailtrap Test' },
to: [
{ email: 'your@email.com' }
],
reply_to: { email: 'support@example.com', name: 'Mailtrap Reply-To' },
subject: 'You are awesome!',
text: 'Congrats for sending test email with Mailtrap!'
)
# Create client and send
client = Mailtrap::Client.new(api_key: 'your-api-key')
client.send(mail)
# You can also pass the request parameters directly
client.send(
from: { email: 'mailtrap@example.com', name: 'Mailtrap Test' },
to: [
{ email: 'your@email.com' }
],
subject: 'You are awesome!',
text: 'Congrats for sending test email with Mailtrap!'
)
import io.mailtrap.client.MailtrapClient;
import io.mailtrap.config.MailtrapConfig;
import io.mailtrap.factory.MailtrapClientFactory;
import io.mailtrap.model.request.emails.Address;
import io.mailtrap.model.request.emails.MailtrapMail;
import java.util.List;
public class MailtrapJavaSDKTest {
private static final String SENDER_EMAIL = "sender@domain.com";
private static final String RECIPIENT_EMAIL = "recipient@domain.com";
public static void main(String[] args) {
final MailtrapConfig config = new MailtrapConfig.Builder()
.token(TOKEN)
.build();
final MailtrapClient client = MailtrapClientFactory.createMailtrapClient(config);
final MailtrapMail mail = MailtrapMail.builder()
.from(new Address(SENDER_EMAIL))
.to(List.of(new Address(RECIPIENT_EMAIL)))
.subject("Hello from Mailtrap Sending!")
.text("Welcome to Mailtrap Sending!")
.build();
// Send an email using Mailtrap Sending API
try {
System.out.println(client.send(mail));
} catch (Exception e) {
System.out.println("Caught exception : " + e);
}
}
}using Mailtrap;
using Mailtrap.Emails.Models;
using Mailtrap.Emails.Requests;
using var mailtrapClientFactory = new MailtrapClientFactory("YOUR_API_TOKEN");
var client = mailtrapClientFactory.CreateClient();
var mail = new SendEmailRequest
{
From = new EmailAddress("sender@yourdomain.com"),
To = new[] { new EmailAddress("recipient@example.com") },
Subject = "Hello from Mailtrap",
TextBody = "Welcome to Mailtrap Email API!"
};
await client.Email().Send(mail);import { MailtrapClient } from "mailtrap";
const apiKey = process.env.MAILTRAP_API_KEY;
const isSandbox = process.env.MAILTRAP_USE_SANDBOX === "true";
const inboxId = isSandbox ? Number(process.env.MAILTRAP_INBOX_ID) : undefined; // required only for sandbox
const client = new MailtrapClient({
token: apiKey,
sandbox: isSandbox,
testInboxId: inboxId, // undefined is ignored for production
});
client
.send({
from: {
name: "Mailtrap Test",
email: isSandbox ? "sandbox@example.com" : "no-reply@your-domain.com",
},
to: [{ email: "recipient@example.com" }],
subject: isSandbox ? "[SANDBOX] Demo email" : "Welcome onboard",
text: "This is a minimal body for demonstration purposes.",
})
.then(console.log)
.catch(console.error);import os
import mailtrap as mt
API_KEY = os.environ["MAILTRAP_API_KEY"]
IS_SANDBOX = os.environ.get("MAILTRAP_USE_SANDBOX", "true").lower() == "true"
INBOX_ID = os.environ.get("MAILTRAP_INBOX_ID")
client = mt.MailtrapClient(
token=API_KEY,
sandbox=IS_SANDBOX,
inbox_id=INBOX_ID, # None is ignored for production
)
# Create mail object
mail = mt.Mail(
sender=mt.Address(email="sender@example.com", name="John Smith"),
to=[mt.Address(email="recipient@example.com")],
subject="You are awesome!",
text="Congrats for sending test email with Mailtrap!",
)
client.send(mail)
<?php
use Mailtrap\Helper\ResponseHelper;
use Mailtrap\MailtrapClient;
use Mailtrap\Mime\MailtrapEmail;
use Symfony\Component\Mime\Address;
require __DIR__ . '/vendor/autoload.php';
$apiKey = getenv('MAILTRAP_API_KEY');
$isSandbox = filter_var(getenv('MAILTRAP_USE_SANDBOX'), FILTER_VALIDATE_BOOL);
$inboxId = $isSandbox ? getenv('MAILTRAP_INBOX_ID') : null; // required only for sandbox
$client = MailtrapClient::initSendingEmails(
apiKey: $apiKey,
isSandbox: $isSandbox,
inboxId: $inboxId // null is ignored for production
);
$email = (new MailtrapEmail())
->from(new Address($isSandbox ? 'sandbox@example.com' : 'no-reply@your-domain.com'))
->to(new Address('recipient@example.com'))
->subject($isSandbox ? '[SANDBOX] Demo email' : 'Welcome onboard')
->text('This is a minimal body for demonstration purposes.');
$response = $client->send($email);
// Access response body as array (helper optional)
var_dump(ResponseHelper::toArray($response));require 'mailtrap'
client = Mailtrap::Client.new(api_key: 'your-api-key', sandbox: true, inbox_id: YOUR_INBOX_ID)
client.send(mail)
# You can also pass the request parameters directly
client.send(
from: { email: 'mailtrap@example.com', name: 'Mailtrap Test' },
to: [
{ email: 'your@email.com' }
],
subject: 'You are awesome!',
text: 'Congrats for sending test email with Mailtrap!'
)import io.mailtrap.client.MailtrapClient;
import io.mailtrap.config.MailtrapConfig;
import io.mailtrap.factory.MailtrapClientFactory;
import io.mailtrap.model.request.emails.Address;
import io.mailtrap.model.request.emails.MailtrapMail;
import java.util.List;
public class MailtrapJavaSDKSandboxTest {
private static final String SENDER_EMAIL = "sender@domain.com";
private static final String RECIPIENT_EMAIL = "recipient@domain.com";
private static final long INBOX_ID = 1L;
public static void main(String[] args) {
final MailtrapConfig config = new MailtrapConfig.Builder()
.token(TOKEN)
.sandbox(true)
.inboxId(INBOX_ID)
.build();
final MailtrapClient client = MailtrapClientFactory.createMailtrapClient(config);
final MailtrapMail mail = MailtrapMail.builder()
.from(new Address(SENDER_EMAIL))
.to(List.of(new Address(RECIPIENT_EMAIL)))
.subject("Hello from Mailtrap Sandbox Sending!")
.text("Welcome to Mailtrap Sandbox Sending!")
.build();
// Send an email using Mailtrap Sending API
try {
System.out.println(client.send(mail));
} catch (Exception e) {
System.out.println("Caught exception : " + e);
}
}
}using var mailtrapClientFactory = new MailtrapClientFactory(apiToken);
IMailtrapClient mailtrapClient = mailtrapClientFactory.CreateClient();
SendEmailRequest request = SendEmailRequest
.Create()
.From("hello@demomailtrap.co", "Mailtrap Test")
.To("world@demomailtrap.co")
.Subject("You are awesome!")
.Text("Congrats for sending test email with Mailtrap!");
SendEmailResponse? response = await mailtrapClient
.Test(inboxId)
.Send(request);+
