📜 ⬆️ ⬇️

Letters in html-format with attached images

There is a reasonable question: why attach images, as this makes heavier letters and increases traffic? And to avoid this:

image

This type occurs when the user reads the received correspondence offline or the email client does not open pictures from external sources, for example, because of its security settings. In short, it looks like it can be readable, but not aesthetically pleasing.
')
If you attach pictures to the letter, then it will acquire a complete look, and there will be no holes anymore, if no mistakes are made at the layout stage.

So, you need to be able to create full-fledged html-templates of letters with pictures for their further use in the program (mailing, automatic notifications, etc.). In the future, the person who is engaged only in layout can prepare the templates without interfering with the program code.

The task is typical, one can say ordinary, and the implementations have been invented quite a few. And in order not to generate the 100,500th fan, let's turn to the notorious Swift Mailer , slightly expanding its functionality in relation to our task.

This is how the conditional letter pattern will look like, let's call it index.html:

<html><head></head><body> <img src=”images/company_logo.gif” > , {username}!  ,       ,    {something}  ,   . <div> : <img src=”images/{something_photo}”></div> </body></html> 


The directory structure for storing templates might look like this:

 email_templates/ template1/ index.html images/ company_logo.gif photo1.png ... photoN.png template2/ ... 


Class:

 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; } } 


Example distribution:

 $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++; } 


Now it is necessary to keep track of the size of the letters being created, and not to exceed reasonable volumes, taking care of those who have Internet from the phone, is slow, with payment for traffic.

Of course, data should be taken from the database, this is just an example. There are also no error handlers in the class to reduce the number of lines, improve code readability and understanding.

Source: https://habr.com/ru/post/172269/


All Articles