Coder Social home page Coder Social logo

mailtm_client's Introduction

Estimated reading time: 2 minutes

Index

  1. Introduction
  2. Example
  3. Usage

Introduction

This package is a simple but complete mail.tm api wrapper, you can use this to save and manage your accounts, as well read all your temporary emails.

Example

See in the example folder

Usage

MailTm

A static class which is mainly needed for storing, creating and loading accounts

Gettings the available domains

To get the available domains use

await MailTm.domains();

Create an account

To create an account you just need to use the MailTm.register method.

Params

  • username: address' username ([email protected], here, abcdqwerty is the username)
  • password: account's password
  • domain: address' domain, you can either get one from MailTm.domains() or, if not given, a random one will be provided.
  • randomStringLength: will be used to generate a random password and/or username if not provided (16 is the default length)
await MailTm.register(password: 'ah yes password');

Returns a Future<AuthenticatedUser> instance

Loading an account

You can load an account by using the MailTm.login method

Params

  • id: Will be used to retrieve the account, ONLY IF it was already retireved using login/register
  • username: Address, only if id was not provided
  • password: Password, only if id was not provided

Returns a Future<AuthenticatedUser> instance

AuthenticatedUser

This class is the one that represents an authenticated account (Having both an account's password and jwt token)

There are many methods, thought, they are pretty simple:

  • messageFrom: Gets a message from its id
  • deleteMessage: Deletes a message
  • readMessage: Marks a message as read
  • unreadMessage: Marks a message as unread
  • messageSource: Gets a message's source (docs.mail.tm for further clarifications)
  • downloadAttachment: Downloads an attachment as a list of bytes
  • delete: Deletes the underlying account
  • update: Updates the underlying account (e.g. its used quota)
  • messagesAt: Gets the messages from a certain page. Each page has 30 messages
  • allMessages: Gets every page from the messages list
  • messages: You can listen to this Stream to get every upcoming message

mailtm_client's People

Contributors

im-trisha avatar

Stargazers

Phil avatar  avatar Terukaze avatar Luca Panteghini avatar

Watchers

 avatar

mailtm_client's Issues

Can not get all messages

Hello,

I'm trying to get all messages from an account:
TMAccount acc = await MailTm.login(address: email, password: pw); print('>>>>> acc.createdAt : ${acc.createdAt}'); final msgs = await acc.getAllMessages();

But this error is thrown:

I/flutter ( 3427): >>>>> acc.createdAt : 2023-11-17 11:32:59.000Z I/flutter ( 3427): ══╡ EXCEPTION CAUGHT BY FLUTTER TEST FRAMEWORK ╞════════════════════════════════════════════════════ I/flutter ( 3427): The following _TypeError was thrown running a test: I/flutter ( 3427): type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'Iterable<dynamic>' I/flutter ( 3427): I/flutter ( 3427): When the exception was thrown, this was the stack: I/flutter ( 3427): #0 new TMMessage._fromMap (package:mailtm_client/src/models/message.dart:119:46) I/flutter ( 3427): #1 messageFromJson (package:mailtm_client/src/models/message.dart:7:15) I/flutter ( 3427): #2 TMAccount._getMessages (package:mailtm_client/src/models/account.dart:131:18) I/flutter ( 3427): <asynchronous suspension> ...

Mercure connection failed with 401 while listening

I tried use

account.messages.listen((event){
setState((){
items.add(event.subject);
});
});

but while listening error occured :
[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: Mercure: Connection failed with 401 status code for https://mercure.mail.tm/.well-known/mercure?topic=%2Faccounts%2F64dfeeecf629(some id here)

My code
main_screen.dart

import 'dart:async';

import 'package:flutter/material.dart';
import 'package:mailtm_client/mailtm_client.dart';
import '../providers/mailtm_client_provider.dart';
import '../widgets/main_appbar_widget.dart';
import '../widgets/main_drawer_widget.dart';
import 'package:provider/provider.dart';

class MainScreen extends StatefulWidget {
  const MainScreen({super.key});

  @override
  // ignore: library_private_types_in_public_api
  _MainScreen createState() => _MainScreen();
}

class _MainScreen extends State<MainScreen> {
  List<String> items = [];
  late StreamSubscription streamSubscription;
  @override
  void initState() {
    super.initState();
    final mailtmProvider = Provider.of<MailTMClient>(context, listen: false);

    streamSubscription =
        mailtmProvider.userAccount!.messages.listen((event) async {
      setState(() {
        items.add(event.subject);
      });
      await streamSubscription.cancel();
    });
  }

  @override
  void dispose() {
    super.dispose();
    streamSubscription!.cancel();
  }

  @override
  Widget build(BuildContext context) {

    return Scaffold(
        appBar: const MainAppBar(),
        drawer: const MainDrawerWidget(),
        body: ListView.builder(
            itemCount: items.length,
            itemBuilder: (BuildContext context, int index) {
              return ListTile(title: Text(items[index]));
            }));
  }
}

mailtm_client_provider.dart

import 'dart:async';

import 'package:flutter/material.dart';
import 'package:mailtm_client/mailtm_client.dart';

class MailTMClient extends ChangeNotifier {
   TMAccount? userAccount;
  Future<TMAccount> register(
      String username, String password, TMDomain domain) async {
    return await MailTm.register(
        username: username, password: password, domain: domain);
  }

  Future<TMAccount> login(String address, String password) async {
    return await MailTm.login(address: address, password: password);
  }

  Future<List<TMDomain>> getDomains() async {
    return await TMDomain.domains;
  }


}

login.dart

final TMAccount account = await mailtmProvider.login(username, password);
mailtmProvider.userAccount = account;

sorry if my explanation is not good.
Thanks.

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.