Send email with attachments in ZF2

How to send email with text / plain, text / html and attached in zf2? I use this code to send email using smtp:

$files = $this->params()->fromFiles(); $smtp = new \Zend\Mail\Transport\Smtp(); $smtp->setAutoDisconnect(true); $optn = new \Zend\Mail\Transport\SmtpOptions(array( 'host' => 'mail.myserver.com', 'connection_class' => 'login', 'connection_config' => array( 'username' => ' user@myserver.com ', 'password' => 'mypassword', ), )); $smtp->setOptions($optn); $htmlPart = new \Zend\Mime\Part('<p>some html</p>'); $htmlPart->type = Mime::TYPE_HTML; $textPart = new \Zend\Mime\Part('some text'); $textPart->type = Mime::TYPE_TEXT; $i=0; $attaches = array(); foreach($files as $file){ if ($file['error']) continue; $attaches[$i] = new \Zend\Mime\Part(file_get_contents($file['tmp_name'])); $attaches[$i]->type = $file['type'].'; name="'.$file['name'].'"'; $attaches[$i]->encoding = 'base64'; $attaches[$i]->disposition = 'attachment'; $attaches[$i]->filename = $file['name']; $i++; } $parts = array(); if (count($attaches)>0) { $parts = array_merge(array($textPart,$htmlPart),$attaches); $type = Mime::MULTIPART_MIXED; } else{ $parts = array($textPart, $htmlPart); $type = Mime::MULTIPART_ALTERNATIVE ; } $body = new \Zend\Mime\Message(); $body->setParts($parts); $message = new \Zend\Mail\Message(); $message->setFrom(' user@myserver.com '); $message->addTo(' receiver@myserver.com '); $message->setSubject('subject'); $message->setEncoding("UTF-8"); $message->setBody($body); $message->getHeaders()->get('content-type')->setType($type); $smtp->send($message); 

If I attach files, it sends files and contents, but it shows plain and html text in the receiver of incoming messages:

 <p>some html</p> some text 

When I do not attach any files, it shows the html text separately:

 some html 

Any help?

+6
source share
4 answers

In ZF2 (2.2), there is no easy way to combine a multi-page / alternative body (html with alternative text for clients who cannot / do not want to use html) with attachments. If you add a header like "multipart / alternative" for the whole message, the attachment (link) will not be displayed on some mail clients.

The solution is to split the message into two parts: body (text and html) and attachment:

http://jw-dev.blogspot.com.es/2013/01/zf2-zend-mail-multipartalternative-and.html

example:

  $content = new MimeMessage(); $htmlPart = new MimePart("<html><body><p>Sorry,</p><p>I'm going to be late today!</p></body></html>"); $htmlPart->type = 'text/html'; $textPart = new MimePart("Sorry, I'm going to be late today!"); $textPart->type = 'text/plain'; $content->setParts(array($textPart, $htmlPart)); $contentPart = new MimePart($content->generateMessage()); $contentPart->type = 'multipart/alternative;' . PHP_EOL . ' boundary="' . $content->getMime()->boundary() . '"'; $attachment = new MimePart(fopen('/path/to/test.pdf', 'r')); $attachment->type = 'application/pdf'; $attachment->encoding = Mime::ENCODING_BASE64; $attachment->disposition = Mime::DISPOSITION_ATTACHMENT; $body = new MimeMessage(); $body->setParts(array($contentPart, $attachment)); $message = new Message(); $message->setEncoding('utf-8') ->addTo(' mywife@home.com ') ->addFrom(' myself@office.com ') ->setSubject('will be late') ->setBody($body); $transport = new SmtpTransport(); $options = new SmtpOptions($transportConfig), )); $transport->setOptions($options); $transport->send($message); 

For the above, you will need the following usage statements:

 use Zend\Mail\Message; use Zend\Mail\Transport\Smtp as SmtpTransport; use Zend\Mail\Transport\SmtpOptions; use Zend\Mime\Mime; use Zend\Mime\Part as MimePart; use Zend\Mime\Message as MimeMessage; 

ZF1 had a _buildBody() method in Zend_Mail_Transport_Abstract that did this automatically.

+12
source

I found this the best solution, so I write it.

 Namespace YourNamesapace; use Zend\Mail\Message as ZendMessage; use Zend\Mime\Part as MimePart; use Zend\Mime\Message as MimeMessage; use Zend\Mail\Transport\Sendmail; class Testmail { public static function sendMailWithAttachment($to, $subject, $htmlMsg, $dir, $fileName) { $fileFullPath = $dir . '/' . $fileName; // Render content from template $htmlContent = $htmlMsg; // Create HTML part $htmlPart = new MimePart($htmlContent); $htmlPart->type = "text/html"; // Create plain text part $stripTagsFilter = new \Zend\Filter\StripTags(); $textContent = str_ireplace(array("<br />", "<br>"), "\r\n", $htmlContent); $textContent = $stripTagsFilter->filter($textContent); $textPart = new MimePart($textContent); $textPart->type = "text/plain"; // Create separate alternative parts object $alternatives = new MimeMessage(); $alternatives->setParts(array($textPart, $htmlPart)); $alternativesPart = new MimePart($alternatives->generateMessage()); $alternativesPart->type = "multipart/alternative;\n boundary=\"".$alternatives->getMime()->boundary()."\""; $body = new MimeMessage(); $body->addPart($alternativesPart); $attachment = new MimePart( file_get_contents($fileFullPath) ); $attachment->type = \Zend\Mime\Mime::TYPE_OCTETSTREAM; $attachment->filename = basename($fileName); $attachment->disposition = \Zend\Mime\Mime::DISPOSITION_ATTACHMENT; $attachment->encoding = \Zend\Mime\Mime::ENCODING_BASE64; $body->addPart($attachment); // Create mail message $mailMessage = new ZendMessage(); $mailMessage->setFrom(' noreply@example.com ', 'from Name'); $mailMessage->setTo($to); $mailMessage->setSubject($subject); $mailMessage->setBody($body); $mailMessage->setEncoding("UTF-8"); $mailMessage->getHeaders()->get('content-type')->setType('multipart/mixed'); $transport = new Sendmail(); $transport->send($mailMessage); } } 

Link: http://resoftsol.com/sending-e-mail-with-alternative-parts-plus-attachments/

+1
source

Set the type from:

 $attaches[$i]->type = $file['type'].'; name="'.$file['name'].'"'; 

To:

 $attaches[$i]->type = \Zend\Mime\Mime::TYPE_OCTETSTREAM; 

You will also want to confirm that if you use the SMTP service that they allow the application through the protocol.

0
source

Email messages with attachments

 $mail = new Zend\Mail\Message(); // build message... $mail->createAttachment($someBinaryString); $mail->createAttachment($myImage, 'image/gif', Zend\Mime\Mime::DISPOSITION_INLINE, Zend\Mime\Mime::ENCODING_BASE64); 

If you need more control over the MIME part generated for this attachment, you can use the createAttachment () return value to change its attributes. The createAttachment () method returns a Zend \ Mime \ Part object:

 $mail = new Zend\Mail\Message(); $at = $mail->createAttachment($myImage); $at->type = 'image/gif'; $at->disposition = Zend\Mime\Mime::DISPOSITION_INLINE; $at->encoding = Zend\Mime\Mime::ENCODING_BASE64; $at->filename = 'test.gif'; $mail->send(); 

An alternative is to instantiate Zend \ Mime \ Part and add it using addAttachment ():

 $mail = new Zend\Mail\Message(); $at = new Zend\Mime\Part($myImage); $at->type = 'image/gif'; $at->disposition = Zend\Mime\Mime::DISPOSITION_INLINE; $at->encoding = Zend\Mime\Mime::ENCODING_BASE64; $at->filename = 'test.gif'; $mail->addAttachment($at); $mail->send(); 

Link1 Link2 Reference3

-3
source

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


All Articles