/** * Prepares the `from` email address. * * @return array */ protected function _prepareFromAddress() { $from = $this->_cakeEmail->returnPath(); if (empty($from)) { $from = $this->_cakeEmail->from(); } return $from; }
/** * Send emails * * @return void * @throws SocketException */ protected function _sendRcpt() { $from = $this->_cakeEmail->returnPath(); if (empty($from)) { $from = $this->_cakeEmail->from(); } $this->_smtpSend('MAIL FROM:<' . key($from) . '>'); $to = $this->_cakeEmail->to(); $cc = $this->_cakeEmail->cc(); $bcc = $this->_cakeEmail->bcc(); $emails = array_merge(array_keys($to), array_keys($cc), array_keys($bcc)); foreach ($emails as $email) { $this->_smtpSend('RCPT TO:<' . $email . '>'); } }
public static function accountCompleteMail($user) { App::uses('CakeEmail', 'Network/Email'); $email = new CakeEmail(); $email->config('default'); $email->domain('nativecamp.net'); $email->emailFormat('text'); $email->template('account_complete'); $email->viewVars(array('name' => $user['nickname'])); $email->to($user['email']); $email->from('*****@*****.**', 'NativeCamp運営事務局'); $email->replyTo('*****@*****.**'); $email->returnPath('*****@*****.**', 'NativeCamp運営事務局'); # $email->subject(__('Account Activation').' - '.Configure::read('my.site_name')); $email->subject("ネイティブキャンプ:事前登録完了メール"); $email->send(); }
/** * Send * * A bit of a misnomer, because this actually just adds it to a CakeResque * queue. The actual sending of the email will be performed later by a worker. * * @params CakeEmail $email * @return array */ public function send(CakeEmail $email) { // Take a copy of the existing configuration. $config = array('headers' => $email->getHeaders(), 'from' => $email->from(), 'sender' => $email->sender(), 'replyTo' => $email->replyTo(), 'readReceipt' => $email->readReceipt(), 'returnPath' => $email->returnPath(), 'to' => $email->to(), 'cc' => $email->cc(), 'bcc' => $email->bcc(), 'subject' => $email->subject(), 'viewRender' => $email->viewRender(), 'viewVars' => $email->viewVars(), 'emailFormat' => $email->emailFormat(), 'messageId' => $email->messageId(), 'attachments' => $email->attachments()); // unset($config['config']['transport']); $template = $email->template(); $config['template'] = $template['template']; $config['layout'] = $template['layout']; // Clean it up to avoid errors. $config = array_filter($config, function ($v) { return (bool) $v; }); debug($config); // Include a message, if they sent one via plain text. $message = $email->message(CakeEmail::MESSAGE_HTML) ? null : $email->message(CakeEmail::MESSAGE_TEXT); // Drop it in a queue. Resque::enqueue('email', 'ResqueEmail.EmailShell', array('send', $config, $message)); return array('headers' => $email->getHeaders(), 'message' => $email->message()); }
/** * Send an email using the specified content, template and layout * * @param string|array $content Either an array of text lines, or a string with contents * If you are rendering a template this variable will be sent to the templates as `$content` * @param string $template Template to use when sending email * @param string $layout Layout to use to enclose email body * @return boolean Success */ public function send($content = null, $template = null, $layout = null) { $lib = new CakeEmail(); $lib->charset = $this->charset; $lib->headerCharset = $this->charset; $lib->from($this->_formatAddresses((array)$this->from)); if (!empty($this->to)) { $lib->to($this->_formatAddresses((array)$this->to)); } if (!empty($this->cc)) { $lib->cc($this->_formatAddresses((array)$this->cc)); } if (!empty($this->bcc)) { $lib->bcc($this->_formatAddresses((array)$this->bcc)); } if (!empty($this->replyTo)) { $lib->replyTo($this->_formatAddresses((array)$this->replyTo)); } if (!empty($this->return)) { $lib->returnPath($this->_formatAddresses((array)$this->return)); } if (!empty($this->readReceipt)) { $lib->readReceipt($this->_formatAddresses((array)$this->readReceipt)); } $lib->subject($this->subject)->messageID($this->messageId); $lib->helpers($this->_controller->helpers); $headers = array('X-Mailer' => $this->xMailer); foreach ($this->headers as $key => $value) { $headers['X-' . $key] = $value; } if ($this->date) { $headers['Date'] = $this->date; } $lib->setHeaders($headers); if ($template) { $this->template = $template; } if ($layout) { $this->layout = $layout; } $lib->template($this->template, $this->layout)->viewVars($this->_controller->viewVars)->emailFormat($this->sendAs); if (!empty($this->attachments)) { $lib->attachments($this->_formatAttachFiles()); } $lib->transport(ucfirst($this->delivery)); if ($this->delivery === 'mail') { $lib->config(array('eol' => $this->lineFeed, 'additionalParameters' => $this->additionalParams)); } elseif ($this->delivery === 'smtp') { $lib->config($this->smtpOptions); } else { $lib->config(array()); } $sent = $lib->send($content); $this->htmlMessage = $lib->message(CakeEmail::MESSAGE_HTML); if (empty($this->htmlMessage)) { $this->htmlMessage = null; } $this->textMessage = $lib->message(CakeEmail::MESSAGE_TEXT); if (empty($this->textMessage)) { $this->textMessage = null; } $this->_header = array(); $this->_message = array(); return $sent; }
/** * testRcptWithReturnPath method * * @return void */ public function testRcptWithReturnPath() { $email = new CakeEmail(); $email->from('*****@*****.**', 'CakePHP Test'); $email->to('*****@*****.**', 'CakePHP'); $email->returnPath('*****@*****.**', 'CakePHP Return'); $this->socket->expects($this->at(0))->method('write')->with("MAIL FROM:<*****@*****.**>\r\n"); $this->socket->expects($this->at(1))->method('read')->will($this->returnValue(false)); $this->socket->expects($this->at(2))->method('read')->will($this->returnValue("250 OK\r\n")); $this->socket->expects($this->at(3))->method('write')->with("RCPT TO:<*****@*****.**>\r\n"); $this->socket->expects($this->at(4))->method('read')->will($this->returnValue(false)); $this->socket->expects($this->at(5))->method('read')->will($this->returnValue("250 OK\r\n")); $this->SmtpTransport->setCakeEmail($email); $this->SmtpTransport->sendRcpt(); }
/** * メールを送信する * * @param string $to 送信先アドレス * @param string $title タイトル * @param mixed $body 本文 * @param array $options オプション * @return bool 送信結果 */ public function sendMail($to, $title = '', $body = '', $options = array()) { $options = array_merge(array('agentTemplate' => true, 'template' => 'default'), $options); if (!empty($this->siteConfigs['smtp_host'])) { $transport = 'Smtp'; $host = $this->siteConfigs['smtp_host']; $port = $this->siteConfigs['smtp_port'] ? $this->siteConfigs['smtp_port'] : 25; $username = $this->siteConfigs['smtp_user'] ? $this->siteConfigs['smtp_user'] : null; $password = $this->siteConfigs['smtp_password'] ? $this->siteConfigs['smtp_password'] : null; $tls = $this->siteConfigs['smtp_tls'] && $this->siteConfigs['smtp_tls'] == 1; } else { $transport = 'Mail'; $host = 'localhost'; $port = 25; $username = null; $password = null; $tls = null; } $config = array('transport' => $transport, 'host' => $host, 'port' => $port, 'username' => $username, 'password' => $password, 'tls' => $tls); $cakeEmail = new CakeEmail($config); // charset if (!empty($this->siteConfigs['mail_encode'])) { $encode = $this->siteConfigs['mail_encode']; } else { $encode = 'ISO-2022-JP'; } // ISO-2022-JPの場合半角カナが文字化けしてしまうので全角に変換する if ($encode == 'ISO-2022-JP') { $title = mb_convert_kana($title, 'KV', "UTF-8"); if (is_string($body)) { $body = mb_convert_kana($body, 'KV', "UTF-8"); } elseif (isset($body['message']) && is_array($body['message'])) { foreach ($body['message'] as $key => $val) { if (is_string($val)) { $body['message'][$key] = mb_convert_kana($val, 'KV', "UTF-8"); } } } } //CakeEmailの内部処理のencodeを統一したいので先に値を渡しておく $cakeEmail->headerCharset($encode); $cakeEmail->charset($encode); //$format if (!empty($options['format'])) { $cakeEmail->emailFormat($options['format']); } else { $cakeEmail->emailFormat('text'); } //bcc 'mail@example.com,mail2@example.com' if (!empty($options['bcc'])) { // 文字列の場合 $bcc = array(); if (is_string($options['bcc'])) { if (strpos($options['bcc'], ',') !== false) { $bcc = explode(',', $options['bcc']); } else { $bcc[] = $options['bcc']; } // 配列の場合 } elseif (is_array($options['bcc'])) { $bcc = $options['bcc']; } foreach ($bcc as $val) { if (Validation::email(trim($val))) { $cakeEmail->addBcc($val); } } unset($bcc); } //cc 'mail@example.com,mail2@example.com' if (!empty($options['cc'])) { // 文字列の場合 $cc = array(); if (is_string($options['cc'])) { if (strpos($options['cc'], ',') !== false) { $cc = explode(',', $options['cc']); } else { $cc[] = $options['cc']; } // 配列の場合 } elseif (is_array($options['cc'])) { $cc = $options['cc']; } foreach ($cc as $val) { if (Validation::email(trim($val))) { $cakeEmail->addCc($val); } } unset($cc); } // to 送信先アドレス (最初の1人がTOで残りがBCC) if (strpos($to, ',') !== false) { $_to = explode(',', $to); $i = 0; if (count($_to) >= 1) { foreach ($_to as $val) { if ($i == 0) { $cakeEmail->addTo($val); $toAddress = $val; } else { $cakeEmail->addBcc($val); } ++$i; } } } else { $cakeEmail->addTo($to); } // 件名 $cakeEmail->subject($title); //From $fromName = $from = ''; if (!empty($options['from'])) { $from = $options['from']; } else { if (!empty($this->siteConfigs['email'])) { $from = $this->siteConfigs['email']; if (strpos($from, ',') !== false) { $from = explode(',', $from); } } else { $from = $toAddress; } } if (!empty($options['fromName'])) { $fromName = $options['fromName']; } else { if (!empty($this->siteConfigs['formal_name'])) { $fromName = $this->siteConfigs['formal_name']; } else { $formalName = Configure::read('BcApp.title'); } } $cakeEmail->from($from, $fromName); //Reply-To if (!empty($options['replyTo'])) { $replyTo = $options['replyTo']; } else { $replyTo = $from; } $cakeEmail->replyTo($replyTo); //Return-Path if (!empty($options['returnPath'])) { $returnPath = $options['returnPath']; } else { $returnPath = $from; } $cakeEmail->returnPath($returnPath); //$sender if (!empty($options['sender'])) { $cakeEmail->sender($options['sender']); } //$theme if ($this->theme) { $cakeEmail->theme($this->theme); } if (!empty($options['theme'])) { $cakeEmail->theme($options['theme']); } //viewRender (利用するviewクラスを設定する) $cakeEmail->viewRender('BcApp'); //template if (!empty($options['template'])) { $layoutPath = $subDir = $plugin = ''; if ($options['agentTemplate'] && Configure::read('BcRequest.agent')) { $layoutPath = Configure::read('BcRequest.agentPrefix'); $subDir = Configure::read('BcRequest.agentPrefix'); } list($plugin, $template) = pluginSplit($options['template']); if ($subDir) { $template = "{$subDir}/{$template}"; } if (!empty($plugin)) { $template = "{$plugin}.{$template}"; } if (!empty($options['layout'])) { $cakeEmail->template($template, $options['layout']); } else { $cakeEmail->template($template); } $content = ''; if (is_array($body)) { $cakeEmail->viewVars($body); } else { $cakeEmail->viewVars(array('body' => $body)); } } else { $content = $body; } // $attachments tmp file path $attachments = array(); if (!empty($options['attachments'])) { if (!is_array($options['attachments'])) { $attachments = array($options['attachments']); } else { $attachments = $options['attachments']; } } $cakeEmail->attachments($attachments); try { $cakeEmail->send($content); return true; } catch (Exception $e) { $this->log($e->getMessage()); return false; } }
public static function sendTemplateMail($mail_id, $to_email, $user) { App::uses('CakeEmail', 'Network/Email'); App::uses('MailTemplate', 'Model'); // 宛先がない if (empty($to_email)) { return; } // テンプレート取得 $mailbase = ClassRegistry::init('MailTemplate')->findByIdAndStatus($mail_id, 1); if (empty($mailbase['MailTemplate'])) { return; } $mail_template = $mailbase['MailTemplate']; // FROMがない if (empty($mail_template['from_email'])) { return; } $search = array('/--id--/', '/--nickname--/', '/--email--/'); $replace = array($user['id'], $user['nickname'], $user['email']); $mail_subject = str_replace($search, $replace, $mail_template['subject']); $mail_body = str_replace($search, $replace, $mail_template['body']); switch ($mail_id) { case Configure::read('site_in_mail.student_changed_email'): $mail_body = str_replace(array('/--new_email--/', '/--url--/'), array($user['new_email'], $user['active_url']), $mail_body); break; case Configure::read('site_in_mail.student_changed_password'): $mail_body = str_replace(array('/--url--/'), array($user['active_url']), $mail_body); break; case Configure::read('site_in_mail.student_reserved'): case Configure::read('site_in_mail.student_cancel_reservation'): $mail_body = str_replace(array('/--teacherName--/', '/--appointmentDate--/', '/--startTime--/'), array($user['teacherName'], $user['appointmentDate'], $user['startTime']), $mail_body); break; case Configure::read('site_in_mail.admin_student_reserved'): case Configure::read('site_in_mail.admin_student_cancel'): $mail_body = str_replace(array('/--teacherName--/', '/--appointmentDate--/', '/--startTime--/', '/--endTime--/'), array($user['teacherName'], $user['appointmentDate'], $user['startTime'], $user['endTime']), $mail_body); break; case Configure::read('site_in_mail.teacher_cancel_reservation'): $mail_body = str_replace(array('/--teacherName--/', '/--appointmentDate--/', '/--startTime--/', '/--endTime--/'), array($user['teacherName'], $user['appointmentDate'], $user['startTime'], $user['endTime']), $mail_body); break; case Configure::read('site_in_mail.student_reservedlesson_before_30minutes'): $mail_body = str_replace(array('/--teacherName--/', '/--appointmentDate--/', '/--startTime--/'), array($user['teacherName'], $user['appointmentDate'], $user['startTime']), $mail_body); break; case Configure::read('site_in_mail.admin_reservedlesson_before_30minutes'): $mail_body = str_replace(array('/--email--/', '/--teacherName--/', '/--appointmentDate--/', '/--startTime--/', '/--endTime--/'), array($user['email'], $user['teacherName'], $user['appointmentDate'], $user['startTime'], $user['endTime']), $mail_body); break; case Configure::read('site_in_mail.student_inquiry'): break; case Configure::read('site_in_mail.student_inquiry_reply'): $mail_body = str_replace(array('/--replymessage--/'), array($user['replymessage']), $mail_body); break; case Configure::read('site_in_mail.admin_student_inquiry'): $mail_body = str_replace(array('/--inquiry_message--/'), array($user['inquiry_message']), $mail_body); break; case Configure::read('site_in_mail.student_regist_email_confirm'): $mail_body = str_replace(array('/--url--/'), array($user['active_url']), $mail_body); break; case Configure::read('site_in_mail.purchase_book_confirm_user'): $mail_subject = str_replace('/--books--/', $user['books'], $mail_subject); $mail_body = str_replace(array('/--tax--/', '/--nickname--/', '/--name--/', '/--address--/', '/--contact--/', '/--email--/', '/--products--/', '/--price--/', '/--quantity--/', '/--subtotal--/', '/--total--/', '/--account--/'), array($user['tax'], $user['nickname'], $user['name'], $user['address'], $user['contact'], $user['email'], $user['products'], $user['price'], $user['qty'], $user['subtotal'], $user['total'], $user['account']), $mail_body); break; case Configure::read('site_in_mail.purchase_book_confirm_admin'): $mail_body = str_replace(array('/--tax--/', '/--name--/', '/--address--/', '/--contact--/', '/--email--/', '/--products--/', '/--price--/', '/--quantity--/', '/--subtotal--/', '/--total--/'), array($user['tax'], $user['name'], $user['address'], $user['contact'], $user['email'], $user['products'], $user['price'], $user['qty'], $user['subtotal'], $user['total']), $mail_body); break; } $email = new CakeEmail(); $email->config('default'); $email->from($mail_template['from_email'], 'NativeCamp運営事務局'); $email->to($to_email); $email->replyTo('*****@*****.**'); $email->returnPath('*****@*****.**', 'NativeCamp運営事務局'); $email->subject($mail_subject); $email->send($mail_body); }
/** * Prepares the `from` email address. * * @param CakeEmail $email CakeEmail * @return array */ protected function _prepareFromAddress(CakeEmail $email) { $from = $email->returnPath(); if (empty($from)) { $from = $email->from(); } return $from; }
public function send($data) { $this->create(null); $this->set($data); $Invoice = ClassRegistry::init('LilInvoices.Invoice'); $Vat = ClassRegistry::init('LilInvoices.Vat'); $vats = $Vat->findList(); if ($this->validates() && ($invoices = $Invoice->find('all', array('conditions' => array('Invoice.id' => $data['InvoiceEmail']['invoices']), 'contain' => array('InvoicesItem', 'InvoicesCounter', 'Client' => 'PrimaryAddress', 'InvoicesAttachment', 'InvoicesTax' => 'Vat'))))) { // generate PDF App::uses('LilReport', 'Lil.Lib'); App::uses('Sanitize', 'Utility'); $report = new LilReport(); $report->helpers(array('Lil.Lil', 'Lil.LilDate', 'Lil.LilFloat', 'Html')); $report->template('', 'LilInvoices.lil_invoices_pdf'); $report->set(array('vats' => $vats)); $page = 0; foreach ($invoices as $i) { if ($page > 0) { $report->addPage(); } $report->set(array('data' => $i)); $report->render(null, array('template' => $i['InvoicesCounter']['layout'])); $page++; } $filename = 'invoices_' . strftime('%Y%m%d%H%M%S'); $report->output(TMP . $filename, 'F'); // init and send email App::uses('CakeEmail', 'Network/Email'); $email = new CakeEmail(); $email->template('LilInvoices.send_invoice'); $email->from(Configure::read('Lil.from.email'), Configure::read('Lil.from.name')); $email->returnPath(Configure::read('Lil.from.email')); $email->subject($data['InvoiceEmail']['subject']); $email->to($data['InvoiceEmail']['to']); if (!empty($data['InvoiceEmail']['cc']) && $data['InvoiceEmail']['cc'] != $data['InvoiceEmail']['to']) { $email->cc($data['InvoiceEmail']['cc']); } Cache::write('LilInvoices.emailCacheSubject', $data['InvoiceEmail']['subject'], 'Lil'); Cache::write('LilInvoices.emailCacheBody', $data['InvoiceEmail']['body'], 'Lil'); Cache::write('LilInvoices.emailCacheCC', $data['InvoiceEmail']['cc'], 'Lil'); if (isset($data['InvoiceEmail']['cc_me'])) { Cache::write('LilInvoices.emailCacheCCMe', $data['InvoiceEmail']['cc_me'], 'Lil'); } Cache::write('LilInvoices.emailCacheTo', $data['InvoiceEmail']['to'], 'Lil'); $email->viewVars(array('body' => $data['InvoiceEmail']['body'], 'invoices' => $invoices)); $ats = array($filename . '.pdf' => array('file' => TMP . $filename . '.pdf', 'mimetype' => 'application/pdf')); App::uses('Sanitize', 'Utility'); foreach ($invoices as $i) { foreach ($i['InvoicesAttachment'] as $atch) { $atch_name = 'at_' . $i['Invoice']['no'] . '_' . $atch['original']; $atch_file = APP . 'uploads' . DS . 'Invoice' . DS . $atch['filename']; if (file_exists($atch_file)) { $ats[Sanitize::paranoid($atch_name, array('-', '_', '.'))] = array('file' => $atch_file, 'mimetype' => $atch['mimetype']); } } } $email->attachments($ats); $result = $email->send(); unlink(TMP . $filename . '.pdf'); return $result; } return false; }