> For the complete documentation index, see [llms.txt](https://docs.sudoplatform.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.sudoplatform.com/guides/email/email-aliases/send-and-receive-aliased-email-messages.md).

# Send & Receive Aliased Email Messages

Email Alias addresses automatically route email messages to and from the user's real email address they have chosen to associate with the alias via the verification process.

The handling of email messages being routed via an Email Alias is different depending on where the users real email address is hosted, those are:

* [**Internal Email Alias**](#internal-email-aliases)**:** Where the real address is another Sudo Platform owned **Email Address** hosted in your Sudo Platform deployment.
* [**External Email Aliases**](#external-email-aliases)**:** Where the real address is an externally hosted email account.

### Internal Email Aliases

An **Internal Email Alias** allows the user to freely send and receive messages via their Alias, without exposing their real email address to other participants in the email chain.

#### Inbound Message Receiving

When the Sudo Platform Email Service receives an email message to an **Internal Email Alias** it is processed in almost an identical manner as emails sent directly to an [Email Address](https://docs.sudoplatform.com/guides/email/email-addresses/send-an-email.md). It will be placed in the `INBOX` of the Email Address and can be distinguished from other messages by the presence of the `emailAliasId` property on the `emailMessage` object. These messages can be retrieved the same way as any other message as described in [Send & Receive Email Messages](https://docs.sudoplatform.com/guides/email/email-addresses/send-an-email.md#receiving-email-messages)

#### Outbound Message Sending

Users can send messages directly using their **Internal Email Alias**. To do this, invoke the `sendEmailMessageViaAlias` method. This works in much the same way as usual, including with the ability to send end-to-end encrypted messages where possible. See [Send & Receive Email Messages](https://docs.sudoplatform.com/guides/email/email-addresses/send-an-email.md) for more detail

{% tabs %}
{% tab title="TypeScript" %}

```typescript
// Collect the input headers, body, attachments and sender email alias id 
// however makes sense for your implementation.
const senderEmailAliasId: string = emailAlias.id 
const emailMessageHeader: InternetMessageFormatHeader = {
    from: { emailAddress: "from@bar.com" }, // Must match emailAlias.aliasAddress
    to: [{ emailAddress: "to@bar.com" }],
    cc: [{ emailAddress: "cc@bar.com" }],
    bcc: [{ emailAddress: "bcc@bar.com" }],
    replyTo: [{ emailAddress: "replyTo@bar.com" }],
    subject: "Example subject line",
}
const emailAttachment: EmailAttachment = {
    filename: "fooAttachment.pdf",
    contentId: uuid.v4(),
    mimeType: "application/pdf",
    inlineAttachment: false,
    data: // ... Some pdf data
}
const inlineAttachment: EmailAttachment = {
    filename: "fooImage.png",
    contentId: uuid.v4(),
    mimeType: "image/png",
    inlineAttachment: true,
    data: // ... Some image data
}
try {
    const input: SendEmailMessageViaAliasInput = {
        senderEmailAliasId: senderEmailAliasId,
        emailMessageHeaders: emailMessageHeader,
        body: "An example email body",
        attachments: [emailAttachment],
        inlineAttachments: [inlineAttachment],
    }
    const result = await emailClient.sendEmailMessageViaAlias(input)
    // `result` contains the identifier and created timestamp associated with the sent email message. You can use this to access the data of the email message.
} catch {
    // Handle/notify user of error 
}

```

{% endtab %}

{% tab title="Swift" %}

```swift
/// Collect the input headers, body, attachments and sender email alias id 
/// however makes sense for your implementation.
let senderEmailAliasId: String = emailAlias.id
let emailMessageHeader = InternetMessageFormatHeader(
    from: EmailAddressDetail(emailAddress: "from@bar.com"), // Must match emailAlias.aliasAddress
    to: [EmailAddressDetail(emailAddress: "to@bar.com")],
    cc: [EmailAddressDetail(emailAddress: "cc@bar.com")],
    bcc: [EmailAddressDetail(emailAddress: "bcc@bar.com")],
    replyTo: [EmailAddressDetail(emailAddress: "replyTo@bar.com")],
    subject: "Example subject line"
)
let emailAttachment = EmailAttachment(
    filename: "fooAttachment.pdf",
    contentId: UUID().uuidString,
    mimetype: "application/pdf",
    inlineAttachment: false,
    data: // ... Some pdf data
)
let inlineAttachment = EmailAttachment(
    filename: "fooImage.png",
    contentId: UUID().uuidString,
    mimetype: "image/png",
    inlineAttachment: true,
    data: // ... Some image data
)
do {
    let input = SendEmailMessageViaAliasInput(
        senderEmailAliasId: senderEmailAliasId,
        emailMessageHeaders: emailMessageHeader,
        body: "An example email body",
        attachments: [emailAttachment],
        inlineAttachments: [inlineAttachment]
    )
    let result = try await emailClient.sendEmailMessageViaAlias(
        withInput: input
    )
    /// `result` contains the identifier and created timestamp associated with the sent email message. You can use this to access the data of the email message.
} catch {
    /// Handle/notify user of error
}
```

{% endtab %}

{% tab title="Kotlin" %}

```kotlin
// Collect the input headers, body, attachments and sender email alias id 
// however makes sense for your implementation.
val senderEmailAliasId: String = emailAlias.id
val emailMessageHeader = InternetMessageFormatHeader(
    from = EmailMessage.EmailAddress("from@bar.com"), // Must match emailAlias.aliasAddress
    to = listOf(EmailMessage.EmailAddress("to@bar.com")),
    cc = listOf(EmailMessage.EmailAddress("cc@bar.com")),
    bcc = listOf(EmailMessage.EmailAddress("bcc@bar.com")),
    replyTo = listOf(EmailMessage.EmailAddress("replyTo@bar.com")),
    subject = "Example subject line",
)
val emailAttachment = EmailAttachment(
    fileName = "fooAttachment.pdf",
    contentId = UUID.randomUUID().toString(),
    mimeType = "application/pdf",
    inlineAttachment = false,
    data = // ... Some pdf data
)
val inlineAttachment = EmailAttachment(
    fileName = "fooImage.png",
    contentId = UUID.randomUUID().toString(),
    mimeType = "image/png",
    inlineAttachment = true,
    data = // ... Some image data
)
launch {
    try {
        val input = SendEmailMessageViaAliasInput(
            senderEmailAliasId = senderEmailAliasId,
            emailMessageHeaders = emailMessageHeader,
            body = "An example email body",
            attachments = listOf(emailAttachment),
            inlineAttachments = listOf(inlineAttachment),
        )
        val result = withContext(Dispatchers.IO) {
            emailClient.sendEmailMessageViaAlias(input)
        }
        // [result] contains the identifier and created timestamp associated with the sent email message. You can use this to access the data of the email message.
    } catch (e: EmailMessageException) {
        // Handle/notify user of exception
    }
}
```

{% endtab %}
{% endtabs %}

### External Email Aliases

An **External Email Alias** handles messages significantly differently to [Internal Email Aliases](#internal-email-aliases). The Sudo Platform Email Service does **not** store a copy of the body or other headers of messages it receives for External Email Aliases. Instead, it adjusts the headers then forwards the message to the user's real email address. There are no methods in the **Sudo Email SDK** associated with sending or receiving messages associated with an External Email Alias.

#### Inbound Message Receiving

The Sudo Platform Email Service must make some changes to the headers of an email before forwarding it to the user's real email address.

For the following example, assume the following:

* The domain set up for Email Aliases in the Sudo Platform is `alias.sudoplatform.com`
* **Email Alias**
  * Alias address: `superman@alias.sudoplatform.com`
  * External email address: `clark.kent@example.com`
* **Sender's address:** `lex.luthor@example.com`

When a message is received that has been sent to `superman@alias.sudoplatform.com` from `lex.luthor@example.com`, the Sudo Platform Email Service makes the following changes to the message headers before forwarding the message to `clark.kent@example.com`:

* The `From` header is changed to `"lex.luthor@example.com via" <superman@alias.sudoplatform.com>`
* The `To` header (or `Cc` if that was used) is changed to `clark.kent@example.com`
* The `Reply-To` header is changed to `replies@alias.sudoplatform.com`

The service will then forward the message to `clark.kent@example.com` and store the message identifier along with the `emailAliasId` and the sender's address securely in order to allow for future replies to be handled.

#### Outbound Message Sending

A user can reply to messages received via their **External Email Alias** from their preferred email client, by the standard email reply flow. The message will be sent to `replies@alias.sudoplatform.com` , processed by the Sudo Platform Email Service, and forwarded to the original sender.

{% hint style="info" %}
Important: A user **cannot initiate an email chain via an External Email Alias**. They can only reply to messages they have already received. That is, there is no way for `superman@alias.sudoplatform.com` to send an email to `lois.lane@example.com` except as a reply to an email from `lois.lane@example.com`.
{% endhint %}

Continuing with the above example, when a message is received that has been sent to `replies@alias.sudoplatform.com`, from `clark.kent@example.com`, the Sudo Platform Email Service will read the `In-Reply-To` header of the message to get the id of the original message and attempt to retrieve the record associated with it. If it is available, it will look up the Email Alias associated with the `emailAliasId` and the original sender's email address. It will then make the following changes to the message headers:

* The `From` header is changed to `superman@alias.sudoplatform.com`
* The `To` header is changed to `lex.luthor@example.com`

The service will also make an effort to remove any other headers that may contain `clark.kent@example.com` . The message is then forwarded to `lex.luthor@example.com` .
