drupal Webform HTML Email hooks - html

I'm trying to send a thank you email to the user submitting the form in HTML.
I found out by using a hook in my template.php file like this works to set the header correctly:
function mythemename_webform_mail_headers($form_values, $node, $sid) {
$headers = array(
'Content-Type' => 'text/html; charset=UTF-8; format=flowed; delsp=yes',
'X-Mailer' => 'Drupal Webform (PHP/'. phpversion() .')',
);
return $headers;
}
This works freat for the "Thank You" email.
The email that the site admin gets with the form results is also html, but it's not converting newlines to breaks in this email. I can't figure out how to use a hook for this, so I had to edit the webform.module file and do this:
function webform_mail($key, &$message, $params) {
$message['headers'] = array_merge($message['headers'], $params['headers']);
$message['subject'] = $params['subject'];
//$message['body'][] = drupal_wrap_mail($params['message']); // replaced this with line below
$message['body'][] = nl2br(drupal_wrap_mail($params['message']));
}
Can this be done with a hook in template.php?

You can use hook_mail_alter to edit mails created with hook_mail, which is what webform uses.

You can't use hook_mail_alter() in a theme, only in a custom module.

Old topic but still usefull I guess. On the webform module's edit page there is a option/fieldset with additional processing:
<?php
$to = $form_values['submitted_tree']['uw_gegevens']['e_mail'];
$from = "no-reply#example.com";
$achternaam = $form_values['submitted_tree']['uw_gegevens']['uw_naam'];
$message = drupal_mail('webform_extra', 'reply', $to, language_default(), array('body' => $body), $from, TRUE);
function webform_extra_mail($key, &$message, $params) {
$message['subject'] = "TEXT.";
$message['body'] = "
TEXT, " . $params['achternaam'] . "
TEXT.
KIND REGARDS,
TEXT
";
} ?>
Hope this helps
Guus van de Wal

Related

How to include HTML tags when making a React\Http\Message\Response (ReactPHP)

How can I write tags from HTML in React\Http\Message\Response?
<?php
use Psr\Http\Message\ServerRequestInterface;
use React\EventLoop\Loop;
use React\Http\HttpServer;
use React\Http\Message\Response;
use React\Socket\SocketServer;
require_once __DIR__ . '/vendor/autoload.php';
$loop = Loop::get();
$server = new HttpServer(function (ServerRequestInterface $request) {
return new Response(
200, ['Content-Type' => 'application/json'], "<h1>Hello World</h1>"
);
});
$socket = new SocketServer('127.0.0.1:8000');
$server->listen($socket);
echo 'Listening on ' . str_replace('tcp', 'http', $socket->getAddress()) . PHP_EOL;
$loop->run();
My code writes out "<h1>Hello World</h1>;" instead of making an h1 element with the contents of "Hello World". How can I make it return an h1 element instead of just writing it out with tags
ReactPHP core developer here. Through the Content-Type header you're telling the browser the content is JSON with the current value of application/json. When you set the Content-Type header to text/html the browser will render it as HTML.

How can I make a 'report' button to send a message without going through email?

What I'm looking for exactly is a button that says something like, "Report a typo," and notifies me when somebody clicks that button on that page.
To clarify, I've got a big writing project with many short pages -- a couple paragraphs in an html template each, which I know could be done much more concisely, but that's another matter. It would help me if, when reading through, I could just click a button every time I (or whoever is reading) see something screwy, then get a message saying on which page the button was clicked without having to send an email. I'm fine with an automatic message to my email (even just as simple as the page name, like 'example.html' in the body), but I don't want to have to open my email client, etc. Honestly, any way I can get notification is fine (can html5 send carrier pigeons?), as long as the user interaction remains minimal. Is there a relatively simple way to do this? I'm happy to learn, but not exactly an expert as it stands.
I apologize in advance if I was unclear, if this has been asked before (I couldn't really think of how to search for what I'm asking), or if the answer is ridiculously simple and I'm just completely missing it. I'm kind of a noob at all this, so I appreciate any patience if this isn't an ideal question, but I'll try to update with suggested fixes. Thanks!
if ($this->form_validation->run() == FALSE) {
$this->load->view('welcome_message');
} else {
$this->load->view('formsuccess');
$name = $this->input->post('name');
$email = $this->input->post('email');
$cell = $this->input->post('cell');
$msg = $this->input->post('msg');
// $captcha=$this->input->post('captcha');
// echo $this->session->flashdata('email_sent');
$data = array('name' => $name, 'email' => $email, 'cell' => $cell, 'msg' => $msg);
$this->load->model('mymodel');
if ($this->mymodel->add($data)) {
$this->load->library('email');
//SMTP & mail configuration
$config = array(
'protocol' => 'smtp',
'smtp_host' => 'ssl://smtp.googlemail.com',
'smtp_port' => 465,
'smtp_user' => 'consecutive.development#gmail.com',
'smtp_pass' => 'devAccountCB!',
'mailtype' => 'html',
'charset' => 'utf-8'
);
$this->email->initialize($config);
$this->email->set_mailtype("html");
$this->email->set_newline("\r\n");
//Email content
$subject = 'Call Back Request';
$message = $this->input->post('msg');
$name = $this->input->post('name');
$from = $this->input->post('email');
$to = 'consecutive.development#gmail.com';
$this->email->from($from, $name);
$this->email->to($to);
$this->email->subject($subject);
$this->email->message("From : " . $from . " <br> Message: " . $message . "<br>Phone Number:" . $cell);
if ($this->email->send()) {
$this->mymodel->add($data);
$this->session->set_flashdata('success_msg', 'Message sent successfully');
//$success= $this->session->set_flashdata('success_msg');
} else {
$this->session->set_flashdata('error_msg', 'Error sending message!');
//redirect('Welcome/index');
// show_error($this->email->print_debugger());
}
echo '<script>alert("Your Message has been sent. Thankyou!");</script>';
redirect('');
It is impossible to do that in Html5 , however it can be done using Jquery or php, as you may need some backend site scripting language.

Yii2 mpdf with barcode-generator

I'm using yii2 and installed two extension : mpdf , and yii2-barcode-generator-8-types.
Both of them were installed and configured properly and working well.
But what I can't do is to load barcode into pdf.
Here is my code :
Controller :
public function actionPdf()
{
$pdf = Yii::$app->pdf;
$pdf->content = $this->renderPartial('_pdf');
echo $this->renderPartial('_pdf');exit;
return $pdf->render();
}
View :
<div id="showBarcode"></div>
<?php
use barcode\barcode\BarcodeGenerator as BarcodeGenerator;
$optionsArray = array(
'elementId'=> 'showBarcode',
'value'=> '12345678',
'type'=>'code128',
);
echo BarcodeGenerator::widget($optionsArray);
?>
And it show something like this
but If I try to delete all code inside actionPdf() and just write
return $this->render("_pdf");
it show like this:
Please help!!!
I think your controller should be this
public function actionPdf()
{
$pdf = Yii::$app->pdf;
$pdf->content = $this->renderPartial('_pdf');
return $pdf->render();
}
The row with echo $this->renderPartial('_pdf');exit; don't must be used because it prevents the program to invoke correctly the render of the pdf page. If you use this instruction you displays only the render of "html like code" page like you see in result you posted moreover immediately after this instruction you exit form action without invoking the $pdf->render.

Trouble with html form in drupal

This is part of code I have problem with
if(user_is_logged_in())
{
$cover='<form method="post">
<input type="text" name="tekst" /><input type="submit" id="add" value="Dodaj adres okładki" />
</form></td></tr>';
}
else
{
$cover=" ";
}
$description .= '<tr><td width="60px">Title</td><td>'.$this->getTytul().'</td><td rowspan="20" width="150px">'.$cover.'</td></tr>';
and I have to run that query when user click submit button
db_query("INSERT INTO okladki_publikacji(id_publikacji,adres_okladki) VALUES(".$this->getId().",".$value_from_form.")");
but I have no idea how to do it in drupal
What I want is run function with that query on action in form but how? action =add_cover() doesnt work
Drupal has an amazing forms API that can help you doing this kind of stuff in a breeze.
Basically you define a form using this API and then define the function to be invoked once the user submits it and that's it. Here's an example on how to define the form:
function myformname_form($form, &$form_state){
// define the input field
$form['name'] = array(
'#type' => 'textfield',
'#title' => t('Dodaj adres okładki'),
);
// define the submit button
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Apply'),
);
return $form;
}
Then you define the function that will be executed once the form is executed. Function basically has to be named the same + "_submit" - Drupal will do the magic behind the scenes to tie it together.
function myformname_form_submit($form, &$form_state){
// and this will be where you execute the query you wanted.
}
Would recommend installing Devel module so you can inspect the form_state variable - that's where the input field value will be.
Now, you can just render the form on your code below using drupal_get_form like this:
$cover = " ";
if(user_is_logged_in()) {
$form = drupal_get_form('myformname_form');
$cover = render($form);
}
$description .= '<tr><td width="60px">Title</td><td>'.$this->getTytul().'</td><td rowspan="20" width="150px">'.$cover.'</td></tr>';
Hope this helps!

CKeditor returning 403 when submitting certain html tags

I've created a page where I have two input textareas and I add CKeditor (ver. 4) to both of them.
The first editor works fine, I've set config.allowedContent = true; in the config.js to stop stripping tags like <script> and everything works as expected.
I have another editor right below it, same settings, same setup, I just changed the ID of the textarea field. It works when I submit normal text, but as soon as I add a <script> tag, for example, and press the submit button of the form in which the editors are in it seems to reload the page, doesn't submit any data and firebug tells me that the server returns 403.
I tried isolating the editor, adding personal configuration. Nothing. The first textarea works like a charm, second one returns 403 if the text has unsafe tags in it.
My setup is as follows, I'm using this ckeditor helper to insert the editors where I need. Page is created with CodeIgniter as you guessed.
I got a config.js file in ckeditor folder.
I'm using a regular form, nothing fancy about it. It looks like this
<form action="http://domain.com/admin/articles/edit/47" method="post">
<div id="cke_ckeditor_en_container">
<textarea cols="75" rows="7" id="ckeditor_en" name="text_en" class="input-text is-col-text"><?php echo set_value('text_en', isset($text_en) ? htmlspecialchars_decode($text_en) : ''); ?></textarea>
<?php echo display_ckeditor($ckeditor_en); ?>
</div>
<input type="submit" value="submit" />
</form>
The form has another part of html for the other editor which is the same, with changed id and other attributes, and a checkbox, nothing relevant.
And got this in my controller
public function edit(){
$this->load->helper('ckeditor');
$id = (int)$this->uri->segment(4);
if (empty($id)){
$this->session->set_flashdata('error', 'Empty ID!');
redirect('admin/articles');
}
$data = $this->articles_model->fetch_article($id);
$data['page_title'] = "Edit `" . $data['title'] . "`";
$data['form_url'] = "admin/articles/edit/" . $id;
$data['ckeditor'] = array(
'id' => 'ckeditor',
'path' => 'js/ckeditor');
$data['ckeditor_en'] = array(
'id' => 'ckeditor_en',
'path' => 'js/ckeditor');
$data['edit'] = true;
if($this->input->post('submit')){
$this->save_article("update",$id);
}
$this->load->view("admin/articles",$data);
}
private function save_article($type='insert', $id=0){
$this->load->library('form_validation');
$this->form_validation->set_rules('title','Title','trim|xss_clean|max_length[150]|min_length[1]');
$this->form_validation->set_rules('text','Text','trim');
$this->form_validation->set_rules('title_en','Title EN','trim|xss_clean|max_length[150]|min_length[1]');
$this->form_validation->set_rules('text_en','Text EN','trim');
$this->form_validation->set_rules('top_menu','Show in top menu','trim|xss_clean|max_length[1]');
if ($this->form_validation->run() === FALSE)
{
return FALSE;
}
// make sure we only pass in the fields we want
$data = array();
$data['title'] = $this->input->post('title');
$data['text'] = htmlspecialchars($this->input->post('text'));
$data['title_en'] = $this->input->post('title_en');
$data['text_en'] = htmlspecialchars($this->input->post('text_en'));
$data['url'] = $this->toAscii($this->input->post('title'));
$data['url_en'] = $this->toAscii($this->input->post('title_en'));
$data['top_menu'] = $this->input->post('top_menu');
if($type == "insert"){
$data['time'] = date("YmdHis");
}
if ($type == 'insert'){
if($this->articles_model->insert($data)){
$this->session->set_flashdata('success', 'Article added successfully!');
}else{
$this->session->set_flashdata('error', 'An error occured!');
}
}else if ($type == 'update'){
if($this->articles_model->update($id, $data)){
$this->session->set_flashdata('success', 'Article `' . $data['title'] . '` edited successfully!');
}else{
$this->session->set_flashdata('error', 'An error ecc!');
}
}
redirect("admin/articles");
}
Safety, or unsafeness, to be exact, of my code is not relevant
edit
Adding config.js for ckeditor.
CKEDITOR.editorConfig = function( config ) {
config.filebrowserBrowseUrl = '/js/kcfinder/browse.php?type=files';
config.filebrowserImageBrowseUrl = '/js/kcfinder/browse.php?type=images';
config.filebrowserFlashBrowseUrl = '/js/kcfinder/browse.php?type=flash';
config.filebrowserUploadUrl = '/js/kcfinder/upload.php?type=files';
config.filebrowserImageUploadUrl = '/js/kcfinder/upload.php?type=images';
config.filebrowserFlashUploadUrl = '/js/kcfinder/upload.php?type=flash';
config.removeButtons = 'Underline,Subscript,Superscript';
config.allowedContent = true;
// Se the most common block elements.
config.format_tags = 'p;h1;h2;h3;pre';
// Make dialogs simpler.
config.removeDialogTabs = 'image:advanced;link:advanced';
};
I'm stumped and stupified, I've got no ideas on what to do. It seems that the one input has been cursed.
Any help appreciated, thank you.
This would be the result of your mod_security rules. Depending on how strict they are they help better protect scripts from being hacked through vulnerabilities, generally those exploited via POST's.
As I understand you are trying to add something to your second textarea. And CKEditor removing some "unsafe" tags. I won't be very secure, but this can help you:
config.extraAllowedContent = '*{*}';
You will add this to your config.js. This code provides you to add anything you want. And CKEditor won't delete "unsafe" tags.
Documenation for this method
https://www.bilisimkitabi.com/403-error-on-submit-of-ckeditor
You can add to following code in your .htaccess file
#ckeditor Post 403 problem
<IfModule mod_security.c>
SecFilterEngine Off
SecFilterScanPOST Off
</IfModule>
#ckeditor Post 403 problem