HTML site with AJAX request image onclick execute PHP file - php

I will really appreciate if you could help me in that coding. I really need help.
I want with single press over the image, to get the data from fields sent over email.
So my HTML image code like that :
<img src="images/some.png" id="help" onClick="help();">
Right before that tag in HTML file I put AJAX like that:
<script type="text/javascript">
function help() {
$.get("/help.php");
return false;
}
</script>
And with that function i want, when the user click over the image to execute my PHP script which is on other file and it contains code like that :
help.php
<?php
{
$helpSubject = 'help';
$webMaster = '****#mail.com';
$helpField = $_POST['help'];
$problemField = $_POST['problem'];
$body = <<<EOD
<br><hr><br>
Help: $help <br>
Problem: $problem <br>
EOD;
$headers = "Reason: $help\r\n";
$headers = "Content-type: text/html\r\n";
$success = mail($webMaster, $helpSubject, $body, $headers);
$theResults = <<<EOD
Success
EOD;
echo "$theResults";
}
?>
Something is wrong, and I can't find what. Could anyone help me with that?
Thank you.

Related

How can i echo input data fields in an file_get_contents html template?

I have this send_mail.php file. I'm trying to echo some fields that are inserted in a form and i can't with echo function. What am I doing wrong?
<?php
$date = $_POST['date'];
$full_name = $_POST['full_name'];
$biz_name = $_POST['biz_name'];
$activity = $_POST['activity'];
$afm = $_POST['afm'];
$phone = $_POST['phone'];
$fax = $_POST['fax'];
$mobile = $_POST['mobile'];
$address = $_POST['address'];
$city = $_POST['website'];
$email = $_POST['email'];
$discount = $_POST['discount'];
$payment_amount = $_POST['payment_amount'];
$seller_name = $_POST['seller_name'];
$to = $email;
$subject = "Welcome to our site!";
$htmlContent = file_get_contents("email_template.html");
// Set content-type header for sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers = "Content-type: text/html; charset=UTF-8" . "\r\n";
// Send email
if(mail($to,$subject,$htmlContent,$headers)):
$successMsg = 'Mail sending successful.';
else:
$errorMsg = 'Oops! Something went wrong.';
endif;
?>
And my email_template.html looks like this:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Welcome to our site</title>
<head/>
<body>
<h1> Welcome <?php echo $full_name; ?> </h1>
...
</body>
</html>
I'm new in web development so be gentle! :P
file_get_contents() doesn't parse the file as php. So one option would be to do something like this:
ob_start();
include('email_template.html');
$htmlContent = ob_get_contents();
ob_end_clean();
When you use include() it assumes you are running php code. Then we use an output buffer to capture the contents.
This way all variables are still available, otherwise you need to setup a whole template engine, in that case I would use an existing one like Twig.
Edit template and mark variables by symbols or special names than replace real vars:
email_template.html:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Welcome to our site</title>
<head/>
<body>
<h1> Welcome --full_name-- </h1>
...
</body>
</html>
php :
<?php
$htmlContent = file_get_contents("email_template.html");
$htmlContent = str_replace("--full_name--",$_POST['full_name'],$htmlContent);
I´ve just learned how to answer like simple text, my first answer was totally "coded".
Well, i suggest you learn codeigniter, it´s not the best for sure, but it´s the easiest on my view, and nowadays the most famous is laravel.
Nothing is easy for beginners anyway, but before starting studing any framework, look for understand php and php oo first.
See´ya! :)
How are both files connected? Are you using ajax?
Look, if you´re using ajax, just return $full_name, and print it with jquery or javascript. (I believe you didn't this, but if you do...)
Otherwise, if you just posted and redirected to send_mail.php, just use echo on this same page, just like:
// Send email
if (mail($to,$subject,$htmlContent,$headers)):
echo = 'Mail sending successful.';
else :
echo = 'Oops! Something went wrong.';
endif ;
A third way I can suggest to you, is using $_SESSION.
On send_mail.php start a session with:
session_start();
//and declare
$_SESSION["full_name "]= $fullname;
Then you'll have this variable enabled for any page, while this user is on your site, and you just have to echo this way
echo $_SESSION['full_name'];
When you feel a little improved, I suggest you learn a php framework, that helps a lot. Good luck!

Moodle email templates

I am working on a Learning Management System build upon Moodle. I want to add an email header and footer for each email.
I did some change in Moodle for adding an image in ./lib/moodlelib.php as follows:
function email_to_user($user, $from, $subject, $messagetext, $messagehtml = '', $attachment = '', $attachname = '',
$usetrueaddress = true, $replyto = '', $replytoname = '', $wordwrapwidth = 79) {
global $CFG, $PAGE, $SITE;
// Append image at top
if($messagehtml){
$tmp = $messagehtml;
// Added image here
$messagehtml = '<img src="'.$CFG->wwwroot.'/images/logo.png" alt="LMS" /><br/>';
// $messagehtml = $image;
$messagehtml .= $tmp;
}
if($messagetext){
$tmp = $messagetext;
// Added image here
$messagetext = '<img src="'.$CFG->wwwroot.'/images/logo.png" alt="LMS" /><br/>';
// $messagehtml = $image;
$messagetext .= $tmp;
}
....................
but I want the header and footer as fixed templates. Please help me.
You could create a message header in a language file (in English either directly under /lang/en or in a plugin) and then add the following string in the language file:
$string ['message_header'] = '<p> write here whatever your style and HTML structure you want in your header</p>
adding your image as well <img src="'.$CFG->wwwroot.'/images/logo.png" alt="LMS" />';
Then you could write a string for your footer as well:
$string ['message_footer'] = 'Your HTML footer here';
And finally you could insert your strings in the message:
$message = 'here your body';
// insert message header - if your language string is in /lang/moodle.php:
$message = get_string ( 'message_header') . $message;
// insert message footer - if your language string is in your /local/myplugin:
$message .= get_string ( 'message_footer', 'local_my_plugin' );
This way your header and footer may be customized at will if you change them directly in the language file (or, in the DB. See here).
a little bit too late, but i hope it helps others:
https://docs.moodle.org/dev/Themed_emails just look for e-mail templates in /var/www/html/moodle/lib/templates. The template with a name email_html.mustache should be the right one.

Need stylesheet before ob_endflush() and before header(location: ), How?

My page has the following structure:
<?php
session_start();
ob_start();
?>
HTML HEADER
links to stylesheets global.css and mail.css
<?php
function email(){
/* using the stylesheets global.css and mail.css */
$text = '<div class="test">
<p id="par">...
<span id="write" class="text">...</span>
</p>
</div>';
return $text;
}
?>
/* some text and html stuff */
<?php
if(isset(...)){
/* php form validation */
/* after validating I send an email to the user where I call
the function email() which use the stylesheets. */
$mail_text = 'Dear .....';
$mail_text .= email();
$mail_headers = "From: test#test.com\r\n".
'X-Mailer: PHP/' . phpversion().
'MIME-Version: 1.0' . "\r\n".
'Content-type: text/html; charset=utf-8' . "\r\n";
if(mail(..., ..., $mail_text, $mail_headers)){
header('location: newlocation.php');
}
?>
/* HTML FORMS and text and .... */
<?php
ob_end_flush();
?>
The email is sent with the text coming from the email() function, but without formatting cause the CSS are "printed out" only after ob_end_flush() at the bottom of the page.
How could I solve that? There are many classes and styles inside email(), so writing every times <div style="..."> and so on is not a good solution.
You are confused about output buffering. The html/css you print out at the start of the page isn't being captured anywhere, and will NOT be part of $mail_text. Just because you're usign output buffering doesn't make that buffer magically appear in a variable. You'd need at least $mail_text .= ob_get_clean() or something to extract the buffer's contents and put it into your variable.

How can I send an base64 image on a mail body with PHP?

I'm trying to send an email with an image in base64 on the body with PHP using the code below, but the image never appears... If I change to an URL it works, but it doesn't with the base64... I tested the base64 on a new page only with <img src=base64> and worked too... What am I missing??
<?php
// recipients
$to = $_POST['email'];
// subject
$subject = 'Test';
// message
$message = '
<html>
<head>
<title>Test</title>
</head>
<body>
<img src="'.$_POST['imageFromOtherPage'].'"/>
</body>
</html>
';
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Mail it
mail($to, $subject, $message, $headers);
?>
Here is my base64 image example: http://jsfiddle.net/28nP4/
I tried different things and the only way I found was uploading the image and getting the URL, I got that from this link: http://j-query.blogspot.in/2011/02/save-base64-encoded-canvas-image-to-png.html
It is very simple:
<?php
// requires php5
define('UPLOAD_DIR', 'images/');
$img = $_POST['img'];
$img = str_replace('data:image/png;base64,', '', $img);
$img = str_replace(' ', '+', $img);
$data = base64_decode($img);
$file = UPLOAD_DIR . uniqid() . '.png';
$success = file_put_contents($file, $data);
print $success ? $file : 'Unable to save the file.';
?>
And this generates an URL, so, instead of using <img src="'.$_POST['imageFromOtherPage'].'"/>, I use the generated URL. Worked perfectly!
i have the same problem, and finaly i resolved it. It might be helpful for you:
You can use SwiftMailer, but you must to add new Image TextHeader 'Content-Location'.
Here is the code:
$message = \Swift_Message::newInstance()->setSubject($subject)->setFrom($fromEmail, 'Name')->setTo($toEmail)->setBcc($bccEmails);
/*get uniqueID from cid:uniqueID */
$imageID = explode(':', $message->embed(\Swift_Image::fromPath('pathToTheImage')->setContentType('image/png')))[1];
/*Add Content-Location to image header*/
/** #var \Swift_Image $image */
$image = $message->getChildren()[0];
$image->getHeaders()->addTextHeader('Content-Location', $imageID);
$message->setBody('here you will have some html with <img src=$imageID alt="Embed Image">', 'text/html');
$mailer->send($message);
function for get all images url :
function getImagesFromMsg($msg, $tmpFolderPath)
{
$arrSrc = array();
if (!empty($msg))
{
preg_match_all('/<img[^>]+>/i', stripcslashes($msg), $imgTags);
//All img tags
for ($i=0; $i < count($imgTags[0]); $i++)
{
preg_match('/src="([^"]+)/i', $imgTags[0][$i], $withSrc);
//Remove src
$withoutSrc = str_ireplace('src="', '', $withSrc[0]);
//data:image/png;base64,
if (strpos($withoutSrc, ";base64,"))
{
//data:image/png;base64,.....
list($type, $data) = explode(";base64,", $withoutSrc);
//data:image/png
list($part, $ext) = explode("/", $type);
//Paste in temp file
$withoutSrc = $tmpFolderPath."/".uniqid("temp_").".".$ext;
#file_put_contents($withoutSrc, base64_decode($data));
}
//Set to array
$arrSrc[] = $withoutSrc;
}
}
return $arrSrc;
}
Try to test if $_POST['imageFromOtherPage'] contains the base64 , probably the code is too long for a Post, you would need to use php://input .
<img src="'.$_POST['imageFromOtherPage'].'"/>
Your POST needs to start with data:image/png;base64, to form a base64 encoded data: URI.
But even if you fix this, it is well possible that the E-Mail client you're viewing the message in doesn't support the method at all. data:URIs are a relatively new phenomenon - at least in the chronology of E-Mail clients, most of which are still in the process of discovering that there's a technology called CSS. The tried and tested method for images in E-Mails is to embed the image as an inline attachment.
Here are some approaches that don't rely on a library: Make PHP send an email with an inline image attachment
However, using a library like Swiftmailer may take away a lot of pain. Here is the appropriate Swiftmailer documentation.

how to add php code in .tpl file in prestashop

Does anyone know how to add php code in .tpl file in prestashop..i tried a lot but couldn't find any solutions..I want to add a mail function in my .tpl file
This is the function
<?php
$name=$_REQUEST["name"];
$email=$_REQUEST["email"];
$matter=$_REQUEST["matter"];
$subject=$name."has shared a weblink";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$contactMsg = '
<html>
<head>
<title>Tell A Friend</title>
</head>
<body>
<table>
<tr>
<td>E-mail: '.$email.' </td>
</tr>
<tr>
<td>Comment: '.$matter.' </td>
</tr>
</table>
</body>
</html>
';
$headers .= "from:abcd" . "\r\n";
$headers .= "To: Info <info#abcd.in";
$headers .= "reply-to:".$email."\r\n";
$headers .= "Cc:".$email."\r\n";
$headers .= 'Bcc: ' . "\r\n";
if(mail($email, $subject, $contactMsg, $headers))
{
$_REQUEST = array();
$error.="Mail Sent Successfully";
}
else
{
$error.="Error Mail Sent Fail!";
//
}
?>
i tried writing the code inside {php} {/php} block..but couldn't help
And how to see the error log in prestashop
You can edit the controller file, eg. if you want to add the form in a cms page, you have to edit this /controller/CMSController.php
edit this
public function displayContent()
{
parent::displayContent();
self::$smarty->display(_PS_THEME_DIR_.'cms.tpl');
}
with this
public function displayContent()
{
IF ($_POST['submit_form']==1){
// here submit action mail() for example
}
parent::displayContent();
self::$smarty->display(_PS_THEME_DIR_.'cms.tpl');
}
it's better to add php functions classes etc in your module controller,
{php} is depreciated for a reason as it allow poor practices. Smarty recommends putting the included script into the PHP logic (controllers,classes,functions)
The use of the {php}{/php} tags in .tpl files is turned off in somme versions of Prestashop
I had the same problem using Prestashop 1.5.4.1
To turn it on make changes in the file config/smarty.config.inc.php :
find these lines (line 27 to line33)
...
define('_PS_SMARTY_DIR_', _PS_TOOL_DIR_.'smarty/');
require_once(_PS_SMARTY_DIR_.'Smarty.class.php');
global $smarty;
$smarty = new Smarty();
$smarty->setCompileDir(_PS_CACHE_DIR_.'smarty/compile');
...
change it to
...
define('_PS_SMARTY_DIR_', _PS_TOOL_DIR_.'smarty/');
require_once(_PS_SMARTY_DIR_.'SmartyBC.class.php');
global $smarty;
$smarty = new SmartyBC();
$smarty->setCompileDir(_PS_CACHE_DIR_.'smarty/compile');
...
...shortly, use SmartyBC.class.php instead of Smarty.class.php
(warning: using {php}{/php} tags in template files is deprecated in Prestashop!)

Categories