# Send email

```perl
use Email::Simple;

my $to      = 'mail@example.com';
my $from    = 'me@example.com';
my $subject = 'test';
my $body    = 'This is a test.';

my $email = Email::Simple.create(
    :header[['To', $to], ['From', $from], ['Subject', $subject]],
    :body($body)
);

say ~$email;

# Note that the following will fail without an actual smtp server that
# will accept anonymous emails on port 25 (Not very common anymore).
# Most public email servers now require authentication and encryption.

my $smtp-server = 'smtp.example.com';
my $smtp-port   = 25;

await IO::Socket::Async.connect($smtp-server, $smtp-port).then(
    -> $smtp {
        if $smtp.status {
            given $smtp.result {
                react {
                    whenever .Supply() -> $response {
                        if $response ~~ /^220/ {
                            .print( join "\r\n",
                                "EHLO $smtp-server",
                                "MAIL FROM:<{$email.from}>",
                                "RCPT TO:<{$email.to}>",
                                "DATA", $email.body,
                                '.', ''
                            )
                        }
                        elsif $response ~~ /^250/ {
                            .print("QUIT\r\n");
                            done
                        }
                        else {
                           say "Send email failed with: $response";
                           done
                        }
                    }
                    .close
                }
            }
        }
    }
)
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://trizen.gitbook.io/perl6-rosettacode/programming_tasks/s/send_email.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
