Hi all!
I am designing a simpe Email contact form and need a bit of help.
I used the wizard to design the form. The fields are: a subject TextBox, a FileUpload to allow a file to be attached with the Email, a TextArea for the Email message, a CheckBox for carbon copy, a Captcha and a Submit button.
How is the following done?
1. Only logged in users should be able to use the form.
2. When the CheckBox is checked, a copy should be sent to the logged in users email address, which should be pulled from the database.
3. The email TO address should be pulled from the database from a user with a certain id-number.
4. The file (preferably more than one, but one is better than none) should be attached to the Email. As i understand it, this file is uploaded to a directory on the the server, but who deletes it when the Email has been sent?
5. What should be in the Email Container?
I am completely new to ChronoForms and quite new to Joomla. I am using latest ChronoForms and Joomla 1.5.7.
/Michel
I am designing a simpe Email contact form and need a bit of help.
I used the wizard to design the form. The fields are: a subject TextBox, a FileUpload to allow a file to be attached with the Email, a TextArea for the Email message, a CheckBox for carbon copy, a Captcha and a Submit button.
How is the following done?
1. Only logged in users should be able to use the form.
2. When the CheckBox is checked, a copy should be sent to the logged in users email address, which should be pulled from the database.
3. The email TO address should be pulled from the database from a user with a certain id-number.
4. The file (preferably more than one, but one is better than none) should be attached to the Email. As i understand it, this file is uploaded to a directory on the the server, but who deletes it when the Email has been sent?
5. What should be in the Email Container?
I am completely new to ChronoForms and quite new to Joomla. I am using latest ChronoForms and Joomla 1.5.7.
/Michel
Hi Michel,
1) You need to check the user object at the beginning of your form html and redirect the user if they aren't registered i.e. the user id = 0. I posted how to find the user infor in the last few hours so please check my recent posts.
2) You'll need a bit of PHP code in the InSubmit Before box to see if the field is check and recover the user email if it is. You should put a hidden 'dummy' input in your form with the name 'user_email' to hold this info.
3) See 2 but presumably looking up a different user, take care to keep the two blocks of info separate.
4) The file(s) will be automatically attached but you'll need to delete them. You could automate this in the OnSubmit After box or do it manually every so often.
5) Dynamic To, Dynamic CC, Subject, From Name, From Email. The dynamic fields take field-names so use the dummy fields you created above.
Bob
1) You need to check the user object at the beginning of your form html and redirect the user if they aren't registered i.e. the user id = 0. I posted how to find the user infor in the last few hours so please check my recent posts.
2) You'll need a bit of PHP code in the InSubmit Before box to see if the field is check and recover the user email if it is. You should put a hidden 'dummy' input in your form with the name 'user_email' to hold this info.
3) See 2 but presumably looking up a different user, take care to keep the two blocks of info separate.
4) The file(s) will be automatically attached but you'll need to delete them. You could automate this in the OnSubmit After box or do it manually every so often.
5) Dynamic To, Dynamic CC, Subject, From Name, From Email. The dynamic fields take field-names so use the dummy fields you created above.
Bob
Hi Bob, thank for your quick reply!
I am working on 2, 3 and 5. And i think i can solve 1. If i get into problems, you will hear from me.
But with number 4, deleting the uploaded files, i will need a little more of your assistance. Any manual procedure is out of the question (a computer is supposed to be a labor saving device, you know!). So, how do i keep track of the names of the attached files and delete the files after submit? Any ideas?
/Michael
I am working on 2, 3 and 5. And i think i can solve 1. If i get into problems, you will hear from me.
But with number 4, deleting the uploaded files, i will need a little more of your assistance. Any manual procedure is out of the question (a computer is supposed to be a labor saving device, you know!). So, how do i keep track of the names of the attached files and delete the files after submit? Any ideas?
/Michael
Hi Michael,
The deletion is straightforward, you just do
I think I'd use the same code but only delete files that were more than say two weeks old. That will stop the folder getting overfull but give you a little bit of a backstop. Here's a function from the PHP manual notes that deletes files more than X seconds old, needs some adapting for Joomal/ChronoForms but the baiscs are good:
Bob
The deletion is straightforward, you just do
<?php
unlink("file_path/file_name");
?>
The question is when to do it. If you delete when the email is sent then sure as eggs is eggs something will go amiss and you'll want to find the file again.I think I'd use the same code but only delete files that were more than say two weeks old. That will stop the folder getting overfull but give you a little bit of a backstop. Here's a function from the PHP manual notes that deletes files more than X seconds old, needs some adapting for Joomal/ChronoForms but the baiscs are good:
function cleantmp() {
$seconds_old = 3600;
$directory = "/var/tmp";
if( !$dirhandle = @opendir($directory) )
return;
while( false !== ($filename = readdir($dirhandle)) ) {
if( $filename != "." && $filename != ".." ) {
$filename = $directory. "/". $filename;
if( @filemtime($filename) < (time()-$seconds_old) )
@unlink($filename);
}
}
}
Bob
Great Bob!
I will probably be able to use someting like that.
Just let me check one thing. Why would i want to keep the files on the server after the email has been sent? Doesn't the files get attached to the email message in the same way as if it had been sent from for instance MS Outlook? I really hope the attached file is embedded in the email message so that it then can be extacted from the recieved email data by outlook. Can you confirm this? If this is the case, it should be safe to delete any file in the upload directory that is older than say 30 seconds.
Michael
I will probably be able to use someting like that.
Just let me check one thing. Why would i want to keep the files on the server after the email has been sent? Doesn't the files get attached to the email message in the same way as if it had been sent from for instance MS Outlook? I really hope the attached file is embedded in the email message so that it then can be extacted from the recieved email data by outlook. Can you confirm this? If this is the case, it should be safe to delete any file in the upload directory that is older than say 30 seconds.
Michael
Hi Michael,
Yes, the file will be embedded and sent and all is then OK . . . as long as the email arrives. We see here too often that they get accidentally deleted, diverted to spam folder or just plain lost. If that happens and the file has been deleted then there may be no way back. If this isn't a problem then deleting them is fine, if it is then a few extra lines of code could save a problem.
Bob
Yes, the file will be embedded and sent and all is then OK . . . as long as the email arrives. We see here too often that they get accidentally deleted, diverted to spam folder or just plain lost. If that happens and the file has been deleted then there may be no way back. If this isn't a problem then deleting them is fine, if it is then a few extra lines of code could save a problem.
Bob
Hi Bob,
I can't make it work. The data i try to pull from the database obviously never reaches the dynamic fields. What am i doing wrong?
This is the debug output after submit:

This is the Email Container:

This is the form HTML:
This is the on Submit code - before sending email:
I am stuck! Is there any way to print debug output? How do you debug php? I am a C-programmer, but a total newbie when it comes to all this web stuff. I would like to set a breakpoint and single step throu the code while inspecting varaiables, but i guess i can forget that.
Michel
I can't make it work. The data i try to pull from the database obviously never reaches the dynamic fields. What am i doing wrong?
This is the debug output after submit:

This is the Email Container:

This is the form HTML:
<div class="form_item">
<div class="form_element cf_heading">
<h1 id="" class="cf_text">Contact The Author</h1>
</div>
<div class="clear"> </div>
</div>
<div class="form_item">
<div class="form_element cf_text"><span class="cf_text">Explain that not all E-mails can be answered.</span></div>
<div class="clear"> </div>
</div>
<div class="form_item">
<div class="form_element cf_textbox">
<label class="cf_label">Subject:</label>
<input class="cf_inputbox required validate-alphanum" maxlength="150" size="30" id="subject" name="subject" type="text">
<a onclick="return false;" class="tooltiplink">
<img src="components/com_chronocontact/css/images/tooltip.png" class="tooltipimg" width="16" border="0" height="16">
</a>
<div class="tooltipdiv">Subject: :: Please enter the subject here</div>
</div>
<div class="clear"> </div>
</div>
<div class="form_item">
<div class="form_element cf_fileupload">
<label class="cf_label">Attachments:</label>
<input class="cf_inputbox" size="20" id="attachment" name="attachment" type="file">
<a onclick="return false;" class="tooltiplink">
<img src="components/com_chronocontact/css/images/tooltip.png" class="tooltipimg" width="16" border="0" height="16">
</a>
<div class="tooltipdiv">Attachments: :: Choose files to attach with the Email (only files of type .pdf .doc or .zip, max size 250KB)</div>
</div>
<div class="clear"> </div>
</div>
<div class="form_item">
<div class="form_element cf_textarea">
<label class="cf_label">Message:</label>
<textarea class="cf_inputbox required" rows="10" id="message" cols="70" name="message"></textarea>
<a onclick="return false;" class="tooltiplink">
<img src="components/com_chronocontact/css/images/tooltip.png" class="tooltipimg" width="16" border="0" height="16">
</a>
<div class="tooltipdiv">Message: :: Please enter the message here</div>
</div>
<div class="clear"> </div>
</div>
<div class="form_item">
<div class="form_element cf_checkbox">
<label class="cf_label">Send me a copy:</label>
<div class="float_left">
<input value="check 1" class="radio" id="check_1" name="check_1" type="checkbox">
<label for="check_1" class="check_label">Send me a copy</label>
<br>
</div>
<a onclick="return false;" class="tooltiplink">
<img src="components/com_chronocontact/css/images/tooltip.png" class="tooltipimg" width="16" border="0" height="16">
</a>
<div class="tooltipdiv">Send me a copy: :: Check if you want a copy sent to your Email address</div>
</div>
<div class="clear"> </div>
</div>
<div class="form_item">
<div class="form_element cf_captcha">
<label class="cf_label">Security code:</label><span>{imageverification}</span>
<a onclick="return false;" class="tooltiplink">
<img src="components/com_chronocontact/css/images/tooltip.png" class="tooltipimg" width="16" border="0" height="16">
</a>
<div class="tooltipdiv">Security code: :: Enter security code from image. If no image is present then try disabling advertisment blocker and then refresh this page.</div>
</div>
<div class="clear"> </div>
</div>
<div class="form_item">
<div class="form_element cf_button">
<input value="Submit" name="undefined" type="submit">
<a onclick="return false;" class="tooltiplink">
<img src="components/com_chronocontact/css/images/tooltip.png" class="tooltipimg" width="16" border="0" height="16">
</a>
<div class="tooltipdiv">Submit :: Send this Email message</div>
</div>
<div class="clear"> </div>
</div>
This is the on Submit code - before sending email:
<?php $user = JFactory::getUser(); ?>
<input type="hidden" name="fromadr" value="<?php echo $user->email; ?>" />
<input type="hidden" name="fromname" value="<?php echo $user->name; ?>" />
<?php
if ($_POST['check_1'] ) {
echo '<input type="hidden" name="ccadr" value="'.$user->email.'" />';
}
else {
echo '<input type="hidden" name="ccadr" value="" />';
}
?>
<?php
$database = &JFactory::getDBO();
$query = "SELECT email FROM jos_users WHERE user_id = '62'";
$database->setQuery( $query );
$data = $database->loadObject();
echo '<input type="hidden" name="toadr" value="'.$data->email.'" />';
?>
I am stuck! Is there any way to print debug output? How do you debug php? I am a C-programmer, but a total newbie when it comes to all this web stuff. I would like to set a breakpoint and single step throu the code while inspecting varaiables, but i guess i can forget that.
Michel
Hi Michel,
it will not work the way you do it, try this on the submit before email:
Regards
Max
it will not work the way you do it, try this on the submit before email:
JRequest::setVar('toadr','someemail@email.com');
Regards
Max
Hi Max, Bob
I can now send mails. But the copy to the sender does not work. I now set the TO address depending on the checkbox for a copy.
If checkbox is unchecked, the TO address is the admin email pulled from the database. This works ok!
If the checkbox is checked, the TO address is a concatenated string consisting of the admin email and the sender email with the following format: "admin@domain.com,sender@otherdomain.com" (without the "). This does not work. PHP_MAILER failes with this error: "PHPMAILER_RECIPIENTS_FAILEDadmin@domain.com,sender@otherdomain.com".
What is wrong? I have copied this method from another post where it worked: http://www.chronoengine.com/forums.html?cont=posts&f=5&t=11436&p=16322&hilit=email+copy&sid=f25fe0dbdf054ba2e90b016ad8b3cebb#p16322
Do i have to use the $mails[0] array? I would guess not since the To address is passed to PHP_MAILER as intended.
This is the HTML now:
This is the Submit before code now:
Michael
I can now send mails. But the copy to the sender does not work. I now set the TO address depending on the checkbox for a copy.
If checkbox is unchecked, the TO address is the admin email pulled from the database. This works ok!
If the checkbox is checked, the TO address is a concatenated string consisting of the admin email and the sender email with the following format: "admin@domain.com,sender@otherdomain.com" (without the "). This does not work. PHP_MAILER failes with this error: "PHPMAILER_RECIPIENTS_FAILEDadmin@domain.com,sender@otherdomain.com".
What is wrong? I have copied this method from another post where it worked: http://www.chronoengine.com/forums.html?cont=posts&f=5&t=11436&p=16322&hilit=email+copy&sid=f25fe0dbdf054ba2e90b016ad8b3cebb#p16322
Do i have to use the $mails[0] array? I would guess not since the To address is passed to PHP_MAILER as intended.
This is the HTML now:
<div class="form_item">
<div class="form_element cf_heading">
<h1 id="" class="cf_text">Contact The Author</h1>
</div>
<div class="clear"> </div>
</div>
<div class="form_item">
<div class="form_element cf_text"><span class="cf_text">Explain that not all E-mails can be answered.</span></div>
<div class="clear"> </div>
</div>
<div class="form_item">
<div class="form_element cf_textbox">
<label class="cf_label">Subject:</label>
<input class="cf_inputbox required validate-alphanum" maxlength="150" size="30" id="subject" name="subject" type="text">
<a onclick="return false;" class="tooltiplink">
<img src="components/com_chronocontact/css/images/tooltip.png" class="tooltipimg" width="16" border="0" height="16">
</a>
<div class="tooltipdiv">Subject: :: Please enter the subject here</div>
</div>
<div class="clear"> </div>
</div>
<div class="form_item">
<div class="form_element cf_fileupload">
<label class="cf_label">Attachments:</label>
<input class="cf_inputbox" size="20" id="attachment" name="attachment" type="file">
<a onclick="return false;" class="tooltiplink">
<img src="components/com_chronocontact/css/images/tooltip.png" class="tooltipimg" width="16" border="0" height="16">
</a>
<div class="tooltipdiv">Attachments: :: Choose files to attach with the Email (only files of type .pdf .doc or .zip, max size 250KB)</div>
</div>
<div class="clear"> </div>
</div>
<div class="form_item">
<div class="form_element cf_textarea">
<label class="cf_label">Message:</label>
<textarea class="cf_inputbox required" rows="10" id="message" cols="70" name="message"></textarea>
<a onclick="return false;" class="tooltiplink">
<img src="components/com_chronocontact/css/images/tooltip.png" class="tooltipimg" width="16" border="0" height="16">
</a>
<div class="tooltipdiv">Message: :: Please enter the message here</div>
</div>
<div class="clear"> </div>
</div>
<div class="form_item">
<div class="form_element cf_checkbox">
<label class="cf_label">Send me a copy:</label>
<div class="float_left">
<input value="check 1" class="radio" id="check_1" name="check_1" type="checkbox">
<label for="check_1" class="check_label">Send me a copy</label>
<br>
</div>
<a onclick="return false;" class="tooltiplink">
<img src="components/com_chronocontact/css/images/tooltip.png" class="tooltipimg" width="16" border="0" height="16">
</a>
<div class="tooltipdiv">Send me a copy: :: Check if you want a copy sent to your Email address</div>
</div>
<div class="clear"> </div>
</div>
<div class="form_item">
<div class="form_element cf_captcha">
<label class="cf_label">Security code:</label><span>{imageverification}</span>
<a onclick="return false;" class="tooltiplink">
<img src="components/com_chronocontact/css/images/tooltip.png" class="tooltipimg" width="16" border="0" height="16">
</a>
<div class="tooltipdiv">Security code: :: Enter security code from image. If no image is present then try disabling advertisment blocker and then refresh this page.</div>
</div>
<div class="clear"> </div>
</div>
<div class="form_item">
<div class="form_element cf_button">
<input value="Submit" name="undefined" type="submit">
<a onclick="return false;" class="tooltiplink">
<img src="components/com_chronocontact/css/images/tooltip.png" class="tooltipimg" width="16" border="0" height="16">
</a>
<div class="tooltipdiv">Submit :: Send this Email message</div>
</div>
<div class="clear"> </div>
</div>
<div>
<input type="hidden" name="fromadr" value="" />
<input type="hidden" name="fromname" value="" />
<input type="hidden" name="toadr" value="" />
</div>
This is the Submit before code now:
<?php
// get the logged in users (the message sender) email and name
$user = JFactory::getUser();
JRequest::setVar('fromadr',$user->email);
JRequest::setVar('fromname',$user->name);
// Get admin email from database
$database = &JFactory::getDBO();
$query = "SELECT * FROM jos_users WHERE id = '62'";
$database->setQuery( $query );
$data = $database->loadObject();
// set toadr depending on checkbox for copy to sender
if (isset($_POST['check_1'])) {
JRequest::setVar('toadr',$data->email.','.$user->email);
} else {
JRequest::setVar('toadr',$data->email);
}
?>
Michael
I think because you can't send from the user email to the user email too, you need to have 2 different emails or change the from email to be something completely different to the admin and user emails, makes sense ?
Max
I have allready tried that. I changed the the submit after code so that the TO address became "admin@domain.com,xxsender@otherdomain.com", see the xx. While the FROM address remaind "sender@otherdomain.com". No change.
I am assuming thatr the email should be sent, even if the address "xxsender@otherdomain.com" in reallity doesn't exist.
No, something else is the matter, but WHAT?
Michel
I have allready tried that. I changed the the submit after code so that the TO address became "admin@domain.com,xxsender@otherdomain.com", see the xx. While the FROM address remaind "sender@otherdomain.com". No change.
I am assuming thatr the email should be sent, even if the address "xxsender@otherdomain.com" in reallity doesn't exist.
No, something else is the matter, but WHAT?
Michel
Hi Max, Bob
Some way (don't ask me how) it has started working, at least so so. I can now send emails to admin and a cc to the sender. But when the checkbock isn't checked no copy should be sent. The ccadr therefore has to be a null-string. The problem now is to clear the ccadr field.
I have tried with:
But that causes an error in php_mailer saying: "SMTP Error! The following recipients failed:" nothing else. It seems as if the ccadr field is not set to a null string, which i guess i should be. How do i set the field to a null string (if that is what it should be)?
Michael
Some way (don't ask me how) it has started working, at least so so. I can now send emails to admin and a cc to the sender. But when the checkbock isn't checked no copy should be sent. The ccadr therefore has to be a null-string. The problem now is to clear the ccadr field.
I have tried with:
JRequest::setVar('ccadr','');
But that causes an error in php_mailer saying: "SMTP Error! The following recipients failed:" nothing else. It seems as if the ccadr field is not set to a null string, which i guess i should be. How do i set the field to a null string (if that is what it should be)?
Michael
Hi, try :
JRequest::setVar('ccadr', NULL);
?
was this ever resolved? i'm looking to do the same or similar.
Thanxs
Tim
Thanxs
Tim
Hi Tim, as you can see, we are doing the final fix, may be jmhook can give you some ideas!
Regards
Max
Regards
Max
Hi Tim, Max, others...
I am slowly getting there. Will post the solution when done in a few days.
In the mean time, this software is seriously lacking documentation. Where is the "advanced practices" manual????
/Michael
I am slowly getting there. Will post the solution when done in a few days.
In the mean time, this software is seriously lacking documentation. Where is the "advanced practices" manual????
/Michael
Hi Michael,
We try to show any code examples or ideas here at the forums, doing a quick search may get many results you need, We can't know what everybody needs and there are too many possibilities, but overall there may be few docs yes, I'm planning to spend some time to make some advanced docs but after the coming release because there will be some advanced new features already!
Regards,
Max
We try to show any code examples or ideas here at the forums, doing a quick search may get many results you need, We can't know what everybody needs and there are too many possibilities, but overall there may be few docs yes, I'm planning to spend some time to make some advanced docs but after the coming release because there will be some advanced new features already!
Regards,
Max
This topic is locked and no more replies can be posted.