/Test cases

Message properties

Learn how to test the common properties of an email or SMS message with Mailosaur.

Before you begin

The examples shown below are based on two key assumptions:

  1. That you have already create a basic automated test using our getting started guides.
  2. You have a chosen assertion library that you will use to test the values shown below.

Email subject

Allows you to check that the correct subject will be displayed in a recipient’s mail client.

console.log(message.subject) // "Hello world"
print(message.subject) # "Hello world"
System.out.println(message.subject()); // "Hello world"
Console.WriteLine(message.Subject); // "Hello world"
puts(message.subject) # "Hello world"
print(count($message->subject)); // "Hello world"
fmt.Println(message.Subject) // "Hello world"

Recipients

The to property of a message contains an array of all recipients of a message (excluding those that have been carbon-copied on an email).

console.log(message.to[0].name) // "Jill Smith"
console.log(message.to[0].email) // "jill@example.com"

// Phone number (for SMS tests only)
console.log(message.to[0].phone) // "+15554532452"
print(message.to[0].name) # "Jill Smith"
print(message.to[0].email) # "jill@example.com"

# Phone number (for SMS tests only)
print(message.to[0].phone) # "+15554532452"
System.out.println(message.to().get(0).name()); // "Jill Smith"
System.out.println(message.to().get(0).email()); // "jill@example.com"

// Phone number (for SMS tests only)
System.out.println(message.to().get(0).phone()); // "+15554532452"
Console.WriteLine(message.To[0].Name); // "Jill Smith"
Console.WriteLine(message.To[0].Email); // "jill@example.com"

// Phone number (for SMS tests only)
Console.WriteLine(message.To[0].Phone); // "+15554532452"
puts(message.to[0].name) # "Jill Smith"
puts(message.to[0].email) # "jill@example.com"

# Phone number (for SMS tests only)
puts(message.to[0].phone) # "+15554532452"
print($message->to[0]->name); # "Jill Smith"
print($message->to[0]->email); # "jill@example.com"

// Phone number (for SMS tests only)
print($message->to[0]->phone); # "+15554532452"
fmt.Println(message.To[0].Name)  // "Jill Smith"
fmt.Println(message.To[0].Email) // "jill@example.com"

// Phone number (for SMS tests only)
fmt.Println(message.To[0].Phone) // "+15554532452"

Carbon copy (CC) recipients

console.log(message.cc[0].name) // "Jack Smith"
console.log(message.cc[0].email) // "jack@example.com"
print(message.cc[0].name) # "Jack Smith"
print(message.cc[0].email) # "jack@example.com"
System.out.println(message.cc().get(0).name()); // "Jack Smith"
System.out.println(message.cc().get(0).email()); // "jack@example.com"
Console.WriteLine(message.Cc[0].Name); // "Jack Smith"
Console.WriteLine(message.Cc[0].Email); // "jack@example.com"
puts(message.cc[0].name) # "Jack Smith"
puts(message.cc[0].email) # "jack@example.com"
print($message->cc[0]->name); # "Jack Smith"
print($message->cc[0]->email); # "jack@example.com"
fmt.Println(message.Cc[0].Name)  // "Jack Smith"
fmt.Println(message.Cc[0].Email) // "jack@example.com"

Blind carbon-copy (BCC) recipients

console.log(message.bcc[0].name) // "Bob Smith"
console.log(message.bcc[0].email) // "bob@example.com"
print(message.bcc[0].name) # "Bob Smith"
print(message.bcc[0].email) # "bob@example.com"
System.out.println(message.bcc().get(0).name()); // "Bob Smith"
System.out.println(message.bcc().get(0).email()); // "bob@example.com"
Console.WriteLine(message.Bcc[0].Name); // "Bob Smith"
Console.WriteLine(message.Bcc[0].Email); // "bob@example.com"
puts(message.bcc[0].name) # "Bob Smith"
puts(message.bcc[0].email) # "bob@example.com"
print($message->cc[0]->name); # "Bob Smith"
print($message->cc[0]->email); # "bob@example.com"
fmt.Println(message.Bcc[0].Name)  // "Bob Smith"
fmt.Println(message.Bcc[0].Email) // "bob@example.com"

Sender

The from property of a message contains an array of senders. In almost all cases there will only be 1, however the email specification technically allows for multiple senders to exist.

console.log(message.from[0].name) // "Customer Support"
console.log(message.from[0].email) // "noreply@example.com"

// Phone number (for SMS tests only)
console.log(message.from[0].phone) // "+15554532452"
print(message.sender[0].name) # "Customer Support"
print(message.sender[0].email) # "noreply@example.com"

# Phone number (for SMS tests only)
print(message.sender[0].phone) # "+15554532452"
System.out.println(message.from().get(0).name()); // "Customer Support"
System.out.println(message.from().get(0).email()); // "noreply@example.com"

// Phone number (for SMS tests only)
System.out.println(message.from().get(0).phone()); // "+15554532452"
Console.WriteLine(message.From[0].Name); // "Customer Support"
Console.WriteLine(message.From[0].Email); // "noreply@example.com"

// Phone number (for SMS tests only)
Console.WriteLine(message.From[0].Phone); // "+15554532452"
puts(message.from[0].name) # "Customer Support"
puts(message.from[0].email) # "noreply@example.com"

# Phone number (for SMS tests only)
puts(message.from[0].phone) # "+15554532452"
print($message->from[0]->name); # "Customer Support"
print($message->from[0]->email); # "noreply@example.com"

// Phone number (for SMS tests only)
print($message->from[0]->phone); # "+15554532452"
fmt.Println(message.From[0].Name)  // "Customer Support"
fmt.Println(message.From[0].Email) // "noreply@example.com"

// Phone number (for SMS tests only)
fmt.Println(message.From[0].Phone) // "+15554532452"

Email Headers

Emails contain a set of headers that are used to transmit metadata which is typically not visible to the recipient.

All email headers are available via the metadata.headers property of a message. Each header has both a field and value property.

const headers = message.metadata.headers 
const returnPath = headers.find(
  h => h.field.toLowerCase() === 'return-path'
)

console.log(returnPath.value); // "<rp@example.com>"
headers = message.metadata.headers
return_path = [h for h in headers if h.field.lower() == "return-path"]

print(return_path[0].value) # "<rp@example.com>"
List<MessageHeader> headers = message.metadata().headers();
Optional<MessageHeader> returnPath = headers.stream()
  .filter(h -> h.field().toLowerCase().equals("return-path")).findFirst();

System.out.println(returnPath.get().value()); // "<rp@example.com>"
var headers = message.Metadata.Headers;
var returnPath = headers.First<MessageHeader>(h => h.Field.ToLowerInvariant().Equals("return-path"));

Console.WriteLine(returnPath.Value); // "<rp@example.com>"
headers = message.metadata.headers
idx = headers.find_index{ |h| h.field.downcase == "return-path" }

puts(headers[idx].value) # "<rp@example.com>"
$headers = $message->metadata->headers;
$returnPath = array_values(array_filter($headers, function ($h) {
  return strtolower($h->field) === "return-path";
}))[0];

print($returnPath->value);
headers := message.Metadata.Headers
idx := sort.Search(len(headers), func(i int) bool {
  return strings.ToLower(headers[i].Field) == "return-path"
})

fmt.Println(headers[idx].Value) // "<rp@example.com>"