i am trying to set html on the output of the email send by joomla. my file is located in the joomla core.
i know i have to add something like ->isHTML(true); but i do not know where and how.
here is the code:
class MailtoController extends JControllerLegacy
{
/**
* Show the form so that the user can send the link to someone.
*
* #return void
*
* #since 1.5
*/
public function mailto()
{
$session = JFactory::getSession();
$session->set('com_mailto.formtime', time());
$this->input->set('view', 'mailto');
$this->display();
}
public function send()
{
// Check for request forgeries
JSession::checkToken() or jexit(JText::_('JINVALID_TOKEN'));
$app = JFactory::getApplication();
$session = JFactory::getSession();
$timeout = $session->get('com_mailto.formtime', 0);
if ($timeout == 0 || time() - $timeout < 1)
{
JError::raiseNotice(500, JText::_('COM_MAILTO_EMAIL_NOT_SENT'));
return $this->mailto();
}
$SiteName = $app->get('sitename');
$link = MailtoHelper::validateHash($this->input->get('link', '', 'post'));
// Verify that this is a local link
if (!$link || !JUri::isInternal($link))
{
// Non-local url...
JError::raiseNotice(500, JText::_('COM_MAILTO_EMAIL_NOT_SENT'));
return $this->mailto();
}
// An array of email headers we do not want to allow as input
$headers = array (
'Content-Type:',
'MIME-Version:',
'Content-Transfer-Encoding:',
'bcc:',
'cc:'
);
// An array of the input fields to scan for injected headers
$fields = array(
'mailto',
'sender',
'from',
'subject',
);
/*
* Here is the meat and potatoes of the header injection test. We
* iterate over the array of form input and check for header strings.
* If we find one, send an unauthorized header and die.
*/
foreach ($fields as $field)
{
foreach ($headers as $header)
{
if (strpos($_POST[$field], $header) !== false)
{
JError::raiseError(403, '');
}
}
}
/*
* Free up memory
*/
unset ($headers, $fields);
$email = $this->input->post->getString('mailto', '');
$sender = $this->input->post->getString('sender', '');
$from = $this->input->post->getString('from', '');
$subject_default = JText::sprintf('COM_MAILTO_SENT_BY', $sender);
$subject = $this->input->post->getString('subject', $subject_default);
// Check for a valid to address
$error = false;
if (!$email || !JMailHelper::isEmailAddress($email))
{
$error = JText::sprintf('COM_MAILTO_EMAIL_INVALID', $email);
JError::raiseWarning(0, $error);
}
// Check for a valid from address
if (!$from || !JMailHelper::isEmailAddress($from))
{
$error = JText::sprintf('COM_MAILTO_EMAIL_INVALID', $from);
JError::raiseWarning(0, $error);
}
if ($error)
{
return $this->mailto();
}
// Build the message to send
$msg = JText::_('COM_MAILTO_EMAIL_MSG');
$link = $link;
//$body = sprintf($msg, $SiteName, $sender, $from, $link);
$body = "<p>Hello Test F,</p><br/><p>Thank you for registering at Deals&offers. Your account is created and activated.</p><br/>You may login to ".$SiteName." using the following username and password:</br><p>Username: ".$sender."</p><p>Password: ".$from."/p><br/><p><b>Note:</b> It is recomended to change your password after first login. ".$link."</p>";
// Clean the email data
$subject = JMailHelper::cleanSubject($subject);
$body = JMailHelper::cleanBody($body);
// To send we need to use punycode.
$from = JStringPunycode::emailToPunycode($from);
$from = JMailHelper::cleanAddress($from);
$email = JStringPunycode::emailToPunycode($email);
// Send the email
if (JFactory::getMailer()->sendMail($from, $sender, $email, $subject, $body) !== true)
{
JError::raiseNotice(500, JText::_('COM_MAILTO_EMAIL_NOT_SENT'));
return $this->mailto();
}
JFactory::getApplication()->enqueueMessage('ok!', '');
$this->input->set('view', 'sent');
$this->display();
}
}
thank you very much
you can add before body or between subject and body . however, it must be before the submit command !!
here is an example of PhpMailler firstly, you need to call the class like this and you can use it
$this->mail= new PHPMailer();
$this->mail->IsSMTP();
$this->mailIsHTML(true);
$subject = JMailHelper::cleanSubject($subject);
$body = JMailHelper::cleanBody($body);
however if the function is static also you call the function in same class
you can call the function by sef command
self::mailIsHTML(true)
Related
i have two rest api end points for the Calculator and another for the email submission.
i want to include the data from calculator to the email handler function.
Calculator Handler:
// function for the endpoint for form
add_action('rest_api_init', 'create_rest_endpoint');
function create_rest_endpoint() {
// Create endpoint for front end to connect to WordPress securely to post form data
register_rest_route('bohio/v1', 'submit', array(
'methods' => 'POST',
'callback' => 'handle_query',
'permission_callback' => '__return_true'
));
}
// callback method
function handle_query($data) {
// Handle the form data that is posted
// Get all parameters from form
$output = $data->get_params();
// form fields : service, bed, bath,square_feet,cadence
}
Email handler:
function handle_email($data) {
// Handle the form data that is posted
// Get all parameters from form
$emailData = $data->get_params();
// Set fields from the form
$field_name = sanitize_text_field($emailData['name']);
$field_email = sanitize_email($emailData['email']);
//Check if nonce is valid, if not, respond back with error
if (!wp_verify_nonce($emailData['_wpnonce'], 'wp_rest')) {
return new WP_Rest_Response('Message not sent', 422);
}
else {
// Remove unneeded data from paramaters
unset($emailData['_wpnonce']);
unset($emailData['_wp_http_referer']);
// Send the email message
$headers = [];
// $admin_email = get_bloginfo('admin_email');
$admin_email = get_option('bh_email_sub');
// var_dump($admin_email);
$from_email = get_option('bh_email_sub');
$admin_name = get_option('bh_admin_sub_name');
// $admin_name = get_bloginfo('name');
// Set admin email as recipient email if no option has been set
$recipient_email = $admin_email;
$headers[] = "From: {$admin_name} <{$from_email}>";
$headers[] = "Reply-to: {$field_name} <{$field_email}>";
$headers[] = "Content-Type: text/html";
$subject = "New email submission from {$field_name}";
$message = '';
$message = "<h2>New Email submission from {$field_name}</h2>";
// Loop through each field posted and sanitize it
foreach ($emailData as $label => $value) {
switch ($label) {
case 'message':
$value = sanitize_textarea_field($value);
break;
case 'email':
$value = sanitize_email($value);
break;
default:
$value = sanitize_text_field($value);
}
$message .= '<strong>' . sanitize_text_field(ucfirst($label)) . ':</strong> ' . $value . '<br />';
}
wp_mail($recipient_email, $subject, $message, $headers);
$confirmation_message = "The message was sent successfully!!";
// return $confirmation_message;
return $confirmation_message;
}
}
How would i access the data from handle_query to handle_email and send via email. Both functions lies in the same file of a plugin.
I have created a custom registration form in Drupal 8, and now i want to sent a mail from the submission of the form. So I have did it like this
This is my .module file
/**
* Implements hook_mail().
*/
function Registration_form_mail($key, &$message, $params) {
$options = array(
'langcode' => $message['langcode'],
);
switch ($key) {
case 'contact_form':
$message['from'] = \Drupal::config('system.site')->get('mail');
$message['subject'] = $params['subject'];
$message['body'][] = $params['body'];
break;
}
}
public function submitForm(array &$form, FormStateInterface $form_state){
$mailManager = \Drupal::service('plugin.manager.mail');
$module = 'Registration_form';
$key = 'contact_form'; // Replace with Your key
$to = $form_state->getValue('Email');
$params = array(
'body' => 'test',
'subject' => 'Website Information Request',
);
$langcode = \Drupal::currentUser()->getPreferredLangcode();
$send = true;
$message['subject'] = t('nouveau contact ');
$message['body'][] = t('test');
$result = $mailManager->mail($module, $key, $to, $langcode, $params, NULL, $send);
if ($result['result'] != true) {
$message = t('There was a problem sending your email notification to #email.', array('#email' => $to));
drupal_set_message($message, 'error');
\Drupal::logger('mail-log')->error($message);
return;
}
else{
$message = t('An email notification has been sent to #email ', array('#email' => $to));
drupal_set_message($message);
\Drupal::logger('mail-log')->notice($message);
}
}
So my question is i'm using localhost in xampp and i want to sent a mail after submission of the form, but i'm getting this error
Unable to send email. Contact the site administrator if the problem persists.
There was a problem sending your email notification to ABC#gmail.com
So how I can resolve my problem, i have gone through whole sites but not able to find answer.
The problem may be with the email configuration. See if this article helps you:
https://stevepolito.design/blog/drupal-configure-smtp-module-work-gmail-updated/
I'm using sendgrid to send out html emails from a document system I'm building. When a file uploads sendgrid needs to email all those associated with that case. I have everything working for individual emails and I can customs the template I have saved but cant send the email to multiple recipients
I have generated an array of recipients
$email = array(j.bloggs#bloggs.com, j.doe#me.net, d.smith#smith.co.uk);
I want to pass this into my sendgrid email object to send to them all
private function send() {
$sg = new \SendGrid(self::$key);
$response = $sg->client->mail()->send()->post(self::$mail);
return $response->statusCode();
}
public function file_saved($file="", $case="") {
self::$from = new SendGrid\Email($this->fromName, $this->fromEmail);
self::$to = new SendGrid\Email($this->toName, $this->toEmail);
self::$content = new SendGrid\Content("text/html", "Hello, Email!");
self::$mail = new SendGrid\Mail(
self::$from,
$this->subject,
self::$to,
self::$content
);
$str = "<p>A file has been successfully uploaded to {$case->case_name} ({$case->case_code}).</p>
<br />
<p>{$file->file_name} - ".size($file->file_size)."</p>";
self::$mail->personalization[0]->addSubstitution("-name-", $this->toName);
self::$mail->personalization[0]->addSubstitution("-str-", $str);
self::$mail->personalization[0]->addSubstitution("-btn-", "Download File");
self::$mail->personalization[0]->addSubstitution("-url-", HTTP.BASE_URL.DS.'uploads'.DS.$file->file_path);
self::$mail->setTemplateId("2f845487-6243-4562-b6fb-022185b7fde7");
if (!$this->send() == 202) {
return false;
}
else {
return true;
}
}
I tried to use the personalization->to and pass that the array but get the error
Call to a member function to() on null in includes/classes/mail.php on line <b>82</b><br />
I just created a loop that went through each recipient and sent them an email, not as efficient as generating a multiple to: list but for now its working until I get a response from the sendgrid dev team
$this->email = array(j.bloggs#bloggs.com, j.doe#me.net, d.smith#smith.co.uk);
private function send() {
$sg = new \SendGrid(self::$key);
$response = $sg->client->mail()->send()->post(self::$mail);
return $response->statusCode();
}
public function file_saved($file="", $case="") {
$str = "<p>A file has been successfully uploaded to {$case->case_name} ({$case->case_code}).</p>
<br />
<p>{$file->file_name} - ".size($file->file_size)."</p>";
self::$from = new SendGrid\Email($this->fromName, $this->fromEmail);
self::$content = new SendGrid\Content("text/html", "Hello, Email!");
foreach($this->email as $email_addr) {
self::$to = new SendGrid\Email($this->toName, $email_addr);
self::$mail = new SendGrid\Mail(
self::$from,
$this->subject,
self::$to,
self::$content
);
self::$mail->personalization[0]->addSubstitution("-str-", $str);
self::$mail->personalization[0]->addSubstitution("-btn-", "Download File");
self::$mail->personalization[0]->addSubstitution("-url-", HTTP.BASE_URL.DS.'uploads'.DS.$file->file_path);
self::$mail->setTemplateId("2f845487-6243-4562-b6fb-022185b7fde7");
if (!$this->send() == 202) {
$response[$address] = false;
}
else {
$response[$address] = true;
}
}
return $response; // contains true/false for each email address if sent or not.
}
I have a mail application that send about 5000 emails daily(a lot of account paperwork) via cron job, and works fine when the mail is sending to just one recipient. The problem comes when we activate the BCC copy, then the app starts sending until 980-1050 mails and start recieving 4.5.3 error from the smtp(Too many recipients). If i pause the job and run the cron again, the php process got a new pid, and start sending ok until it reach the same limit(980-1050 mails);
So my question is: is there a way to regenerate the php process id ?
If i was capable to do that, then the app would send those mails without problem.
Or maybe i'm missing some postfix configuration?
Relevant part of the code:
/**
* _Send_SmtpData
* Handles the SMTP negotiation for sending the email header and body.
*
* #param String $rcpt_to The 'receipt to' address to send the email to. This is a bare email address only.
* #param String $to The 'to' address to send this to. This can contain a name / email address in the standard format ("Name" <email#address>)
* #param String $subject The subject of the email to send.
* #param String $body The body of the email to send.
* #param String $headers The headers of the email to send.
**/
function _Send_SmtpData(&$rcpt_to, &$to, &$subject, &$body, &$headers)
{
$data = "DATA";
$this->DebugMemUsage('Trying to put ' . $data);
if (!$this->_Put_Smtp_Connection($data)) {
$this->ErrorCode = 12;
$this->ErrorCodeSMTPEnhanced = false;
$this->Error = GetLang('UnableToSendEmail_Data');
$this->_Close_Smtp_Connection();
$this->DebugMemUsage('Got error ' . $this->Error);
return array(false, $this->Error);
}
$response = $this->_get_response();
$this->DebugMemUsage('Got response ' . $response);
$responsecode = substr($response, 0, 3);
if ($responsecode != '354') {
$this->ErrorCode = $responsecode;
$this->ErrorCodeSMTPEnhanced = $this->_GetSMTPEnhancedErrorCode($response);
$this->Error = $response;
$this->_Close_Smtp_Connection();
$this->DebugMemUsage('Got error ' . $this->Error);
return array(false, $this->Error);
}
$msg = "To: " . $to . $this->_smtp_newline . "Subject: " . $subject . $this->_smtp_newline . $headers . $this->_smtp_newline . preg_replace('/^\.(\r|\n)/m', ' .${1}', $body);
$msg = str_replace("\r\n","\n",$msg);
$msg = str_replace("\r","\n",$msg);
$lines = explode("\n",$msg);
foreach ($lines as $no => $line) {
// we need to rtrim here so we don't get rid of tabs before the start of the line.
// the tab is extremely important for boundaries (eg sending multipart + attachment)
// so it needs to stay.
$data = rtrim($line);
$this->DebugMemUsage('Trying to put ' . $data);
if (!$this->_Put_Smtp_Connection($data)) {
$this->ErrorCode = 13;
$this->ErrorCodeSMTPEnhanced = false;
$this->Error = GetLang('UnableToSendEmail_DataWriting');
$this->_Close_Smtp_Connection();
$this->DebugMemUsage('Got error ' . $this->Error);
return array(false, $this->Error);
}
}
$data = $this->_smtp_newline . ".";
$this->DebugMemUsage('Trying to put ' . $data);
if (!$this->_Put_Smtp_Connection($data)) {
$this->ErrorCode = 14;
$this->ErrorCodeSMTPEnhanced = false;
$this->Error = GetLang('UnableToSendEmail_DataFinished');
$this->_Close_Smtp_Connection();
$this->DebugMemUsage('Got error ' . $this->Error);
return array(false, $this->Error);
}
$response = $this->_get_response();
$this->DebugMemUsage('Got response ' . $response);
$responsecode = substr($response, 0, 3);
if ($responsecode != '250') {
$this->ErrorCodeSMTPEnhanced = $this->_GetSMTPEnhancedErrorCode($response);
$this->ErrorCode = $responsecode;
$this->Error = $response;
$this->_Close_Smtp_Connection();
$this->DebugMemUsage('Got error ' . $this->Error);
return array(false, $this->Error);
}
$this->DebugMemUsage('Mail accepted ');
/**
* We got this far, this means we didn't encounter any errors.
* Cleanup previous error codes and variables since they are no longer relevant
* with the current process iteration.
*/
$this->Error = '';
$this->ErrorCode = false;
$this->ErrorCodeSMTPEnhanced = false;
$this->_smtp_email_count++;
return array(true, false);
}
In the end the "bug" was in another function, the bcc copy function; in every call for the working process, it was not cleaning the previous bcc copies of the mail, but sum them until they reach the limit.
I'm new to using EWS from Exchangeclient classes.
I'm looking for a simple example how to send an email with an attachment. I've found examples about how to send an email but not sending an email with an attachment.
This is my script:
$exchangeclient = new Exchangeclient();
$exchangeclient->init($username, $password, NULL, 'ews/Services.wsdl');
$exchangeclient->send_message($mail_from, $subject, $body, 'HTML', true, true);
function - PHP Classes:
function send_message($to, $subject, $content, $bodytype="Text", $saveinsent=true, $markasread=true) {
$this->setup();
if($saveinsent) {
$CreateItem->MessageDisposition = "SendOnly";
$CreateItem->SavedItemFolderId->DistinguishedFolderId->Id = "sentitems";
}
else
$CreateItem->MessageDisposition = "SendOnly";
$CreateItem->Items->Message->ItemClass = "IPM.Note";
$CreateItem->Items->Message->Subject = $subject;
$CreateItem->Items->Message->Body->BodyType = $bodytype;
$CreateItem->Items->Message->Body->_ = $content;
$CreateItem->Items->Message->ToRecipients->Mailbox->EmailAddress = $to;
if($markasread)
$CreateItem->Items->Message->IsRead = "true";
$response = $this->client->CreateItem($CreateItem);
$this->teardown();
if($response->ResponseMessages->CreateItemResponseMessage->ResponseCode == "NoError")
return true;
else {
$this->lastError = $response->ResponseMessages->CreateItemResponseMessage->ResponseCode;
return false;
}
}
You have to first save the email as a draft (with the appropriate message disposition), then CreateAttachment() so it has an attachment, then edit it with UpdateItem() so the message disposition is SendOnly. Then it will be sent.
See David Sterling's reply on this thread: http://social.technet.microsoft.com/Forums/en-US/exchangesvrdevelopment/thread/f7d5257e-ec98-40fd-b301-f378ba3080fd/ (It's about Meeting Requests but they work the same way.)