<html><head></head><body> <img src=”images/company_logo.gif” > , {username}! , , {something} , . <div> : <img src=”images/{something_photo}”></div> </body></html>
email_templates/ template1/ index.html images/ company_logo.gif photo1.png ... photoN.png template2/ ...
class SendEMail { static $transport = false; public $filename = 'index.html', // html $tplFolder = '', // $imgFolder = 'images/', $subject = '', $from = '', $fromName = null, $contentType = 'text/html', $charset = 'utf-8'; private $message, $data; public function __construct($options) { foreach($options as $option => $value) $this->$option = $value; if (!self::$transport) self::$transport = Swift_SmtpTransport::newInstance(); } public function Send($data, $email, $name = null) { $this->data = $data; $this->message = Swift_Message::newInstance(); $mess =& $this->message; // subject , $subject = $this->SubstituteData($this->subject); $body = $this->GetBody(); // email $mess->setTo($email, $name); // $mess->setFrom($this->from, $this->fromName); // $mess->setSubject($subject); // $mess->setBody($body); $mess->setContentType($this->contentType); $mess->setCharset($this->charset); $mailer = Swift_Mailer::newInstance(self::$transport); return $mailer->send($this->message); } private function GetBody() { // $body = file_get_contents($this->tplPath.$this->filename); // $body = $this->SubstituteData($body); // , imgPath jpg, png, gif, src img // 'self::AddImage' php > 5.3, 5.2 array($this, 'AddImage') return preg_replace_callback('/'.preg_quote($this->imgPath, '/').'((.+)\.(jpg|png|gif))/i', 'self::AddImage', $body); } // , src private function AddImage($matches) { $path = $this->tplPath."/".$matches[0]; return $this->message->embed(Swift_Image::fromPath($path)); } // private function SubstituteData($str) { if (empty($this->data)) return $str; foreach($this->data as $k => $v) $str = str_replace($k, $v, $str); return $str; } }
$sm = new SendEMail(array( 'tplPath' => '/path_to_emails/email_templates/template1/', 'subject' => ' ', 'from' => 'noreply@site.com' )); $recipients[0] = array( 'email' => 'dima@blabla.com', 'user' => ' ', 'something' => '', 'something_photo' => 'myalka.jpg' ); $recipients[1] = array( 'email' => 'volodya@blabla.com', 'user' => ' ', 'something' => '', 'something_photo' => 'meshalka.jpg' ); $success = 0; foreach($recipients as $rec) { $data = array('{user}' => $rec['user'], '{something}' => $rec['something'], '{something_photo}' => $rec['something_photo']); $sended = $sm->Send($data, $rec['email'], $rec['user']); if ($sended) $success++; }
Source: https://habr.com/ru/post/172269/
All Articles