public function send(string $mode) : \Jyxo\Mail\Sender\Result
{
// Sending modes
static $modes = [self::MODE_SMTP => true, self::MODE_MAIL => true, self::MODE_NONE => true];
if (!isset($modes[$mode])) {
throw new \InvalidArgumentException(sprintf('Unknown sending mode %s.', $mode));
}
$this->mode = $mode;
// Check of required parameters
if (null === $this->email->from) {
throw new Sender\CreateException('No sender was set.');
}
if (count($this->email->to) + count($this->email->cc) + count($this->email->bcc) < 1) {
throw new Sender\CreateException('No recipient was set.');
}
// Creates a result
$this->result = new Sender\Result();
// Creates an email
$this->create();
$body = trim($this->createdBody);
if (empty($body)) {
throw new Sender\CreateException('No body was created.');
}
// Choose the appropriate sending method
switch ($this->mode) {
case self::MODE_SMTP:
$this->sendBySmtp();
break;
case self::MODE_MAIL:
$this->sendByMail();
break;
case self::MODE_NONE:
// Break missing intentionally
// Break missing intentionally
default:
// No sending
break;
}
// Save the generated source code to the result
$this->result->source = $this->getHeader() . $this->createdBody;
// Flush of created email
$this->createdHeader = [];
$this->createdBody = '';
return $this->result;
}