diff --git a/example/AttachmentImg.png b/example/AttachmentImg.png new file mode 100644 index 0000000..80d0493 Binary files /dev/null and b/example/AttachmentImg.png differ diff --git a/example/sendMail.php b/example/sendMail.php new file mode 100644 index 0000000..b2a6592 --- /dev/null +++ b/example/sendMail.php @@ -0,0 +1,50 @@ + 465, + //Replace server address with your mail server address + 'server-address' => 'mail.example.com', + //Replace server username with your mail server username + 'user' => 'test@example.com', + 'pass' => 'KnvcbxFYCz77', + 'sender-name' => 'Ibrahim', + //Replace sender address with your mail server sender address + 'sender-address' => 'test@example.com', + 'account-name' => 'no-reply' +]); + +//Second, create your actual email. using the account that was just created to +//send messages. +$email = new EmailMessage($smtp); + +//Set subject +$email->setSubject('Hello World From PHP 😀'); + +//Optionally, set priority +$email->setPriority(1); + +//Specify who will receive the message +$email->addTo('super-megaman-x@outlook.com'); + +//Add optional attachments +$email->addAttachment(__DIR__.DIRECTORY_SEPARATOR.'AttachmentImg.png'); + +//Build your HTML Message +$div = $email->insert('div'); +$div->addChild('p')->text('Hello World Message'); +$div->addChild('p', [ + 'style' => [ + 'font-weight' => 'bold', + 'color' => 'red' + ] +])->text('This is just a test message.'); + +//Finally, send. +$email->send(); + +echo 'sent'; \ No newline at end of file diff --git a/tests/webfiori/tests/mail/EmailMessageTest.php b/tests/webfiori/tests/mail/EmailMessageTest.php index 0c7fdc0..f72ded5 100644 --- a/tests/webfiori/tests/mail/EmailMessageTest.php +++ b/tests/webfiori/tests/mail/EmailMessageTest.php @@ -5,6 +5,7 @@ use webfiori\email\EmailMessage; use webfiori\email\SMTPAccount; use webfiori\email\SMTPServer; +use webfiori\email\exceptions\SMTPException; /** * A test class for testing the class 'webfiori\framework\mail\EmailMessage'. * @@ -29,6 +30,15 @@ class EmailMessageTest extends TestCase { 'sender-address' => 'test@programmingacademia.com', 'account-name' => 'no-reply2' ]; + private $acc02 = [ + 'port' => 465, + 'server-address' => 'mail.programmingacademia.com', + 'user' => 'test@programmingacademia.com', + 'pass' => '2233', + 'sender-name' => 'Ibrahim', + 'sender-address' => 'test@programmingacademia.com', + 'account-name' => 'no-reply2' + ]; /** * @test */ @@ -149,47 +159,16 @@ public function testSend00() { /** * @test */ -// public function test01() { -// $smtp = new SMTPAccount(); -// $smtp->setAccountName('smtp-acc-00'); -// //$smtp->setServerAddress('mail.invalid.com'); -// WebFioriApp::getAppConfig()->addAccount($smtp); -// $this->expectException(\Exception::class); -// $this->expectExceptionMessage('The account "smtp-acc-00" has invalid host or port number. Port: 465, Host: .'); -// $message = new EmailMessage('smtp-acc-00'); -// } - /** - * @test - */ -// public function test02() { -// $this->expectException(\Exception::class); -// $this->expectExceptionMessage('The account "smtp-acc-00" has invalid host or port number. Port: 255, Host: mail.programmingacademia.com.'); -// $smtp = new SMTPAccount(); -// $smtp->setPassword('iz1Iimu#z'); -// $smtp->setAddress('test@programmingacademia.com'); -// $smtp->setUsername('test@programmingacademia.com'); -// $smtp->setServerAddress('mail.programmingacademia.com '); -// $smtp->setPort(255); -// $smtp->setAccountName('smtp-acc-00'); -// WebFioriApp::getAppConfig()->addAccount($smtp); -// $message = new EmailMessage('smtp-acc-00'); -// } - /** - * @test - */ -// public function test03() { -// $this->expectException(\Exception::class); -// $this->expectExceptionMessage('The account "smtp-acc-00" has invalid host or port number. Port: 765765, Host: mail.programmingacademia.com.'); -// $smtp = new SMTPAccount(); -// $smtp->setPassword('izimu#z'); -// $smtp->setAddress('test@programmingacademia.com'); -// $smtp->setUsername('test@programmingacademia.com'); -// $smtp->setServerAddress('mail.programmingacademia.com '); -// $smtp->setPort(765765); -// $smtp->setAccountName('smtp-acc-00'); -// WebFioriApp::getAppConfig()->addAccount($smtp); -// $message = new EmailMessage('smtp-acc-00'); -// $this->assertTrue($message instanceof EmailMessage); -// } - + public function testSend01() { + $this->expectException(SMTPException::class); + $this->expectExceptionMessage('Unable to login to SMTP server: 535 Incorrect authentication data'); + $message = new EmailMessage(new SMTPAccount($this->acc02)); + $message->setSubject('Test Email From WebFiori'); + $message->setPriority(1); + $message->insert('p')->text('Super test message.'); + $message->addTo('ibinshikh@outlook.com'); + + $message->send(); + + } }