How to embed the EMAIL CONTACT FORM
| Project: | Email Field |
| Version: | 5.x-1.x-dev |
| Component: | Code |
| Category: | support request |
| Priority: | normal |
| Assigned: | Unassigned |
| Status: | needs review |
Jump to:
Hi folks,
My ultimate question is how to use the email module to embed the email contact form, but I am also curious as to why the form isn't being rendered in my node when I use this code:
<?php print drupal_get_form('email_mail_page_form'); ?>I am trying to get the email contact form generated by this module into the node itself. I looked through the module and I narrowed it down to two functions that coudl help me get this accomplished:
function email_mail_page_form()
and/or
function email_mail_page($nid=null, $fieldname=null)
I've used:
<?php print drupal_get_form('email_mail_page_form'); ?>This does not generate anything on the page. :( I expected this to at least render the form created by this code in the email.module file:
function email_mail_page_form() {
global $user;
if ($user->uid) {
$edit['name'] = $user->name;
$edit['mail'] = $user->mail;
}
$form['#token'] = $user->name . $user->mail;
$form['name'] = array('#type' => 'textfield',
'#title' => t('Your name'),
'#maxlength' => 255,
'#default_value' => $edit['name'],
'#required' => TRUE,
);
$form['mail'] = array('#type' => 'textfield',
'#title' => t('Your e-mail address'),
'#maxlength' => 255,
'#default_value' => $edit['mail'],
'#required' => TRUE,
);
$form['subject'] = array('#type' => 'textfield',
'#title' => t('Subject'),
'#maxlength' => 255,
'#required' => TRUE,
);
$form['message'] = array('#type' => 'textarea',
'#title' => t('Message'),
'#required' => TRUE,
);
$form['submit'] = array('#type' => 'submit',
'#value' => t('Send e-mail'),
);
return $form;
}I then tried:
<?php print drupal_get_form('email_mail_page', $node->nid, $node->field_business_email); ?>This returns a Drupal 404 error I suspect due to this code in the email.module file:
if (empty($nid) || empty($fieldname)) {
drupal_not_found();
return;
}
$node = node_load(intval($nid));
if (!$node) {
drupal_not_found();
return;
}
// Validate field name
$types = content_types($node->type);
if (!isset($types['fields'][$fieldname]) ||
$types['fields'][$fieldname]['type'] != 'email' ||
$types['fields'][$fieldname]['widget']['link_type'] != 'form') {
drupal_not_found();
return;
}
$field = $node->$fieldname;
if (empty($field) || empty($field[0]['email'])) {
drupal_not_found();
return;
}My ultimate question is how to use the email module to embed the email contact form, but I am also curious as to why the form isn't being rendered in my node when I use this code:
<?php print drupal_get_form('email_mail_page_form'); ?>Thanks kindly for reading.

#1
Hi there,
When I invoke
<?phpprint drupal_get_form('email_mail_page_form');
?>
<?phpprint drupal_get_form('email_mail_page', $node->nid, $node->field_business_email);
?>
Cheers
#2
Where exactly do you embed this code (which file?):
<?phpprint drupal_get_form('email_mail_page_form');
?>
Thanks
#3
Hi all,
Very interesting question!!
I´m new in drupal and I would like also to know how to embed the email form, and some extra thing.. how to fill up the subject field with the title of the node where it is embed? (sorry about my english).
I´m using drupal 6.2 but I think it should be pretty similar to 5.x.
Thanks in advance.
#4
Were you able to find the answer to your question i.e. how to fill up the subject field with the title of the node? I am trying to do the same thing but unable.
#5
+1
#6
Any help on the above request please?
#7
Try this approach http://drupal.org/node/335020
its for drupal 6 but should not take much to change for drupal 5
#8
[#1] doesn't work for me. I always got "Page not found". I tried through block and contemplate. I don't think there is any difference.
#9
I couldn't figure it out and just ended up using a web form to email service:
http://www.formmailhosting.com
Good part is they send you the email of the form results (of course) but they also store the submissions in MS Excel which is a nice feature too.
- Mark
#10
I sort of solved it. But through such ugly hack (it uses Iframe and hacks Email Field module directly), that I probably shouldn't recommend it to anyone. So I will describe it only briefly. Reply if you're interested.
Of course I am still interested in better solutions.
#11
The majority of code on this support request still applies to Drupal 5.x
thus changing the version to 5.x otherwise it would be confusing to the people that see this issue in the future
#12
This one threw me for a bit, but this is how I solved the problem without any hacking, please note this is for D6.
Because
email_mail_page()is not a form function a but normal menu call back. In this function adrupal_get_formgets called onemail_mail_page_form()But
email_mail_page()does a bunch of stuff including checking for flood thresholds.So I could call
drupal_get_formonemail_mail_page_formdirectly but instead I used the code fromemail_mail_page()and modified it to keep the extra checking.Here is the steps I took.
I created content type called
contactand added an email field calledfield_contact_email.I then created a template file for my new content type called
node-contact.tpl.phpand saved it in my theme.I copied the contents of
node.tpl.phpinto mynode-contact.tpl.php.At the end of my new template file I added the following code.
<?php
if ($page) {
// fallback if email module is every disabled
if (module_exists('email')) {
// Setup required variables
$field_name = 'field_contact_email';
$show_form = TRUE;
// Taken from email_mail_page(), but modified.
if (module_exists('content_permissions')) {
if (!user_access('view '. $field_name)) {
$show_form = FALSE;
}
}
$field = $node->$field_name;
$email = $field[0]['email'];
$types = content_types($node->type);
// Validate field name
$types = content_types($node->type);
if (empty($email) ||
!isset($types['fields'][$field_name]) ||
$types['fields'][$field_name]['type'] != 'email' ||
($types['fields'][$field_name]['display_settings']['teaser']['format'] != 'contact' &&
$types['fields'][$field_name]['display_settings']['full']['format'] != 'contact')) {
$show_form = FALSE;
}
if ($show_form) {
if (!flood_is_allowed('email', variable_get('email_hourly_threshold', 3))) {
print t("You cannot send more than %number messages per hour. Please try again later.", array('%number' => variable_get('email_hourly_threshold', 3)));
}
else {
print drupal_get_form('email_mail_page_form', $node, $field_name, $email);
}
}
}
// Change the page title
drupal_set_title(t('Contact: !title', array('!title' => $node->title)));
}
?>
Hope this helps other people.
#13
Hi,
On which function you added this php-code in template.php please? I have the acquia_slate theme and am not seeing were to add this.
Looking for same sort solution on D6. Can you also add fields to the contactform?
Thanks for your reply in advance!
Greetings, Martijn
#14
Sorry I was not clear, you add it to your new tpl.php file. Just remember that I called everything
contactand you will need to change that in the code and file names.EG:
<?php$field_name = 'field_contact_email';
?>
#15
Hi, will look into it more!
Do you also know how to add a field to the form, say the url of the contact?
greetings,
Martijn
#16
Interesting. I've been looking at doing this as well, and I will probably try out the approach in #12. In looking at the code, although it's not a "hack" per se, it does look like it is copying some basic code from the email contact form module, is that true? If so, might there be some issues maintaining this to stay in sync with updates to that module?