@@ -71,22 +71,57 @@ protected function embedImages(Swift_Mime_SimpleMessage $message, Swift_Mime_Sim
7171 * see https://github.com/swiftmailer/swiftmailer/issues/139
7272 */
7373 if (strpos ($ src , 'cid: ' ) === false ) {
74+ $ path = $ this ->getPathFromSrc ($ src );
7475
75- $ path = $ src ;
76+ if ($ this ->fileExists ($ path )) {
77+ $ entity = \Swift_Image::fromPath ($ path );
78+ $ message ->setChildren (
79+ array_merge ($ message ->getChildren (), [$ entity ])
80+ );
7681
77- if (filter_var ($ src , FILTER_VALIDATE_URL , FILTER_FLAG_SCHEME_REQUIRED ) === false ) {
78- $ path = $ this ->basePath . $ src ;
82+ $ image ->setAttribute ('src ' , 'cid: ' . $ entity ->getId ());
7983 }
80-
81- $ entity = \Swift_Image::fromPath ($ path );
82- $ message ->setChildren (
83- array_merge ($ message ->getChildren (), [$ entity ])
84- );
85-
86- $ image ->setAttribute ('src ' , 'cid: ' . $ entity ->getId ());
8784 }
8885 }
8986
9087 return $ dom ->saveHTML ();
9188 }
89+
90+ /**
91+ * @param $path
92+ *
93+ * @return bool
94+ */
95+ protected function isUrl ($ path )
96+ {
97+ return filter_var ($ path , FILTER_VALIDATE_URL , FILTER_FLAG_SCHEME_REQUIRED ) !== false ;
98+ }
99+
100+ /**
101+ * @param $src
102+ *
103+ * @return string
104+ */
105+ protected function getPathFromSrc ($ src )
106+ {
107+ if ($ this ->isUrl ($ src )) {
108+ return $ src ;
109+ }
110+
111+ return $ this ->basePath . $ src ;
112+ }
113+
114+ /**
115+ * @param $path
116+ *
117+ * @return bool
118+ */
119+ protected function fileExists ($ path )
120+ {
121+ if ($ this ->isUrl ($ path )) {
122+ return !!@getimagesize ($ path );
123+ }
124+
125+ return file_exists ($ path );
126+ }
92127}
0 commit comments