Skip to content

Commit

Permalink
initial commit..
Browse files Browse the repository at this point in the history
  • Loading branch information
yus-ham committed Jun 18, 2023
0 parents commit 262a847
Show file tree
Hide file tree
Showing 4 changed files with 255 additions and 0 deletions.
34 changes: 34 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"name": "yus-ham/yii2-resend",
"description": "Yii 2 extension for [Resend](https://resend.com)",
"keywords": ["yii2", "extension", "resend-php"],
"type": "yii2-extension",
"homepage": "https://github.com/yus-ham/yii2-resend",
"license": "MIT",
"authors": [
{
"name": "Yusup Hambali",
"email": "[email protected]"
}
],
"require": {
"php": ">8.0",
"yiisoft/yii2": "~2.0",
"resend/resend-php": "*"
},
"autoload": {
"psr-4": {
"yusham\\resend\\" : "src"
}
},
"extra": {
"branch-alias": {
"dev-main": "1.x-dev"
}
},
"config": {
"allow-plugins": {
"yiisoft/yii2-composer": true
}
}
}
30 changes: 30 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Yii 2 extension for [Resend](https://resend.com/)


You need this library? just install it through composer

`composer require yus-ham/yii2-resend --prefer-dist -o`


Also don't forget to configure

```php
$config['components']['mailer'] = [
'class' => 'yusham\resend\Mailer',
'useFileTransport' => false,
'viewPath' => '@app/mail',
'transport' => [
'apiKey' => '<YOUR_API_KEY>'
],
];
```


And you can then send an email as usually
```php
Yii::$app->mailer->compose('contact/html')
->setFrom('[email protected]')
->setTo($form->email)
->setSubject($form->subject)
->send();
```
51 changes: 51 additions & 0 deletions src/Mailer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php
/**
* @copyright Yusup Hambali
* @license MIT
*/
namespace yusham\resend;

use Yii;


class Mailer extends \yii\mail\BaseMailer
{
public $messageClass = Message::class;

private $_transport = null;

public function setTransport($transport): void
{
$this->_transport = $transport;
}

/**
* @param \yii\mail\MessageInterface $message
*/
protected function sendMessage($message): bool
{
try {
$resend = \Resend::client($this->_transport['apiKey']);

$resend->emails->send([
'from' => $message->getFrom(),
'to' => implode(', ', $message->getTo()),
'subject' => $message->getSubject(),
'html' => $message->getHtmlBody(),
'attachments' => array_map(function($attachment) {
$content = $attachment['content'] ?? file_get_contents($attachment['file']);
$content = mb_convert_encoding($content, 'UTF-8', mb_detect_encoding($content) ?: 'UTF-8');
$filename = $attachment['fileName'] ?? (isset($attachment['file']) ? basename($attachment['file']) : uniqid());
return compact('content', 'filename');
}, $message->getAttachments()),
]);


return true;
}
catch(\Throwable $t) {
}

return false;
}
}
140 changes: 140 additions & 0 deletions src/Message.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
<?php
/**
* @copyright Yusup Hambali
* @license MIT
*/
namespace yusham\resend;

use Yii;

class Message extends \yii\mail\BaseMessage
{
private $from, $tos = [];
private $text, $html;
private $subject;
private $attachments = [];

public function getFrom()
{
return $this->from;
}

public function setFrom($from): self
{
$this->from = $from;
return $this;
}

public function getTo()
{
return $this->tos;
}

public function setTo($to): self
{
$this->tos[] = $to;
return $this;
}

public function getSubject(): string
{
return (string) $this->subject;
}

public function setSubject($subject): self
{
$this->subject = $subject;
return $this;
}

public function setTextBody($text): self
{
$this->text = $text;
return $this;
}

public function setHtmlBody($html): self
{
$this->html = $html;
return $this;
}

public function getHtmlBody()
{
return $this->html;
}

public function getCharset()
{
throw new \RuntimeException(__METHOD__ .' Not implemented');
}

public function setCharset($charset)
{
throw new \RuntimeException(__METHOD__ .' Not implemented');
}

public function getReplyTo()
{
throw new \RuntimeException(__METHOD__ .' Not implemented');
}

public function setReplyTo($replyTo)
{
throw new \RuntimeException(__METHOD__ .' Not implemented');
}

public function getCc()
{
throw new \RuntimeException(__METHOD__ .' Not implemented');
}

public function setCc($cc)
{
throw new \RuntimeException(__METHOD__ .' Not implemented');
}

public function getBcc()
{
throw new \RuntimeException(__METHOD__ .' Not implemented');
}

public function setBcc($bcc)
{
throw new \RuntimeException(__METHOD__ .' Not implemented');
}

public function attach($fileName, array $options = []): self
{
$options['file'] = Yii::getAlias($fileName);
$this->attachments[] = $options;
return $this;
}

public function attachContent($content, array $options = []): self
{
$options['content'] = $content;
$this->attachments[] = $options;
return $this;
}

public function getAttachments()
{
return $this->attachments;
}

public function embed($fileName, array $options = []): string
{
throw new \RuntimeException(__METHOD__ .' Not implemented');
}

public function embedContent($content, array $options = []): string
{
throw new \RuntimeException(__METHOD__ .' Not implemented');
}

public function toString()
{
return '';
}
}

0 comments on commit 262a847

Please sign in to comment.