Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PSDI Army
yii2
Commits
774c4db8
Commit
774c4db8
authored
Oct 29, 2013
by
Paul Klimov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
'yii\mail\MessageInterface' file related methods interface adjusted to use…
'yii\mail\MessageInterface' file related methods interface adjusted to use options, embed file methods added.
parent
35429fbd
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
170 additions
and
31 deletions
+170
-31
Message.php
extensions/swiftmailer/yii/swiftmailer/Message.php
+52
-4
BaseMessage.php
framework/yii/mail/BaseMessage.php
+0
-18
MessageInterface.php
framework/yii/mail/MessageInterface.php
+30
-6
MessageTest.php
tests/unit/extensions/swiftmailer/MessageTest.php
+81
-2
BaseMailerTest.php
tests/unit/framework/mail/BaseMailerTest.php
+7
-1
No files found.
extensions/swiftmailer/yii/swiftmailer/Message.php
View file @
774c4db8
...
@@ -155,18 +155,66 @@ class Message extends BaseMessage
...
@@ -155,18 +155,66 @@ class Message extends BaseMessage
/**
/**
* @inheritdoc
* @inheritdoc
*/
*/
public
function
attach
ContentAsFile
(
$content
,
$fileName
,
$contentType
=
'application/octet-stream'
)
public
function
attach
File
(
$fileName
,
array
$options
=
[]
)
{
{
if
(
empty
(
$contentType
))
{
$attachment
=
\Swift_Attachment
::
fromPath
(
$fileName
);
$contentType
=
'application/octet-stream'
;
if
(
!
empty
(
$options
[
'fileName'
]))
{
$attachment
->
setFilename
(
$options
[
'fileName'
]);
}
if
(
!
empty
(
$options
[
'contentType'
]))
{
$attachment
->
setContentType
(
$options
[
'contentType'
]);
}
}
$attachment
=
\Swift_Attachment
::
newInstance
(
$content
,
$fileName
,
$contentType
);
$this
->
getSwiftMessage
()
->
attach
(
$attachment
);
$this
->
getSwiftMessage
()
->
attach
(
$attachment
);
}
}
/**
/**
* @inheritdoc
* @inheritdoc
*/
*/
public
function
attachContent
(
$content
,
array
$options
=
[])
{
$attachment
=
\Swift_Attachment
::
newInstance
(
$content
);
if
(
!
empty
(
$options
[
'fileName'
]))
{
$attachment
->
setFilename
(
$options
[
'fileName'
]);
}
if
(
!
empty
(
$options
[
'contentType'
]))
{
$attachment
->
setContentType
(
$options
[
'contentType'
]);
}
$this
->
getSwiftMessage
()
->
attach
(
$attachment
);
}
/**
* @inheritdoc
*/
public
function
embedFile
(
$fileName
,
array
$options
=
[])
{
$embedFile
=
\Swift_EmbeddedFile
::
fromPath
(
$fileName
);
if
(
!
empty
(
$options
[
'fileName'
]))
{
$embedFile
->
setFilename
(
$options
[
'fileName'
]);
}
if
(
!
empty
(
$options
[
'contentType'
]))
{
$embedFile
->
setContentType
(
$options
[
'contentType'
]);
}
return
$this
->
getSwiftMessage
()
->
embed
(
$embedFile
);
}
/**
* @inheritdoc
*/
public
function
embedContent
(
$content
,
array
$options
=
[])
{
$embedFile
=
\Swift_EmbeddedFile
::
newInstance
(
$content
);
if
(
!
empty
(
$options
[
'fileName'
]))
{
$embedFile
->
setFilename
(
$options
[
'fileName'
]);
}
if
(
!
empty
(
$options
[
'contentType'
]))
{
$embedFile
->
setContentType
(
$options
[
'contentType'
]);
}
return
$this
->
getSwiftMessage
()
->
embed
(
$embedFile
);
}
/**
* @inheritdoc
*/
public
function
__toString
()
public
function
__toString
()
{
{
return
$this
->
getSwiftMessage
()
->
toString
();
return
$this
->
getSwiftMessage
()
->
toString
();
...
...
framework/yii/mail/BaseMessage.php
View file @
774c4db8
...
@@ -53,24 +53,6 @@ abstract class BaseMessage extends Object implements MessageInterface
...
@@ -53,24 +53,6 @@ abstract class BaseMessage extends Object implements MessageInterface
/**
/**
* @inheritdoc
* @inheritdoc
*/
*/
public
function
attachFile
(
$fileName
,
$contentType
=
null
,
$attachFileName
=
null
)
{
if
(
!
file_exists
(
$fileName
))
{
throw
new
InvalidParamException
(
'Unable to attach file "'
.
$fileName
.
'": file does not exists!'
);
}
if
(
empty
(
$contentType
))
{
$contentType
=
FileHelper
::
getMimeType
(
$fileName
);
}
if
(
empty
(
$attachFileName
))
{
$attachFileName
=
basename
(
$fileName
);
}
$content
=
file_get_contents
(
$fileName
);
$this
->
attachContentAsFile
(
$content
,
$attachFileName
,
$contentType
);
}
/**
* @inheritdoc
*/
public
function
render
(
$view
,
$params
=
[])
public
function
render
(
$view
,
$params
=
[])
{
{
return
$this
->
getMailer
()
->
render
(
$view
,
$params
);
return
$this
->
getMailer
()
->
render
(
$view
,
$params
);
...
...
framework/yii/mail/MessageInterface.php
View file @
774c4db8
...
@@ -84,18 +84,42 @@ interface MessageInterface
...
@@ -84,18 +84,42 @@ interface MessageInterface
/**
/**
* Attach specified content as file for the email message.
* Attach specified content as file for the email message.
* @param string $content attachment file content.
* @param string $content attachment file content.
* @param string $fileName attachment file name.
* @param array $options options for embed file. Valid options are:
* @param string $contentType MIME type of the attachment file, by default 'application/octet-stream' will be used.
* - fileName: name, which should be used to attach file.
* - contentType: attached file MIME type.
*/
*/
public
function
attachContent
AsFile
(
$content
,
$fileName
,
$contentType
=
'application/octet-stream'
);
public
function
attachContent
(
$content
,
array
$options
=
[]
);
/**
/**
* Attaches existing file to the email message.
* Attaches existing file to the email message.
* @param string $fileName full file name
* @param string $fileName full file name
* @param string $contentType MIME type of the attachment file, if empty it will be suggested automatically.
* @param array $options options for embed file. Valid options are:
* @param string $attachFileName name, which should be used for attachment, if empty file base name will be used.
* - fileName: name, which should be used to attach file.
* - contentType: attached file MIME type.
*/
*/
public
function
attachFile
(
$fileName
,
$contentType
=
null
,
$attachFileName
=
null
);
public
function
attachFile
(
$fileName
,
array
$options
=
[]);
/**
* Attach a file and return it's CID source.
* This method should be used when embedding images or other data in a message.
* @param string $fileName file name.
* @param array $options options for embed file. Valid options are:
* - fileName: name, which should be used to attach file.
* - contentType: attached file MIME type.
* @return string attachment CID.
*/
public
function
embedFile
(
$fileName
,
array
$options
=
[]);
/**
* Attach a content as file and return it's CID source.
* This method should be used when embedding images or other data in a message.
* @param string $content attachment file content.
* @param array $options options for embed file. Valid options are:
* - fileName: name, which should be used to attach file.
* - contentType: attached file MIME type.
* @return string attachment CID.
*/
public
function
embedContent
(
$content
,
array
$options
=
[]);
/**
/**
* Sends this email message.
* Sends this email message.
...
...
tests/unit/extensions/swiftmailer/MessageTest.php
View file @
774c4db8
...
@@ -3,6 +3,7 @@
...
@@ -3,6 +3,7 @@
namespace
yiiunit\extensions\swiftmailer
;
namespace
yiiunit\extensions\swiftmailer
;
use
Yii
;
use
Yii
;
use
yii\helpers\FileHelper
;
use
yii\swiftmailer\Mailer
;
use
yii\swiftmailer\Mailer
;
use
yii\swiftmailer\Message
;
use
yii\swiftmailer\Message
;
use
yiiunit\VendorTestCase
;
use
yiiunit\VendorTestCase
;
...
@@ -26,6 +27,26 @@ class MessageTest extends VendorTestCase
...
@@ -26,6 +27,26 @@ class MessageTest extends VendorTestCase
'mail'
=>
$this
->
createTestEmailComponent
()
'mail'
=>
$this
->
createTestEmailComponent
()
]
]
]);
]);
$filePath
=
$this
->
getTestFilePath
();
if
(
!
file_exists
(
$filePath
))
{
FileHelper
::
createDirectory
(
$filePath
);
}
}
public
function
tearDown
()
{
$filePath
=
$this
->
getTestFilePath
();
if
(
file_exists
(
$filePath
))
{
FileHelper
::
removeDirectory
(
$filePath
);
}
}
/**
* @return string test file path.
*/
protected
function
getTestFilePath
()
{
return
Yii
::
getAlias
(
'@yiiunit/runtime'
)
.
DIRECTORY_SEPARATOR
.
basename
(
get_class
(
$this
))
.
'_'
.
getmypid
();
}
}
/**
/**
...
@@ -45,6 +66,26 @@ class MessageTest extends VendorTestCase
...
@@ -45,6 +66,26 @@ class MessageTest extends VendorTestCase
return
Yii
::
$app
->
getComponent
(
'mail'
)
->
createMessage
();
return
Yii
::
$app
->
getComponent
(
'mail'
)
->
createMessage
();
}
}
/**
* Creates image file with given text.
* @param string $fileName file name.
* @param string $text text to be applied on image.
* @return string image file full name.
*/
protected
function
createImageFile
(
$fileName
=
'test.jpg'
,
$text
=
'Test Image'
)
{
if
(
!
function_exists
(
'imagecreatetruecolor'
))
{
$this
->
markTestSkipped
(
'GD lib required.'
);
}
$fileFullName
=
$this
->
getTestFilePath
()
.
DIRECTORY_SEPARATOR
.
$fileName
;
$image
=
imagecreatetruecolor
(
120
,
20
);
$textColor
=
imagecolorallocate
(
$image
,
233
,
14
,
91
);
imagestring
(
$image
,
1
,
5
,
5
,
$text
,
$textColor
);
imagejpeg
(
$image
,
$fileFullName
);
imagedestroy
(
$image
);
return
$fileFullName
;
}
// Tests :
// Tests :
public
function
testGetSwiftMessage
()
public
function
testGetSwiftMessage
()
...
@@ -83,14 +124,52 @@ class MessageTest extends VendorTestCase
...
@@ -83,14 +124,52 @@ class MessageTest extends VendorTestCase
/**
/**
* @depends testSend
* @depends testSend
*/
*/
public
function
test
CreateAttachm
ent
()
public
function
test
AttachCont
ent
()
{
{
$message
=
$this
->
createTestMessage
();
$message
=
$this
->
createTestMessage
();
$message
->
setTo
(
$this
->
testEmailReceiver
);
$message
->
setTo
(
$this
->
testEmailReceiver
);
$message
->
setFrom
(
'someuser@somedomain.com'
);
$message
->
setFrom
(
'someuser@somedomain.com'
);
$message
->
setSubject
(
'Yii Swift Create Attachment Test'
);
$message
->
setSubject
(
'Yii Swift Create Attachment Test'
);
$message
->
setText
(
'Yii Swift Create Attachment Test body'
);
$message
->
setText
(
'Yii Swift Create Attachment Test body'
);
$message
->
attachContentAsFile
(
'Test attachment content'
,
'test.txt'
);
$message
->
attachContent
(
'Test attachment content'
,
[
'fileName'
=>
'test.txt'
]);
$this
->
assertTrue
(
$message
->
send
());
}
/**
* @depends testSend
*/
public
function
testEmbedFile
()
{
$fileName
=
$this
->
createImageFile
(
'embed_file.jpg'
,
'Embed Image File'
);
$message
=
$this
->
createTestMessage
();
$cid
=
$message
->
embedFile
(
$fileName
);
$message
->
setTo
(
$this
->
testEmailReceiver
);
$message
->
setFrom
(
'someuser@somedomain.com'
);
$message
->
setSubject
(
'Yii Swift Embed File Test'
);
$message
->
setHtml
(
'Embed image: <img src="'
.
$cid
.
'" alt="pic">'
);
$this
->
assertTrue
(
$message
->
send
());
}
/**
* @depends testSend
*/
public
function
testEmbedContent
()
{
$fileName
=
$this
->
createImageFile
(
'embed_file.jpg'
,
'Embed Image File'
);
$message
=
$this
->
createTestMessage
();
$cid
=
$message
->
embedContent
(
file_get_contents
(
$fileName
),
[
'contentType'
=>
'image/jpeg'
]);
$message
->
setTo
(
$this
->
testEmailReceiver
);
$message
->
setFrom
(
'someuser@somedomain.com'
);
$message
->
setSubject
(
'Yii Swift Embed File Test'
);
$message
->
setHtml
(
'Embed image: <img src="'
.
$cid
.
'" alt="pic">'
);
$this
->
assertTrue
(
$message
->
send
());
$this
->
assertTrue
(
$message
->
send
());
}
}
...
...
tests/unit/framework/mail/BaseMailerTest.php
View file @
774c4db8
...
@@ -186,7 +186,13 @@ class Message extends BaseMessage
...
@@ -186,7 +186,13 @@ class Message extends BaseMessage
public
function
addHtml
(
$html
)
{}
public
function
addHtml
(
$html
)
{}
public
function
attachContentAsFile
(
$content
,
$fileName
,
$contentType
=
'application/octet-stream'
)
{}
public
function
attachContent
(
$content
,
array
$options
=
[])
{}
public
function
attachFile
(
$fileName
,
array
$options
=
[])
{}
public
function
embedFile
(
$fileName
,
array
$options
=
[])
{}
public
function
embedContent
(
$content
,
array
$options
=
[])
{}
public
function
__toString
()
public
function
__toString
()
{
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment