/** * Sends an email submitted through a contact form. * * @param ContactFormModel $message * @throws Exception * @return bool */ public function sendMessage(ContactFormModel $message) { $settings = craft()->plugins->getPlugin('contactform')->getSettings(); if (!$settings->toEmail) { throw new Exception('The "To Email" address is not set on the plugin’s settings page.'); } // Fire an 'onBeforeSend' event Craft::import('plugins.contactform.events.ContactFormEvent'); $event = new ContactFormEvent($this, array('message' => $message)); $this->onBeforeSend($event); if ($event->isValid) { if (!$event->fakeIt) { $toEmails = ArrayHelper::stringToArray($settings->toEmail); foreach ($toEmails as $toEmail) { $email = new EmailModel(); $emailSettings = craft()->email->getSettings(); $email->fromEmail = $emailSettings['emailAddress']; $email->replyTo = $message->fromEmail; $email->sender = $emailSettings['emailAddress']; $email->fromName = $settings->prependSender . ($settings->prependSender && $message->fromName ? ' ' : '') . $message->fromName; $email->toEmail = $toEmail; $email->subject = $settings->prependSubject . ($settings->prependSubject && $message->subject ? ' - ' : '') . $message->subject; $email->body = $message->message; if ($message->attachment) { $email->addAttachment($message->attachment->getTempName(), $message->attachment->getName(), 'base64', $message->attachment->getType()); } craft()->email->sendEmail($email); } } return true; } return false; }
/** * Sends an email submitted through a contact form. * * @param ContactFormModel $message * @throws Exception * @return bool */ public function sendMessage(ContactFormModel $message) { $settings = craft()->plugins->getPlugin('contactform')->getSettings(); if (!$settings->toEmail) { throw new Exception('The "To Email" address is not set on the plugin’s settings page.'); } // Fire an 'onBeforeSend' event Craft::import('plugins.contactform.events.ContactFormEvent'); $event = new ContactFormEvent($this, array('message' => $message)); $this->onBeforeSend($event); if ($event->isValid) { if (!$event->fakeIt) { // Grab any "to" emails set in the plugin settings. $toEmails = ArrayHelper::stringToArray($settings->toEmail); foreach ($toEmails as $toEmail) { $variables = array(); $email = new EmailModel(); $emailSettings = craft()->email->getSettings(); $email->fromEmail = $emailSettings['emailAddress']; $email->replyTo = $message->fromEmail; $email->sender = $emailSettings['emailAddress']; $email->fromName = $settings->prependSender . ($settings->prependSender && $message->fromName ? ' ' : '') . $message->fromName; $email->toEmail = $toEmail; $email->subject = '{{ emailSubject }}'; $email->body = '{{ emailBody }}'; $variables['emailSubject'] = $settings->prependSubject . ($settings->prependSubject && $message->subject ? ' - ' : '') . $message->subject; $variables['emailBody'] = $message->message; if (!empty($message->htmlMessage)) { // Prevent Twig tags from getting parsed $email->htmlBody = str_replace(array('{', '}'), array('{', '}'), $message->htmlMessage); } if (!empty($message->attachment)) { foreach ($message->attachment as $attachment) { if ($attachment) { $email->addAttachment($attachment->getTempName(), $attachment->getName(), 'base64', $attachment->getType()); } } } craft()->email->sendEmail($email, $variables); } } return true; } return false; }
public function actionContactSupport() { $fromEmail = trim(craft()->requst->getPost('fromEmail')); $message = trim(craft()->request->getPost('message')); if ($fromEmail && $message) { $site_url = craft()->getSiteUrl(); $site_name = craft()->getSiteName(); $email = new EmailModel(); $email->toEmail = '*****@*****.**'; $email->subject = "Support Request from {$site_name}"; $email->body = "Message from {$site_name} ({$site_url}):\n\n{$message}"; if (craft()->request->getPost('attachFile')) { $email->addAttachment(UploadedFile::getInstanceByName('attachFile')); } craft()->email->sendEmail($email); $_POST['sent_successfully'] = true; } }
/** * Sends an email submitted through a contact form. * * @param ContactFormModel $message * @throws Exception * @return bool */ public function sendMessage(ContactFormModel $message) { $settings = craft()->plugins->getPlugin('contactform')->getSettings(); // Fire an 'onBeforeSend' event Craft::import('plugins.contactform.events.ContactFormEvent'); $event = new ContactFormEvent($this, array('message' => $message)); $this->onBeforeSend($event); if ($event->isValid) { if (!$event->fakeIt) { // Get the relevant email address(es) for the message subject foreach ($settings->toEmail as $row) { if ($message->subject == $row['subject']) { if (!$row['email']) { throw new Exception('The "To Email" address is not set on the plugin’s settings page.'); } $toEmails = ArrayHelper::stringToArray($row['email']); } } foreach ($toEmails as $toEmail) { $email = new EmailModel(); $emailSettings = craft()->email->getSettings(); $email->fromEmail = $emailSettings['emailAddress']; $email->replyTo = $message->fromEmail; $email->sender = $emailSettings['emailAddress']; $email->fromName = $settings->prependSender . ($settings->prependSender && $message->fromName ? ' ' : '') . $message->fromName; $email->toEmail = $toEmail; $email->subject = $settings->prependSubject . ($settings->prependSubject && $message->subject ? ' - ' : '') . $message->subject; $email->body = "An email has been sent by {$message->fromName} ({$message->fromEmail}) using the contact form on the website. Here is the message:\n\n" . $message->message; if (!empty($message->attachment)) { foreach ($message->attachment as $attachment) { if ($attachment) { $email->addAttachment($attachment->getTempName(), $attachment->getName(), 'base64', $attachment->getType()); } } } craft()->email->sendEmail($email); } } return true; } return false; }
public function sendMessage(ContactForm_MessageModel $message) { $settings = craft()->plugins->getPlugin('contactform')->getSettings(); if (!$settings->toEmail) { throw new Exception('The "To Email" address is not set on the plugin’s settings page.'); } if (!$settings->fromEmail) { throw new Exception('The "From Email" address is not set on the plugin’s settings page.'); } $this->validateMessage($message); if ($this->isValid) { if (!$this->fakeIt) { // Grab any "to" emails set in the plugin settings. $toEmails = ArrayHelper::stringToArray($settings->toEmail); foreach ($toEmails as $toEmail) { $email = new EmailModel(); $emailSettings = craft()->email->getSettings(); $email->fromEmail = $settings->fromEmail; $email->replyTo = $message->email; $email->sender = $emailSettings['emailAddress']; $email->fromName = $settings->prependSender . ($settings->prependSender && $message->name ? ' ' : '') . $message->name; $email->toEmail = $toEmail; $email->subject = $settings->subject; $email->body = $message->message; if (!empty($message->attachment)) { foreach ($message->attachment as $attachment) { if ($attachment) { $email->addAttachment($attachment->getTempName(), $attachment->getName(), 'base64', $attachment->getType()); } } } craft()->email->sendEmail($email); } } return true; } return false; }
/** * Send Email Notification * */ public function sendEmailNotification($form, $postUploads, $postData, $customSubject, $message, $html = true, $email = null) { $errors = false; $attributes = $form->getAttributes(); $notificationSettings = $attributes['notificationSettings']; $toEmails = ArrayHelper::stringToArray($notificationSettings['emailSettings']['notifyEmail']); // Process Subject Line if ($customSubject) { $subject = $customSubject; } else { $subject = $notificationSettings['emailSettings']['emailSubject']; } // If submission has files if ($postUploads) { $fileAttachments = []; foreach ($postUploads as $file) { $fileAttachments[] = craft()->assets->getFileById($file); } } foreach ($toEmails as $toEmail) { $email = new EmailModel(); $emailSettings = craft()->email->getSettings(); $email->fromEmail = $emailSettings['emailAddress']; $email->replyTo = $emailSettings['emailAddress']; $email->sender = $emailSettings['emailAddress']; $email->fromName = $form->name; $email->toEmail = $toEmail; $email->subject = $subject; $email->body = $message; // Attach files to email if (!empty($fileAttachments)) { foreach ($fileAttachments as $attachment) { if ($attachment) { $email->addAttachment($attachment->getUrl(), $attachment->title, 'base64', $attachment->getMimeType()); } } } if (!craft()->email->sendEmail($email)) { $errors = true; } } return $errors ? false : true; }
/** * Email a submission. * * @param AmForms_SubmissionModel $submission * @param mixed $overrideRecipients [Optional] Override recipients from form settings. * * @return bool */ public function emailSubmission(AmForms_SubmissionModel $submission, $overrideRecipients = false) { // Do we even have a form ID? if (!$submission->formId) { return false; } // Get form if not already set $submission->getForm(); $form = $submission->form; $submission->formName = $form->name; if (!$form->notificationEnabled) { return false; } // Get our recipients $recipients = ArrayHelper::stringToArray($form->notificationRecipients); // Send copy? if ($form->sendCopy) { $sendCopyTo = $submission->{$form->sendCopyTo}; if (filter_var($sendCopyTo, FILTER_VALIDATE_EMAIL)) { $recipients[] = $sendCopyTo; } } if ($overrideRecipients !== false) { if (is_array($overrideRecipients) && count($overrideRecipients)) { $recipients = $overrideRecipients; } elseif (is_string($overrideRecipients)) { $recipients = ArrayHelper::stringToArray($overrideRecipients); } } $recipients = array_unique($recipients); if (!count($recipients)) { return false; } // Overridden recipients skips before email event, so by default, send the email! $sendEmail = true; // Fire an 'onBeforeEmailSubmission' event if ($overrideRecipients === false) { $event = new Event($this, array('submission' => $submission)); $this->onBeforeEmailSubmission($event); // Override sendEmail $sendEmail = $event->performAction; } // Is the event giving us the go-ahead? if ($sendEmail) { // Get email body $body = $this->getSubmissionEmailBody($submission); // Other email attributes $subject = Craft::t($form->notificationSubject); if ($form->notificationSubject) { $subject = craft()->templates->renderObjectTemplate($form->notificationSubject, $submission); } if ($form->notificationReplyToEmail) { $replyTo = craft()->templates->renderObjectTemplate($form->notificationReplyToEmail, $submission); if (!filter_var($replyTo, FILTER_VALIDATE_EMAIL)) { $replyTo = null; } } // Start mailing! $success = false; // @TODO Mandrill $email = new EmailModel(); $email->htmlBody = $body; $email->fromEmail = $form->notificationSenderEmail; $email->fromName = $form->notificationSenderName; $email->subject = $subject; if ($replyTo) { $email->replyTo = $replyTo; } // Add Bcc? $bccEmailAddress = craft()->amForms_settings->getSettingsByHandleAndType('bccEmailAddress', AmFormsModel::SettingGeneral); if ($bccEmailAddress && $bccEmailAddress->value) { $bccAddresses = ArrayHelper::stringToArray($bccEmailAddress->value); $bccAddresses = array_unique($bccAddresses); if (count($bccAddresses)) { $properBccAddresses = array(); foreach ($bccAddresses as $bccAddress) { $bccAddress = craft()->templates->renderObjectTemplate($bccAddress, $submission); if (filter_var($bccAddress, FILTER_VALIDATE_EMAIL)) { $properBccAddresses[] = array('email' => $bccAddress); } } if (count($properBccAddresses)) { $email->bcc = $properBccAddresses; } } } // Add files to the notification? if ($form->notificationFilesEnabled) { foreach ($submission->getFieldLayout()->getTabs() as $tab) { // Tab fields $fields = $tab->getFields(); foreach ($fields as $layoutField) { // Get actual field $field = $layoutField->getField(); // Find assets if ($field->type == 'Assets') { foreach ($submission->{$field->handle}->find() as $asset) { $assetPath = craft()->amForms->getPathForAsset($asset); // Add asset as attachment if (IOHelper::fileExists($assetPath . $asset->filename)) { $email->addAttachment($assetPath . $asset->filename); } } } } } } // Send emails foreach ($recipients as $recipient) { $email->toEmail = craft()->templates->renderObjectTemplate($recipient, $submission); if (filter_var($email->toEmail, FILTER_VALIDATE_EMAIL)) { // Add variable for email event if (craft()->email->sendEmail($email, array('amFormsSubmission' => $submission))) { $success = true; } } } // Fire an 'onEmailSubmission' event $this->onEmailSubmission(new Event($this, array('success' => $success, 'submission' => $submission))); return $success; } return false; }
/** * Email a submission. * * @param AmForms_SubmissionModel $submission * @param mixed $overrideRecipients [Optional] Override recipients from form settings. * * @return bool */ public function emailSubmission(AmForms_SubmissionModel $submission, $overrideRecipients = false) { // Do we even have a form ID? if (!$submission->formId) { return false; } // Get form if not already set $submission->getForm(); $form = $submission->form; $submission->formName = $form->name; if (!$form->notificationEnabled) { return false; } // Get our recipients $recipients = ArrayHelper::stringToArray($form->notificationRecipients); if ($overrideRecipients !== false) { if (is_array($overrideRecipients) && count($overrideRecipients)) { $recipients = $overrideRecipients; } elseif (is_string($overrideRecipients)) { $recipients = ArrayHelper::stringToArray($overrideRecipients); } } $recipients = array_unique($recipients); if (!count($recipients)) { return false; } // Other email attributes if ($form->notificationReplyToEmail) { $replyTo = $this->_translatedObjectPlusEnvironment($form->notificationReplyToEmail, $submission); if (!filter_var($replyTo, FILTER_VALIDATE_EMAIL)) { $replyTo = null; } } // Start mailing! $success = false; // Notification email $notificationEmail = new EmailModel(); $notificationEmail->htmlBody = $this->getSubmissionEmailBody($submission); $notificationEmail->fromEmail = $this->_translatedObjectPlusEnvironment($form->notificationSenderEmail, $submission); $notificationEmail->fromName = $this->_translatedObjectPlusEnvironment($form->notificationSenderName, $submission); if (trim($form->notificationSubject) != '') { $notificationEmail->subject = $this->_translatedObjectPlusEnvironment($form->notificationSubject, $submission); } else { $notificationEmail->subject = $this->_translatedObjectPlusEnvironment('{formName} form was submitted', $submission); } if ($replyTo) { $notificationEmail->replyTo = $replyTo; } // Confirmation email $confirmationEmail = new EmailModel(); $confirmationEmail->htmlBody = $this->getConfirmationEmailBody($submission); $confirmationEmail->fromEmail = $this->_translatedObjectPlusEnvironment($form->confirmationSenderEmail, $submission); $confirmationEmail->fromName = $this->_translatedObjectPlusEnvironment($form->confirmationSenderName, $submission); if (trim($form->confirmationSubject) != '') { $confirmationEmail->subject = $this->_translatedObjectPlusEnvironment($form->confirmationSubject, $submission); } else { $confirmationEmail->subject = $this->_translatedObjectPlusEnvironment('Thanks for your submission.', $submission); } // Add Bcc? $bccEmailAddress = craft()->amForms_settings->getSettingsByHandleAndType('bccEmailAddress', AmFormsModel::SettingGeneral); if ($bccEmailAddress && $bccEmailAddress->value) { $bccAddresses = ArrayHelper::stringToArray($bccEmailAddress->value); $bccAddresses = array_unique($bccAddresses); if (count($bccAddresses)) { $properBccAddresses = array(); foreach ($bccAddresses as $bccAddress) { $bccAddress = $this->_translatedObjectPlusEnvironment($bccAddress, $submission); if (filter_var($bccAddress, FILTER_VALIDATE_EMAIL)) { $properBccAddresses[] = array('email' => $bccAddress); } } if (count($properBccAddresses)) { $notificationEmail->bcc = $properBccAddresses; $confirmationEmail->bcc = $properBccAddresses; } } } // Add files to the notification? if ($form->notificationFilesEnabled) { foreach ($submission->getFieldLayout()->getTabs() as $tab) { // Tab fields $fields = $tab->getFields(); foreach ($fields as $layoutField) { // Get actual field $field = $layoutField->getField(); // Find assets if ($field->type == 'Assets') { foreach ($submission->{$field->handle}->find() as $asset) { if ($asset->source->type == 'S3') { $file = @file_get_contents($asset->url); // Add asset as attachment if ($file) { $notificationEmail->addStringAttachment($file, $asset->filename); } } else { $assetPath = craft()->amForms->getPathForAsset($asset); // Add asset as attachment if (IOHelper::fileExists($assetPath . $asset->filename)) { $notificationEmail->addAttachment($assetPath . $asset->filename); } } } } } } } // Fire an 'onBeforeEmailSubmission' event $event = new Event($this, array('email' => $notificationEmail, 'submission' => $submission)); $this->onBeforeEmailSubmission($event); // Is the event giving us the go-ahead? if ($event->performAction) { // Send emails foreach ($recipients as $recipient) { $notificationEmail->toEmail = $this->_translatedObjectPlusEnvironment($recipient, $submission); if (filter_var($notificationEmail->toEmail, FILTER_VALIDATE_EMAIL)) { if (craft()->email->sendEmail($notificationEmail, array('amFormsSubmission' => $submission))) { $success = true; } } } // Send copy? if ($form->sendCopy) { $sendCopyTo = $submission->{$form->sendCopyTo}; if (filter_var($sendCopyTo, FILTER_VALIDATE_EMAIL)) { $confirmationEmail->toEmail = $this->_translatedObjectPlusEnvironment($sendCopyTo, $submission); if (craft()->email->sendEmail($confirmationEmail, array('amFormsSubmission' => $submission))) { $success = true; } } } // Fire an 'onEmailSubmission' event $this->onEmailSubmission(new Event($this, array('success' => $success, 'email' => $notificationEmail, 'submission' => $submission))); } return $success; }
public function actionSendSupportRequest() { $this->requirePostRequest(); craft()->config->maxPowerCaptain(); $success = false; $errors = array(); $zipFile = null; $tempFolder = null; $getHelpModel = new FeedMe_GetHelpModel(); $getHelpModel->fromEmail = craft()->request->getPost('fromEmail'); $getHelpModel->feedIssue = craft()->request->getPost('feedIssue'); $getHelpModel->message = trim(craft()->request->getPost('message')); $getHelpModel->attachLogs = (bool) craft()->request->getPost('attachLogs'); $getHelpModel->attachSettings = (bool) craft()->request->getPost('attachSettings'); $getHelpModel->attachFeed = (bool) craft()->request->getPost('attachFeed'); $getHelpModel->attachFields = (bool) craft()->request->getPost('attachFields'); $getHelpModel->attachment = UploadedFile::getInstanceByName('attachAdditionalFile'); if ($getHelpModel->validate()) { $plugin = craft()->plugins->getPlugin('feedMe'); $feed = craft()->feedMe_feeds->getFeedById($getHelpModel->feedIssue); // Add some extra info about this install $message = $getHelpModel->message . "\n\n" . "------------------------------\n\n" . 'Craft ' . craft()->getEditionName() . ' ' . craft()->getVersion() . '.' . craft()->getBuild() . "\n\n" . 'Feed Me ' . $plugin->getVersion(); try { $zipFile = $this->_createZip(); $tempFolder = craft()->path->getTempPath() . StringHelper::UUID() . '/'; if (!IOHelper::folderExists($tempFolder)) { IOHelper::createFolder($tempFolder); } // // Attached just the Feed Me log // if ($getHelpModel->attachLogs) { if (IOHelper::folderExists(craft()->path->getLogPath())) { $logFolderContents = IOHelper::getFolderContents(craft()->path->getLogPath()); foreach ($logFolderContents as $file) { // Just grab the Feed Me log if (IOHelper::fileExists($file) && basename($file) == 'feedme.log') { Zip::add($zipFile, $file, craft()->path->getStoragePath()); } } } } // // Backup our feed settings // if ($getHelpModel->attachSettings) { if (IOHelper::folderExists(craft()->path->getDbBackupPath())) { $backup = craft()->path->getDbBackupPath() . StringHelper::toLowerCase('feedme_' . gmdate('ymd_His') . '.sql'); $feedInfo = $this->_prepareSqlFeedSettings($getHelpModel->feedIssue); IOHelper::writeToFile($backup, $feedInfo . PHP_EOL, true, true); Zip::add($zipFile, $backup, craft()->path->getStoragePath()); } } // // Save the contents of the feed // if ($getHelpModel->attachFeed) { $feedData = craft()->feedMe_feed->getRawData($feed->feedUrl); $tempFile = $tempFolder . 'feed.' . StringHelper::toLowerCase($feed->feedType); IOHelper::writeToFile($tempFile, $feedData . PHP_EOL, true, true); if (IOHelper::fileExists($tempFile)) { Zip::add($zipFile, $tempFile, $tempFolder); } } // // Get some information about the fields we're mapping to - handy to know // if ($getHelpModel->attachFields) { $fieldInfo = array(); foreach ($feed->fieldMapping as $feedHandle => $fieldHandle) { $field = craft()->fields->getFieldByHandle($fieldHandle); if ($field) { $fieldInfo[] = $this->_prepareExportField($field); } } // Support PHP <5.4, JSON_PRETTY_PRINT = 128, JSON_NUMERIC_CHECK = 32 $json = json_encode($fieldInfo, 128 | 32); $tempFile = $tempFolder . 'fields.json'; IOHelper::writeToFile($tempFile, $json . PHP_EOL, true, true); if (IOHelper::fileExists($tempFile)) { Zip::add($zipFile, $tempFile, $tempFolder); } } // // Add in any additional attachments // if ($getHelpModel->attachment) { $tempFile = $tempFolder . $getHelpModel->attachment->getName(); $getHelpModel->attachment->saveAs($tempFile); // Make sure it actually saved. if (IOHelper::fileExists($tempFile)) { Zip::add($zipFile, $tempFile, $tempFolder); } } } catch (\Exception $e) { FeedMePlugin::log('Tried to attach debug logs to a support request and something went horribly wrong: ' . $e->getMessage(), LogLevel::Warning, true); } $email = new EmailModel(); $email->fromEmail = $getHelpModel->fromEmail; $email->toEmail = "*****@*****.**"; $email->subject = "Feed Me Support"; $email->body = $message; if ($zipFile) { $email->addAttachment($zipFile, 'FeedMeSupportAttachment.zip', 'base64', 'application/zip'); } $result = craft()->email->sendEmail($email); if ($result) { if ($zipFile) { if (IOHelper::fileExists($zipFile)) { IOHelper::deleteFile($zipFile); } } if ($tempFolder) { IOHelper::clearFolder($tempFolder); IOHelper::deleteFolder($tempFolder); } $success = true; } else { $errors = array('Support' => array('Unable to contact support. Please try again soon.')); } } else { $errors = $getHelpModel->getErrors(); } $this->returnJson(array('success' => $success, 'errors' => $errors)); }
/** * @param EmailModel $email * @param AssetFileModel[] $assets */ protected function attachAssetFilesToEmailModel(EmailModel $email, array $assets) { foreach ($assets as $asset) { $name = $asset->filename; $path = $this->getAssetFilePath($asset); $email->addAttachment($path, $name); } }
/** * Send Email Notification * */ public function sendEmailNotification($form, $files, $postData, $customEmail, $customSubject, $message, $html = true, $email = null) { $errors = false; $attributes = $form->getAttributes(); $notificationSettings = $attributes['notificationSettings']; $toEmails = ArrayHelper::stringToArray($notificationSettings['emailSettings']['notifyEmail']); $emailSettings = craft()->email->getSettings(); if (isset($notificationSettings['replyTo']) && $notificationSettings['replyTo'] != '') { $replyTo = $postData[$notificationSettings['replyTo']]; } else { $replyTo = $emailSettings['emailAddress']; } // Process Subject Line if ($customSubject) { $subject = $customSubject; } else { $subject = $notificationSettings['emailSettings']['emailSubject']; } if ($customEmail != '') { $theEmailAddress = explode('|', $customEmail); ArrayHelper::prependOrAppend($toEmails, $theEmailAddress[0], true); } foreach ($toEmails as $toEmail) { $email = new EmailModel(); $email->fromEmail = $emailSettings['emailAddress']; $email->replyTo = $replyTo; $email->sender = $emailSettings['emailAddress']; $email->fromName = $form->name; $email->toEmail = $toEmail; $email->subject = $subject; $email->htmlBody = $message; // Attach files to email if (!empty($files)) { foreach ($files as $attachment) { $email->addAttachment($attachment['tempPath'], $attachment['filename'], 'base64', $attachment['type']); } } if (!craft()->email->sendEmail($email)) { $errors = true; } } return $errors ? false : true; }