@@ -71,22 +71,57 @@ protected function embedImages(Swift_Mime_SimpleMessage $message, Swift_Mime_Sim
71
71
* see https://github.com/swiftmailer/swiftmailer/issues/139
72
72
*/
73
73
if (strpos ($ src , 'cid: ' ) === false ) {
74
+ $ path = $ this ->getPathFromSrc ($ src );
74
75
75
- $ path = $ src ;
76
+ if ($ this ->fileExists ($ path )) {
77
+ $ entity = \Swift_Image::fromPath ($ path );
78
+ $ message ->setChildren (
79
+ array_merge ($ message ->getChildren (), [$ entity ])
80
+ );
76
81
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 ());
79
83
}
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 ());
87
84
}
88
85
}
89
86
90
87
return $ dom ->saveHTML ();
91
88
}
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
+ }
92
127
}
0 commit comments