I made a simple email form to send an attachment (image). All sender information is taken from the joomla database, the user selects from the dropdown list (populated from database) new name of image.
- In lines 1-50, I created all the variables from the joomala base
- In lines 50-56 I have tested variables with echo and everything works
In email form after line 58 variables $alias and $sender does not work (get empty space instead of value in email message)
<?php // $link_id $link_id = JRequest::getInt('link_id'); // $sender $db = JFactory::getDbo(); $query1 = $db->getQuery(true); $query1->select('link_name'); $query1->from($db->quoteName('#__hikers')); $query1->where($db->quoteName('#__hikers.link_id')." = ".$db->quote($link_id)); $db->setQuery($query1); $sender = $db->loadResult(); // $alias $db = JFactory::getDbo(); $querya = $db->getQuery(true); $querya->select('#__hikers.alias'); $querya->from($db->quoteName('#__hikers')); $querya->where($db->quoteName('#__hikers.link_id')." = ".$db->quote($link_id)); $db->setQuery($querya); $alias = $db->loadResult(); // $sender_email $db = JFactory::getDbo(); $query3 = $db->getQuery(true); $query3->select('#__users.email'); $query3->from($db->quoteName('#__users')); $query3->where($db->quoteName('#__users.id')." = ".$db->quote($userId)); $db->setQuery($query3); $sender_email = $db->loadResult(); // Dropdown menu from database $db = JFactory::getDbo(); $squery = $db->getQuery(true); $squery->select('peak_name'); $squery->from($db->qn('#__unclimbedpeaks')); $squery->where($db->quoteName('#__unclimbedpeaks.link_id')." = ".$db->quote($link_id)); $squery->order('peak_id ASC'); $db->setQuery($squery); $speaks_list = $db->loadColumn(); $speaks_select = '<select name="name" id="peaks">'; $speaks_select .= '<option value="">-- Odaberi vrh --</option>'; foreach($speaks_list as $p){ $speaks_select .= '<option value="' . $p . '">' . $p . '</option>'; } $speaks_select .= '</select>'; ?> <?php // Testing $alias, $sender, $sender_email and $link_id echo $alias; echo $sender; echo $sender_email; echo $link_id; // Email form if(isset($_POST['submit2'])) { $svrh_name = $_POST['name']; $output = header('Location: https://mysite/'.$alias); /* Redirect browser */; //Set the form flag to no display (cheap way!) $flags = 'style="display:none;"'; //Deal with the email $to = 'admin@MySite.com'; $subject = 'Message from '.$sender; $message = 'This is message with '.$svrh_name.' from '.$sender.'. Please visit link: https://MySite/'.$alias.' Sender email:'.$sender_email.' Sender ID:'.$link_id.''; $attachment = chunk_split(base64_encode(file_get_contents($_FILES['file']['tmp_name']))); $filename = $svrh_name.'.jpg'; $boundary =md5(date('r', time())); $headers = 'From: '.$sender_email.' \r\nReply-To: '.$sender_email.''; $headers .= "\r\nMIME-Version: 1.0\r\nContent-Type: multipart/mixed; boundary=\"_1_$boundary\""; $message="This is a multi-part message in MIME format. --_1_$boundary Content-Type: multipart/alternative; boundary=\"_2_$boundary\" --_2_$boundary Content-Type: text/plain; charset=\"iso-8859-1\" Content-Transfer-Encoding: 7bit $message --_2_$boundary-- --_1_$boundary Content-Type: application/octet-stream; name=\"$filename\" Content-Transfer-Encoding: base64 Content-Disposition: attachment $attachment --_1_$boundary--"; mail($to, $subject, $message, $headers); } ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>MailFile</title> </head> <body> <?php echo $output; ?> <form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF'];?>" method="post" <?php echo $flags;?>> <?php echo $speaks_select; ?> <p><label for="file">File</label> <input type="file" name="file" id="file"></p> <p><input type="submit" name="submit2" id="submit2" value="Send"></p> </form> </body> </html>
JRequest
is deprecated. Please use JInput$output = header('Location: https://mysite/'.$alias); /* Redirect browser */;
header()
doesn't offer a return value. Are you trying to exit the script prematurely and load the new page?$alias
and$sender
don't work, is that to mean that the other variables DO contain their expected values? Have you implemented Lodder's advice to use Joomla's mailing technique? You should try to reduce your total calls to the database by 1. merging the$sender
and$alias
queries and 2. only query for$speaks_select
when you are building your form. You should not be usingdisplay:none
as a short cut, you should be using anif/else
block to isolate pre-submission and post-submission processes.?link_id=[integer]
? What happens when you submit (POST
) your form? Your$_GET['link_id']
data is lost isn't it?$_SERVER['PHP_SELF']
is delivering the script, but not your querystring. I think you should be retaining thelink_id
during form submission by writing a hidden input field.