# Sending emails with MailerSend SDKs (/sdk)



MailerSend ships official SDKs for PHP, Node.js, Python, Go, Ruby, and Java. Pick your language below for the install, configure, and send-an-email steps.

## Prerequisites [#prerequisites]

* A MailerSend account with at least one verified sending domain
* An API token generated in **Settings → API Tokens**
* The runtime for your chosen language installed locally

## Install the SDK and send an email [#install-the-sdk-and-send-an-email]

<Tabs items="['Node.js', 'PHP', 'Python', 'Go', 'Ruby', 'Java']" groupId="sdk-language">
  <Tab value="Node.js">
    <Steps>
      <Step>
        **Install the SDK**

        Run the following command in your project directory to install the MailerSend Node.js SDK and the `dotenv` module:

        ```shell
        npm install mailersend dotenv
        ```
      </Step>

      <Step>
        **Set your API key**

        Create a `.env` file in your project root and add your API key:

        ```shell title=".env"
        MAILERSEND_API_KEY=<your_api_key>
        ```
      </Step>

      <Step>
        **Send an email**

        Create `app.js` with the following:

        ```js title="app.js"
        import 'dotenv/config';
        import { MailerSend, EmailParams, Sender, Recipient } from "mailersend";

        const mailerSend = new MailerSend({
          apiKey: process.env.MAILERSEND_API_KEY,
        });

        const sentFrom = new Sender("you@yourdomain.com", "Your name");
        const recipients = [new Recipient("recipient@example.com", "Recipient name")];

        const emailParams = new EmailParams()
          .setFrom(sentFrom)
          .setTo(recipients)
          .setSubject("Hello from MailerSend")
          .setHtml("<strong>This is the HTML content</strong>")
          .setText("This is the text content");

        mailerSend.email
          .send(emailParams)
          .then((response) => console.log(response))
          .catch((error) => console.log(error));
        ```

        Run it:

        ```shell
        node app.js
        ```
      </Step>
    </Steps>
  </Tab>

  <Tab value="PHP">
    <Steps>
      <Step>
        **Install the SDK**

        The PHP SDK uses PSR-7 and PSR-18, so install an HTTP adapter alongside the SDK:

        ```shell
        composer require php-http/guzzle7-adapter nyholm/psr7
        composer require mailersend/mailersend
        ```
      </Step>

      <Step>
        **Set your API key**

        Initialise the client with your API key:

        ```php
        use MailerSend\MailerSend;

        $mailersend = new MailerSend(['api_key' => 'YOUR_API_KEY']);
        ```
      </Step>

      <Step>
        **Send an email**

        Build the email and send it:

        ```php
        use MailerSend\Helpers\Builder\EmailParams;
        use MailerSend\Helpers\Builder\Recipient;

        $recipients = [
            new Recipient('recipient@example.com', 'Recipient Name'),
        ];

        $email = (new EmailParams())
            ->setFrom('you@yourdomain.com')
            ->setFromName('Your Name')
            ->setRecipients($recipients)
            ->setSubject('Hello from MailerSend')
            ->setHtml('<p>Hello world!</p>')
            ->setText('Hello world!');

        $mailersend->email->send($email);
        ```
      </Step>
    </Steps>
  </Tab>

  <Tab value="Python">
    <Steps>
      <Step>
        **Install the SDK**

        Install the SDK with pip:

        ```shell
        pip install mailersend
        ```
      </Step>

      <Step>
        **Set your API key**

        Export it as an env var:

        ```shell
        export MAILERSEND_API_KEY=<your_api_key>
        ```
      </Step>

      <Step>
        **Send an email**

        ```python
        import os
        from mailersend import emails

        mailer = emails.NewEmail(os.environ['MAILERSEND_API_KEY'])

        mail_body = {}

        mail_from = {
            "name": "Your Name",
            "email": "you@yourdomain.com",
        }

        recipients = [
            {"name": "Recipient Name", "email": "recipient@example.com"},
        ]

        mailer.set_mail_from(mail_from, mail_body)
        mailer.set_mail_to(recipients, mail_body)
        mailer.set_subject("Hello from MailerSend", mail_body)
        mailer.set_html_content("<p>This is the HTML content</p>", mail_body)
        mailer.set_plaintext_content("This is the text content", mail_body)

        mailer.send(mail_body)
        ```
      </Step>
    </Steps>
  </Tab>

  <Tab value="Go">
    <Steps>
      <Step>
        **Install the SDK**

        ```shell
        go get github.com/mailersend/mailersend-go
        ```
      </Step>

      <Step>
        **Set your API key**

        ```go
        import "github.com/mailersend/mailersend-go"

        ms := mailersend.NewMailersend("YOUR_API_KEY")
        ```
      </Step>

      <Step>
        **Send an email**

        ```go
        package main

        import (
            "context"
            "log"

            "github.com/mailersend/mailersend-go"
        )

        func main() {
            ms := mailersend.NewMailersend("YOUR_API_KEY")

            from := mailersend.From{
                Name:  "Your Name",
                Email: "you@yourdomain.com",
            }

            recipients := []mailersend.Recipient{
                {Name: "Recipient Name", Email: "recipient@example.com"},
            }

            message := ms.Email.NewMessage()
            message.SetFrom(from)
            message.SetRecipients(recipients)
            message.SetSubject("Hello from MailerSend")
            message.SetHTML("<p>This is the HTML content</p>")
            message.SetText("This is the text content")

            if _, err := ms.Email.Send(context.TODO(), message); err != nil {
                log.Fatal(err)
            }
        }
        ```
      </Step>
    </Steps>
  </Tab>

  <Tab value="Ruby">
    <Steps>
      <Step>
        **Install the SDK**

        ```shell
        gem install mailersend-ruby
        ```
      </Step>

      <Step>
        **Set your API key**

        Add the token to a `.env` file in your project root, or export it system-wide:

        ```shell title=".env"
        MAILERSEND_API_TOKEN=<your_api_key>
        ```
      </Step>

      <Step>
        **Send an email**

        ```ruby
        require "mailersend-ruby"

        ms_email = Mailersend::Email.new

        ms_email.add_from("email" => "you@yourdomain.com", "name" => "Your Name")
        ms_email.add_recipients("email" => "recipient@example.com", "name" => "Recipient Name")
        ms_email.add_subject("Hello from MailerSend")
        ms_email.add_html("<p>This is the HTML content</p>")
        ms_email.add_text("This is the text content")

        ms_email.send
        ```
      </Step>
    </Steps>
  </Tab>

  <Tab value="Java">
    <Steps>
      <Step>
        **Install the SDK**

        Add the dependency to your build file:

        ```xml title="pom.xml (Maven)"
        <dependency>
            <groupId>com.mailersend</groupId>
            <artifactId>java-sdk</artifactId>
            <version>1.0.0</version>
        </dependency>
        ```

        ```groovy title="build.gradle (Gradle)"
        implementation 'com.mailersend:java-sdk:1.0.0'
        ```
      </Step>

      <Step>
        **Set your API key**

        ```java
        MailerSend ms = new MailerSend();
        ms.setToken("YOUR_API_KEY");
        ```
      </Step>

      <Step>
        **Send an email**

        ```java
        Email email = new Email();
        email.setFrom("Your Name", "you@yourdomain.com");
        email.addRecipient("Recipient Name", "recipient@example.com");
        email.subject = "Hello from MailerSend";
        email.html = "<p>This is the HTML content</p>";
        email.text = "This is the text content";

        MailerSend ms = new MailerSend();
        ms.setToken("YOUR_API_KEY");

        MailerSendResponse response = ms.emails().send(email);
        System.out.println(response.responseStatusCode);
        ```
      </Step>
    </Steps>
  </Tab>
</Tabs>

## What's next? [#whats-next]

* SDK source on GitHub:
  * [mailersend-nodejs](https://github.com/mailersend/mailersend-nodejs)
  * [mailersend-php](https://github.com/mailersend/mailersend-php)
  * [mailersend-python](https://github.com/mailersend/mailersend-python)
  * [mailersend-go](https://github.com/mailersend/mailersend-go)
  * [mailersend-ruby](https://github.com/mailersend/mailersend-ruby)
  * [mailersend-java](https://github.com/mailersend/mailersend-java)
* [Email API Reference](/api/v1/email)
