-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5401dc3
commit 54dd369
Showing
10 changed files
with
168 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters