Skip to content

Commit

Permalink
Adding Mail
Browse files Browse the repository at this point in the history
  • Loading branch information
javad-zobeidi committed Mar 19, 2024
1 parent 5401dc3 commit 54dd369
Show file tree
Hide file tree
Showing 10 changed files with 168 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lib/src/application.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Application extends Container {

server = BaseHttpServer(config: config);

if (config['isolate']) {
if (config['isolate'] != null && config['isolate']) {
server.spawnIsolates(config['isolateCount'] ?? 1);
} else {
server.startServer();
Expand Down
12 changes: 12 additions & 0 deletions lib/src/mail/content.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class Content {
/// The Blade view that represents the text version of the message.
final String? text;

/// The Blade view that should be rendered for the mailable.
final String? html;

const Content({
this.text,
this.html,
});
}
26 changes: 26 additions & 0 deletions lib/src/mail/envelope.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import 'package:mailer/mailer.dart';

class Envelope {
///The address sending the message.
final Address? from;

/// The recipients of the message.
final List<Address> to;

/// The recipients receiving a copy of the message.
final List<dynamic>? cc;

/// The recipients receiving a blind copy of the message.
final List<dynamic>? bcc;

/// The subject of the message.
final String subject;

Envelope({
this.from,
required this.to,
required this.subject,
this.cc,
this.bcc,
});
}
11 changes: 11 additions & 0 deletions lib/src/mail/mail.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import 'package:mailer/mailer.dart';

import 'content.dart';
import 'envelope.dart';

abstract class Mail {
const Mail();
Content content();
Envelope envelope();
List<Attachment>? attachments();
}
101 changes: 101 additions & 0 deletions lib/src/mail/mailable.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import 'package:mailer/mailer.dart' as mailer;
import 'package:mailer/smtp_server.dart';
import 'package:meta/meta.dart';
import 'package:vania/src/mail/mail.dart';
import 'package:vania/vania.dart';

@immutable
class Mailable implements Mail {
const Mailable();

SmtpServer _setupSmtpServer() {
switch (Config().get('mail')['driver']) {
case 'gmail':
return gmail(
Config().get('mail')['username'], Config().get('mail')['password']);
case 'gmailSaslXoauth2':
return gmailSaslXoauth2(Config().get('mail')['username'],
Config().get('mail')['accessToken']);
case 'gmailRelaySaslXoauth2':
return gmail(Config().get('mail')['username'],
Config().get('mail')['accessToken']);
case 'hotmail':
return hotmail(
Config().get('mail')['username'], Config().get('mail')['password']);
case 'mailgun':
return mailgun(
Config().get('mail')['username'], Config().get('mail')['password']);
case 'qq':
return qq(
Config().get('mail')['username'], Config().get('mail')['password']);
case 'yahoo':
return yahoo(
Config().get('mail')['username'], Config().get('mail')['password']);
case 'yandex':
return yandex(
Config().get('mail')['username'], Config().get('mail')['password']);
default:
return SmtpServer(
Config().get('mail')['host'] ?? '',
username: Config().get('mail')['username'] ?? '',
password: Config().get('mail')['password'] ?? '',
port: Config().get('mail')['port'],
ssl: Config().get('mail')['encryption'] == 'ssl',
ignoreBadCertificate:
Config().get('mail')['ignoreBadCertificate'] ?? true,
);
}
}

Future<void> send() async {
final message = mailer.Message();

message.from = envelope().from ??
Address(
Config().get('mail')['from_address'],
Config().get('mail')['from_name'],
);
message.recipients.addAll(envelope().to);

if (envelope().cc != null) {
message.ccRecipients.addAll(envelope().cc!);
}

if (envelope().bcc != null) {
message.ccRecipients.addAll(envelope().bcc!);
}

message.subject = envelope().subject;
message.text = content().text;
message.html = content().html;

if (attachments() != null) {
message.attachments.addAll(attachments()!);
}

try {
await mailer.send(message, _setupSmtpServer());
} catch (e) {
print('Failed to send email: $e');
rethrow;
}
}

@mustBeOverridden
@override
List<mailer.Attachment>? attachments() {
throw UnimplementedError();
}

@mustBeOverridden
@override
Content content() {
throw UnimplementedError();
}

@mustBeOverridden
@override
Envelope envelope() {
throw UnimplementedError();
}
}
4 changes: 2 additions & 2 deletions lib/src/storage/local_storage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class LocalStorage implements StorageDriver {
await file.writeAsBytes(bytes);
return file.path.replaceFirst(storagePath, '');
}

@override
String fullPath(String file) => "$storagePath/$file";
String fullPath(String file) => "$storagePath/$file";
}
2 changes: 1 addition & 1 deletion lib/src/storage/storage_driver.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ abstract class StorageDriver {
);

Future<Uint8List?> get(String fileName);

String fullPath(String file);

Future<bool> exists(String fileName);
Expand Down
5 changes: 5 additions & 0 deletions lib/src/utils/helper.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import 'dart:io';

String storagePath(String file) => '${Directory.current.path}/storage/$file';

String publicPath(String file) => '${Directory.current.path}/public/$file';
8 changes: 8 additions & 0 deletions lib/vania.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,11 @@ export 'package:eloquent/src/query/query_builder.dart';
export 'src/authentication/authentication.dart';
export 'src/authentication/authenticate.dart';
export 'src/cryptographic/hash.dart';

export 'src/mail/mailable.dart';
export 'src/mail/content.dart';
export 'src/mail/envelope.dart';
export 'package:mailer/src/entities/address.dart';
export 'package:mailer/src/entities/attachment.dart';

export 'src/utils/helper.dart';
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ dependencies:
crypto: ^3.0.3
dart_jsonwebtoken: ^2.13.0
eloquent: ^3.0.0
mailer: ^6.1.0
meta: ^1.12.0
mime: ^1.0.5
path: ^1.9.0
Expand Down

0 comments on commit 54dd369

Please sign in to comment.