Sending an SMTP email in Dart -
i looked through api documentation , language guide, did not see sending emails in dart. checked google groups post, it's quite old dart standards.
is possible do? know can use process class invoke external programs, i'd prefer real dart solution if there's any.
there's library called mailer
, asked for: sends out emails.
set dependency in pubspec.yaml
, run pub install
:
dependencies: mailer:
i give simple example using gmail on local windows machine:
import 'package:mailer/mailer.dart'; main() { var options = new gmailsmtpoptions() ..username = 'kaisellgren@gmail.com' ..password = 'my gmail password'; // if use google app-specific passwords, use 1 of those. // pointed justin in comments, careful store in source code. // careful check public repository. // i'm merely giving simplest example here. // right smtp transport method supported. var transport = new smtptransport(options); // create envelope send. var envelope = new envelope() ..from = 'support@yourcompany.com' ..fromname = 'your company' ..recipients = ['someone@somewhere.com', 'another@example.com'] ..subject = 'your subject' ..text = 'here goes body message'; // finally, send it! transport.send(envelope) .then((_) => print('email sent!')) .catcherror((e) => print('error: $e')); }
the gmailsmtpoptions
helper class. if want use local smtp server:
var options = new smtpoptions() ..hostname = 'localhost' ..port = 25;
you can check here possible fields in smtpoptions
class.
here's example using popular rackspace mailgun:
var options = new smtpoptions() ..hostname = 'smtp.mailgun.org' ..port = 465 ..username = 'postmaster@yourdomain.com' ..password = 'from mailgun';
the library supports html emails , attachments well. check out the example learn how that.
i using mailer
mailgun in production use.
Comments
Post a Comment