/** * Send email via Mailgun SDK * * @param Email $email * @return \stdClass $result containing status code and message * @throws Exception */ public function send(Email $email) { $config = $email->profile(); $email->domain($config['mailgun_domain']); $emailHeaders = ['from', 'sender', 'replyTo', 'readReceipt', 'returnPath', 'to', 'cc', 'bcc', 'subject', '_headers']; //'_headers' will include all extra tags that may be related to mailgun fields with prefix 'o:' or custom data with prefix 'v:' foreach ($email->getHeaders($emailHeaders) as $header => $value) { if (isset($this->ParamMapping[$header]) && !empty($value)) { //empty params are not excepted by mailgun, throws error $key = $this->ParamMapping[$header]; $params[$key] = $value; continue; } if ($this->isDataCustom($header, $value)) { $params[$header] = $value; } } $params['html'] = $email->message(Email::MESSAGE_HTML); $params['text'] = $email->message(Email::MESSAGE_TEXT); $attachments = array(); foreach ($email->attachments() as $name => $file) { $attachments['attachment'][] = ['filePath' => '@' . $file['file'], 'remoteName' => $name]; } return $this->mailgun($config, $params, $attachments); }
/** * @return void */ public function testSendWithEmail() { $config = ['transport' => 'queue', 'charset' => 'utf-8', 'headerCharset' => 'utf-8']; $this->QueueTransport->config($config); $Email = new Email($config); $Email->from('*****@*****.**', 'CakePHP Test'); $Email->to('*****@*****.**', 'CakePHP'); $Email->cc(['*****@*****.**' => 'Mark Story', '*****@*****.**' => 'Juan Basso']); $Email->bcc('*****@*****.**'); $Email->subject('Testing Message'); $Email->attachments(['wow.txt' => ['data' => 'much wow!', 'mimetype' => 'text/plain', 'contentId' => 'important']]); $Email->template('test_template', 'test_layout'); $Email->subject("L'utilisateur n'a pas pu être enregistré"); $Email->replyTo('*****@*****.**'); $Email->readReceipt('*****@*****.**'); $Email->returnPath('*****@*****.**'); $Email->domain('cakephp.org'); $Email->theme('EuroTheme'); $Email->emailFormat('both'); $Email->set('var1', 1); $Email->set('var2', 2); $result = $this->QueueTransport->send($Email); $this->assertEquals('Email', $result['job_type']); $this->assertTrue(strlen($result['data']) < 10000); $output = json_decode($result['data'], true); $emailReconstructed = new Email($config); foreach ($output['settings'] as $method => $setting) { call_user_func_array([$emailReconstructed, $method], (array) $setting); } $this->assertEquals($emailReconstructed->from(), $Email->from()); $this->assertEquals($emailReconstructed->to(), $Email->to()); $this->assertEquals($emailReconstructed->cc(), $Email->cc()); $this->assertEquals($emailReconstructed->bcc(), $Email->bcc()); $this->assertEquals($emailReconstructed->subject(), $Email->subject()); $this->assertEquals($emailReconstructed->charset(), $Email->charset()); $this->assertEquals($emailReconstructed->headerCharset(), $Email->headerCharset()); $this->assertEquals($emailReconstructed->emailFormat(), $Email->emailFormat()); $this->assertEquals($emailReconstructed->replyTo(), $Email->replyTo()); $this->assertEquals($emailReconstructed->readReceipt(), $Email->readReceipt()); $this->assertEquals($emailReconstructed->returnPath(), $Email->returnPath()); $this->assertEquals($emailReconstructed->messageId(), $Email->messageId()); $this->assertEquals($emailReconstructed->domain(), $Email->domain()); $this->assertEquals($emailReconstructed->theme(), $Email->theme()); $this->assertEquals($emailReconstructed->profile(), $Email->profile()); $this->assertEquals($emailReconstructed->viewVars(), $Email->viewVars()); $this->assertEquals($emailReconstructed->template(), $Email->template()); //for now cannot be done 'data' is base64_encode on set but not decoded when get from $email //$this->assertEquals($emailReconstructed->attachments(),$Email->attachments()); //$this->assertEquals($Email, $output['settings']); }
public function sendNewMissionsToInterestedUsers($nbrDays = 7) { $created = Time::now()->subDays($nbrDays); $missions = $this->Missions->find('all', ['conditions' => ['Missions.created >=' => $created]])->toArray(); $newMissions = []; foreach ($missions as $mission) { $users = $this->Users->find('all')->toArray(); foreach ($users as $user) { $usersTypeMissions = $this->UsersTypeMissions->findByUserId($user['id'])->toArray(); foreach ($usersTypeMissions as $userTypeMissions) { // Check if the current mission has the same type has the current userType mission if ($userTypeMissions['type_mission_id'] == $mission['type_mission_id']) { // Add a new mission to the list of missions to send if (isset($newMissions[$user['email']])) { $newMission = []; $newMission['link'] = Router::url(['controller' => 'Missions', 'action' => 'view', $mission['id']]); $newMission['name'] = $mission['name']; //print("Mission " . $newMission['name'] . " added to user " . $user['email'] . "\n"); array_push($newMissions[$user['email']], $newMission); } else { //print("Creating mission list for user " . $user['email'] . "\n"); $newMissions[$user['email']] = []; $newMission = []; $newMission['link'] = Router::url(['controller' => 'Missions', 'action' => 'view', $mission['id']]); $newMission['name'] = $mission['name']; //print("Mission " . $newMission['name'] . " added to user " . $user['email'] . "\n"); array_push($newMissions[$user['email']], $newMission); } } } } } // For each user send their associated list of missions foreach ($newMissions as $userEmail => $missionsToSend) { $email = new Email(); $email->domain('maisonlogiciellibre.org'); $email->to($userEmail); $email->subject('New missions available at ML2'); $email->template('new_missions'); $email->emailFormat('both'); $email->viewVars(['missions' => $missionsToSend]); $email->send(); } }
/** * Send mail * * @param \Cake\Mailer\Email $email Email * @return array */ public function send(Email $email) { if (!empty($this->_config['queue'])) { $this->_config = $this->_config['queue'] + $this->_config; $email->config((array) $this->_config['queue'] + ['queue' => []]); unset($this->_config['queue']); } $settings = ['from' => [$email->from()], 'to' => [$email->to()], 'cc' => [$email->cc()], 'bcc' => [$email->bcc()], 'charset' => [$email->charset()], 'replyTo' => [$email->replyTo()], 'readReceipt' => [$email->readReceipt()], 'returnPath' => [$email->returnPath()], 'messageId' => [$email->messageId()], 'domain' => [$email->domain()], 'getHeaders' => [$email->getHeaders()], 'headerCharset' => [$email->headerCharset()], 'theme' => [$email->theme()], 'profile' => [$email->profile()], 'emailFormat' => [$email->emailFormat()], 'subject' => method_exists($email, 'getOriginalSubject') ? [$email->getOriginalSubject()] : [$email->subject()], 'transport' => [$this->_config['transport']], 'attachments' => [$email->attachments()], 'template' => $email->template(), 'viewVars' => [$email->viewVars()]]; foreach ($settings as $setting => $value) { if (array_key_exists(0, $value) && ($value[0] === null || $value[0] === [])) { unset($settings[$setting]); } } $QueuedJobs = $this->getQueuedJobsModel(); $result = $QueuedJobs->createJob('Email', ['settings' => $settings]); $result['headers'] = ''; $result['message'] = ''; return $result; }