Giter Club home page Giter Club logo

mailgun-rs's Introduction

mailgun-rs

An unofficial client library for the Mailgun API

# Cargo.toml
[dependencies]
mailgun-rs = "0.1.11"

Examples

Send with async

See examples/async

$ cd examples/async
$ cargo run

Send a simple email

use mailgun_rs::{EmailAddress, Mailgun, MailgunRegion, Message};
use std::collections::HashMap;

fn main() {
    let domain = "huatuo.xyz";
    let key = "key-xxxxxx";
    let recipient = "[email protected]";

    send_html(recipient, key, domain);
    send_template(recipient, key, domain);
}

fn send_html(recipient: &str, key: &str, domain: &str) {
    let recipient = EmailAddress::address(recipient);
    let message = Message {
        to: vec![recipient],
        subject: String::from("mailgun-rs"),
        html: String::from("<h1>hello from mailgun</h1>"),
        ..Default::default()
    };

    let client = Mailgun {
        api_key: String::from(key),
        domain: String::from(domain),
        message,
    };
    let sender = EmailAddress::name_address("no-reply", "[email protected]");

    match client.send(MailgunRegion::US, &sender) {
        Ok(_) => {
            println!("successful");
        }
        Err(err) => {
            println!("Error: {err}");
        }
    }
}

Send a template email

fn send_template(recipient: &str, key: &str, domain: &str) {
    let mut template_vars = HashMap::new();
    template_vars.insert(String::from("firstname"), String::from("Dongri"));

    let recipient = EmailAddress::address(recipient);
    let message = Message {
        to: vec![recipient],
        subject: String::from("mailgun-rs"),
        template: String::from("template-1"),
        template_vars,
        ..Default::default()
    };

    let client = Mailgun {
        api_key: String::from(key),
        domain: String::from(domain),
        message,
    };
    let sender = EmailAddress::name_address("no-reply", "[email protected]");

    match client.send(MailgunRegion::US, &sender) {
        Ok(_) => {
            println!("successful");
        }
        Err(err) => {
            println!("Error: {err}");
        }
    }
}

mailgun-rs's People

Contributors

dongri avatar xerenahmed avatar benjaminch avatar

Stargazers

 avatar Pal Kerecsenyi avatar  avatar Arto Bendiken avatar Harry avatar open source avatar Kay Gosho avatar Eric avatar Oleks Gnatovskyi avatar Christian Haynes avatar

Watchers

James Cloos avatar  avatar  avatar

mailgun-rs's Issues

Bumping Reqwest version

Hello there !
First of, thanks for this lib, it's very handy :)

I just created a PR #2 to bump reqwest version.
Having old reqwest version is causing some versions clashes eventually in downstream apps / libraries.

Thanks again :)

Allow change the api url

I've been having difficulty with posting to the API. It kept responding with a 'Forbidden' message, and nothing more. After some research, I discovered the reason: in my situation, the API endpoint should start with 'api.eu'.

NOTE: If you’re sending from our EU infrastructure, be sure to substitute the beginning of the endpoint “https://api.mailgun.net” with “https://api.eu.mailgun.net”

from https://documentation.mailgun.com/en/latest/quickstart-sending.html#send-via-api:~:text=NOTE%3A%20If%20you%E2%80%99re%20sending%20from%20our%20EU%20infrastructure%2C%20be%20sure%20to%20substitute%20the%20beginning%20of%20the%20endpoint%20%E2%80%9Chttps%3A//api.mailgun.net%E2%80%9D%20with%20%E2%80%9Chttps%3A//api.eu.mailgun.net%E2%80%9D

Cannot drop a runtime in a context where blocking is not allowed while using Rocket with mailgun-rs

Hi, thanks for an awesome library first of. I tried setting it up, but I encounter some issues while using this with Rocket framework. I get this error:

thread 'rocket-worker-thread' panicked at 'Cannot drop a runtime in a context where blocking is not allowed. This happens when a runtime is dropped from within an asynchronous context.', /Users/marcus.cvjeticanin/.cargo/registry/src/github.com-1ecc6299db9ec823/tokio-1.28.1/src/runtime/blocking/shutdown.rs:51:21
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

This is my POST endpoint in Rocket that basically calls the function send_mail_confirmation() that is basically the send_html() function from your examples:

#[post("/order/save", format = "application/json", data = "<order>")]
pub fn save_order(order: Json<Order<'_>>) -> Json<MailConfirmation> {
    // take the order and use the information to send an email
    send_mail_confirmation(&order);

    let confirmation = MailConfirmation {
        message: "Mail sent successfully!",
    };
    Json(confirmation)
}

Google DMARC initiative.

Hi! I tried to send gmail-to-gmail email but got this on mailgun dashboard.

Unauthenticated email from google.com is not accepted due to domain's DMARC policy. Please contact the administrator of google.com domain if this was a legitimate mail. Please visit https://support.google.com/mail/answer/2451690 to learn about the DMARC initiative. 

Any fix you can suggest?
Otherwise it's not particularly better, comparing to construct mailgun request with Command substitution...

Is it possible to define a template and custom variables

Thanks for putting this together. The JS mailgun API allows you to specify a template and custom variables that can be read within the mailgun template:

        to: email,
        subject: `Welcome to ${appName}`,
        template: 'recovery',
        'v:email': email,

Is it possible to do the same with this library? I don't see them in the Message struct:

pub struct Message {
    pub to: Vec<EmailAddress>,
    pub cc: Vec<EmailAddress>,
    pub bcc: Vec<EmailAddress>,
    pub subject: String,
    pub text: String,
    pub html: String,
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.