2

When I add a new Webform node type, this have no e-mail recipien by default. You can read this message:

Currently not sending e-mails, add an e-mail recipient below.

Is there a way to get a e-mail recipient value by default when creating a webform node?

5
+50

You can do this with a custom module and hook_node_presave(). After you have a custom module created, you can use code like this to create a default e-mail configuration for any created Webforms.

Assuming your module were called myexample.module, the hook would look like this:

<?php
/**
 * Implements hook_node_presave().
 */
function myexample_node_presave($node) {
  // Add a default e-mail configuration for new nodes.
  if (empty($node->nid) && $node->type == 'webform') {
    $node->webform['emails'][] = array(
      'email' => 'example@example.com',
      'subject' => 'default',
      'from_name' => 'default',
      'from_address' => 'default',
      'template' => 'default',
      'excluded_components' => array(),
      'html' => 0,
      'attachments' => 0,
    );
  }
}
?>
  • hook_node_presave will execute before each insert and update operation, and after each update new recipient will attached to webform, so add $node->is_new to if statement or use hook_node_insert – Mohammad Ali Akbari Apr 16 '12 at 6:22
  • Thanks it works as expected. Could I also set a default email template text, something like 'Hello, a message is sent from %title...' ? – chefnelone Apr 16 '12 at 11:41
  • I already get the default email template text by setting 'template' in the array. What about setting 'from_address' to the email component? – chefnelone Apr 16 '12 at 11:47
  • Adding $node->is_new isn't needed since I checked empty($node->nid). hook_node_insert() fires after the node has already been saved, so if you want webform to do the saving for you, it has to be in hook_node_presave(). You can set the email text by changing the "template" portion. Using the special word "default" will use the default webform template, as produced by the theme system and webform-mail.tpl.php. The same principle applies to from_name and from_address. – Nate Lampton Apr 16 '12 at 15:39
  • I would like to add that you might want to make sure that when you're doing this on hook_node_insert you need to make sure that your module is on a higher weight then the webform module, since it'll delete your e-mails elsewise. Hope this helps some others out, been at this for hours. – Ambidex Sep 12 '14 at 13:47
-1

Yes, you can.

You go to your webform node, click on "webform" then "email's", you should set up one if you don't have one, and on "E-mail to address" you can write your default email. You can even set up the subject and the body. You can print the whole email through %email_values token value, or field by field with label using %email[yourfield] or without label using %value[yourfield].

I'm editing my answer:

You can set a defaul email recipient you have three choices:

  1. You create an email field with a defaul value (your email) and the user can change that value;
  2. create an email field with your email such as default value and click on "disable" if you don't want the user changes the email but he can still see it;
  3. create a email field with your email such as default and click on "hidden", the user won't be able to see and change that email;

I hope this time will be useful :D

  • The OP is asking if you can automatically have the e-mail to address populated. This doesn't answer his question. – Chapabu Apr 3 '12 at 10:55
  • I need the e-mail automatically added when I create the node. – chefnelone Apr 3 '12 at 10:55
  • Sorry, I've edited my answer. – Tyler Durden Apr 3 '12 at 11:09
  • This still isn't a sufficient answer. OP wants e-mail recipients automatically added at time of creation. He doesn't want to have to click through to Add an e-mail recipient every time. I've taken a look at using hook_node_insert, but it doesn't seem to save the webform data at that point. Might be worth checking out hook_node_update. – Chapabu Apr 3 '12 at 11:28
  • what about rules, can this be done with a rule? – chefnelone Apr 3 '12 at 19:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.