use Zend\Mime; use Zend\Mail\Message; use Zend\Mail\Transport\Smtp as SmtpTransport; use Zend\Mail\Transport\SmtpOptions;
try { // 純文字內容 $text = new Mime\Part("信件內容"); $text->type = Mime\Mime::TYPE_TEXT; $text->charset = 'utf-8'; // 附件檔案 $fileContent = fopen($zipFile, 'r'); $attachment = new Mime\Part($fileContent); $attachment->type = 'application/zip'; $attachment->filename = $cur['optid'] . '._zip'; $attachment->encoding = Mime\Mime::ENCODING_BASE64; $attachment->disposition = Mime\Mime::DISPOSITION_ATTACHMENT; // 將純文字內容和附件檔案加入信件內容 $mimeMessage = new Mime\Message(); $mimeMessage->setParts(array($text, $attachment)); // 建立信件 $message = new Message(); $message->setEncoding('utf-8') ->addTo('zz@example.com') ->addFrom('yy@example.com') ->setSubject('測試') ->setBody($mimeMessage); // Gmail 發信設定 $transport = new SmtpTransport(); $options = new SmtpOptions(array( 'host' => 'smtp.gmail.com', 'connection_class' => 'login', 'connection_config' => array( 'ssl' => 'tls', 'username' => 'Gmail帳號', 'password' => '密碼' ), 'port' => 587 )); $transport->setOptions($options); $transport->send($message); } catch (\Exception $e) { var_dump($e->getMessage()); }
參考:
send email with attached files in ZF2
ZF2 Zend Mail, multipart/alternative and multipart/mixed Combined
Send HTML Mail Using ZF2
E-mail Attachments — Zend Framework 2 2.2.8 documentation
沒有留言:
張貼留言