Coldfusion, send cfhtmltopdf by email - coldfusion

Is possible to send a pdf created using cfhtmltopdf in the email? I'm trying to set the cfhtmltopdf in a variable and send by email, but I think it is not possible.
Here is my test cfhtmltopdf:
<cfhtmltopdf
orientation="portrait" pagetype="A4" margintop="1" marginbottom="1" marginleft="1">
<html>
<head>
<body>
<table>
<tr>
<th>Test1</th>
<th>Test2</th>
<th>Test3</th>
</tr>
<tr>
<td>ABC</td>
<td>ABC</td>
<td>ABC</td>
</tr>
<tr>
<td>ABC</td>
<td>ABC</td>
<td>ABC</td>
</tr>
<tr>
<td>ABC</td>
<td>ABC</td>
<td>ABC</td>
</tr>
</table>
</body>
</head>
</html>
</cfhtmltopdf>

Simply add destination attribute with the path for creating the file, then use that path for the mail.
<cfset filePath = GetTempDirectory() & "emailfile.pdf">
<cfhtmltopdf destination="#filePath#" orientation="portrait" pagetype="A4" margintop="1" marginbottom="1" marginleft="1" >
<html>
<head>
</head>
<body>
<table>
<tr>
<th>Test1</th>
<th>Test2</th>
<th>Test3</th>
</tr>
<tr>
<td>ABC</td>
<td>ABC</td>
<td>ABC</td>
</tr>
</table>
</body>
</html>
</cfhtmltopdf>
<cfmail to="..." .... >
<cfmailparam file="#filePath#" disposition="attachment" type="#fileGetMimeType(filePath)#" remove="true">
<cfmailpart type="html">Content</cfmailpart>
</cfmail>

Related

I have been trying to make an error handling on Coldfusion, everything is working good but I am not able to get the current session username

Application.cfm
<cfapplication name="AppError" clientmanagement="Yes" sessionmanagement="Yes"
clientstorage="cookie">
<cferror type="exception" template="ExpressError.cfm" exception="expression">
<cferror type="exception" template="DatabaseError.cfm" exception="database">
ExpressError.cfm
<cfoutput>
Sorry! Internal Error Occurred!
<cfsavecontent variable="errortext">
<div class="container">
<table class="err">
<tr>
<th>URL</th><td>#cgi.server_name##cgi.script_name#</td>
</tr>
<tr>
<th>Message</th><td>#Error.message#</td>
</tr>
<tr>
<th>Diagnostics</th><td>#Error.Diagnostics#</td>
</tr>
<tr>
<th>Template</th><td>#Error.TEMPLATE#</td>
</tr>
</table>
</div>
</cfsavecontent>
<cfmail to="xxx#xxx.com" from="yyy#yyyy.com" subject="Error : #Error.message#" type="html">
<HTML>
<head>
<style type="text/css">
<cfinclude template="Style/errorstyle.css">
</style>
</head>
<body>
#errortext#
</body>
</HTML>
</cfmail>
</cfoutput>
DatabaseError.cfm
<cfoutput>
Sorry! Internal Error Occurred!
<cfsavecontent variable="errortext">
<div class="container">
<table class="err">
<tr>
<th>Application</th><td>#elmsapp#</td>
</tr>
<tr>
<th>URL</th><td>#cgi.server_name##cgi.script_name#</td>
</tr>
<tr>
<th>Message</th><td>#Error.message#</td>
</tr>
<tr>
<th>Diagnostics</th><td>#Error.Diagnostics#</td>
</tr>
<tr>
<th>Template</th><td>#Error.TEMPLATE#</td>
</tr>
<tr>
<th>Sql</th><td>#Error.RootCause.Sql#</td>
</tr>
<tr>
<th>DataSource</th><td>#Error.RootCause.DataSource#</td>
</tr>
<tr>
<th>RemoteAddress</th><td>#Error.RemoteAddress#</td>
</tr>
</table>
</div>
</cfsavecontent>
<cfmail to="xxx#xxx.com" from="yyy#yyyyy.com" subject="Error: #Error.message#" type="html">
<HTML>
<head>
<style type="text/css">
<cfinclude template="Style/errorstyle.css">
</style>
</head>
<body>
#errortext#
</body>
</HTML>
</cfmail>
</cfoutput>
test.cfm
<cfoutput>#sdfssfsdf#</cfoutput>
user.cfm
<cfoutput> #trim(session.auth_user)# </cfoutput>
So when i run test.cfm, i am getting a correct email saying "Variable SDFSSFSDF is undefined." but when i run user.cfm, i am getting an email saying "Element AUTH_USER is undefined in SESSION.".
Point is when i delete application.cfm, user.cfm page gives the correct username of the session but with application.cfm in place...it gives an error. I think i need to define the sessiom.auth_user but how and where is the question.
Would appreciate if someone could please help me out. Thanks

How to send an email using email_user containing HTML and CSS in Python Django?

I am sending an activation link to the user on their registered email for my blog. I am using the following lines of code for sending the email in my signup view. I am not using EmailMultiAlternatives or send_email because, I am creating a unique token for the user signing up and I want to use email_user, which will automatically take care of for and to email address:-
current_site = get_current_site(request)
subject = 'Activate Your Account'
message = render_to_string('accounts/account_activation_email.html', {
'user': user,
'domain': current_site.domain,
'uid': urlsafe_base64_encode(force_bytes(user.pk)).decode(),
'token': account_activation_token.make_token(user),
})
message.content_subtype = "html"
user.email_user(subject, message)
I have added html and css in my template account_activation_email.html as under:-
{% autoescape off %}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Activate your SaralGyaan account</title>
<style type="text/css" rel="stylesheet" media="all">
[styles go there not shown due to beverity]
</style>
</head>
<body>
<span class="preheader">Use this link to activate your SaralGyaan account. The link is only valid for 24 hours.</span>
<table class="email-wrapper" width="100%" cellpadding="0" cellspacing="0">
<tr>
<td align="center">
<table class="email-content" width="100%" cellpadding="0" cellspacing="0">
<tr>
<td class="email-masthead">
<a href="https://saralgyaan.com" class="email-masthead_name">
SaralGyaan
</a>
</td>
</tr>
<!-- Email Body -->
<tr>
<td class="email-body" width="100%" cellpadding="0" cellspacing="0">
<table class="email-body_inner" align="center" width="570" cellpadding="0" cellspacing="0">
<!-- Body content -->
<tr>
<td class="content-cell">
<h1>Hi {{user.get_full_name}},</h1>
<p>Thank you for registering your SaralGyaan's account. Please click the link below to activate your account. <strong>This link is only valid for the next 24 hours.</strong></p>
<!-- Action -->
<table class="body-action" align="center" width="100%" cellpadding="0" cellspacing="0">
<tr>
<td align="center">
<!-- Border based button
https://litmus.com/blog/a-guide-to-bulletproof-buttons-in-email-design -->
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td align="center">
<table border="0" cellspacing="0" cellpadding="0">
<tr>
<td>
Activate your account
</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
</table>
<p> If you have not created an account, please ignore this email or contact support if you have questions.</p>
<p>Thanks,
<br>The SaralGyaan Team</p>
<!-- Sub copy -->
<table class="body-sub">
<tr>
<td>
<p class="sub">If you’re having trouble with the button above, copy and paste the URL below into your web browser.</p>
<p class="sub">{{ protocol }}://{{ domain }}{% url 'activate' uidb64=uid token=token %}</p>
</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td>
<table class="email-footer" align="center" width="570" cellpadding="0" cellspacing="0">
<tr>
<td class="content-cell" align="center">
<p class="sub align-center">© 2018 SaralGyaan. All rights reserved.</p>
<p class="sub align-center">
[SaralGyaan]
</p>
</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>
But the email which I am receiving is not the html version of it but plain text. There is no error in this html code as I am using the same with my password reset email and it is working fine there.
You can't send HTML alone in an email. HTML is sent as an alternative to plain text.
Use the html_message to send the html part and use message to send in plain text.
message = render_to_string('template.txt', ...)
html_message = render_to_string('textmplate.html', ...)

CFWINDOW - Form submit in CF to stay inside CFWindow

I have a page with this basic process:
Click checkboxes next to forms you want to download
Click submit
CFWindow pops up to collect some basic info
Click submit to download forms while staying in the window that was opened by CFWindow
When you click the submit button inside the CFWindow , it brings you back to the parent page. I want the form submission to stay inside the CFWindow. I saw this post: Refresh cfwindow content. However, the refreshOnShow = "true" does not work. Every time I hit submit, it goes back to the parent window.
Here is the Parent page:
<cfform name="myform">
<cfinput type="hidden" name="OrgID" value="#getit.orgID#">
<table width="95%" border="0" cellspacing="5" cellpadding="0" align="center">
<tr>
<td width="50%" valign="top" class="coretextforBR">
<cfinput type="checkbox" name="GetThese" value="#form_ID#">
<a class="corelinkforBR">#Forms_Name#</a>
<br /><br />
#Forms_Description#
<br /><br />
</td>
</tr>
<tr>
<td width="50%" valign="top" class="coretextforBR">
<cfinput type="checkbox" name="GetThese" value="#form_ID#">
<a class="corelinkforBR">#Forms_Name#</a>
<br /><br />
#Forms_Description#
<br /><br />
</td>
</tr>
<tr>
<td width="50%" valign="top" class="coretextforBR">
<cfinput type="checkbox" name="GetThese" value="#form_ID#">
<a class="corelinkforBR">#Forms_Name#</a>
<br /><br />
#Forms_Description#
<br /><br />
</td>
</tr>
<tr>
<td colspan="2">
<input type="submit"value="Get It" onclick="javascript:ColdFusion.Window.show('mywindow1')">
</td>
</tr>
</table>
</cfform>
<cfwindow x="250" y="250" width="400" height="400"
name="mywindow1" title="Almost ready to download" initshow="false" draggable="false" resizable="false"
refreshOnShow="true" source="submitform.cfm?GetThese={myform:GetThese.value}&OrgID={myform:OrgID}&action=information"
bodystyle="background-color: white;" headerStyle="background-color: ###getcss.color#; font-family: #getcss.font#; color: ###getcss.fontcolor#;" />
Here is the source (submit.cfm) for the CFWwindow:
<cfparam name="attributes.action" default="information">
<cfoutput>
<html>
<head>
<style type="text/css">
</style>
</head>
<body>
This window will collect information to begin download
<br>
<!--- action for downloading --->
<cfif attributes.action eq "download">
<cfloop info and stuff left out>
#Forms_Name#<br />
</cfloop>
<!--- what you see when page initially loads --->
<cfelse>
<form action="submitform.cfm?action=download" method="post">
<input type="hidden" name="GetThese" value="#attributes.GetThese#">
<input type="hidden" name="OrgID" value="#attributes.OrgID#">
<table width="95%" border="0" align="center">
<tr>
<td class="coretextforBR">First:</td>
<td><input type="text" name="CollectedInfo_First"></td>
</tr>
<tr>
<td class="coretextforBR">Last:</td>
<td><input type="text" name="CollectedInfo_Last"></td>
</tr>
<tr>
<td class="coretextforBR">Phone:</td>
<td><input type="text" name="CollectedInfo_Phone"></td>
</tr>
<tr>
<td class="coretextforBR">Email:</td>
<td><input type="text" name="CollectedInfo_Email"></td>
</tr>
<tr>
<td class="coretextforBR">Best way <br> to contact:</td>
<td><input type="text" name="CollectedInfo_BestWay"></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="Download" class="button one"></td>
</tr>
</table>
</form>
</cfif>
</cfoutput>
</body>
</html>
I have no idea what I am doing wrong. I couldn't find a definite answer to my problem either other than the refreshOnShow. Should I rethink and do a ajax submit?
Using <cfform> instead of normal <form> worked as Liegh suggested.

PHP insert won't work

I have been battling with phpMyAdmin insert for sometime now,
It doesn't give me an error, yet it won't insert into the database.
My code goes like this
<html>
<head>
<meta http-equiv="Content-Language" content="en-us">
<meta name="GENERATOR" content="Microsoft FrontPage 5.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>New Page 1</title>
</head>
<body>
<table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="43%" id="AutoNumber1">
<tr>
<td width="100%">
<p align="left"> </td>
</tr>
<tr>
<td width="100%">
<form method="GET" name="testform" id="testform" action="logout.php">
<table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="69%" id="AutoNumber2" height="63">
<tr>
<td width="36%" height="22">Username</td>
<td width="64%" height="22"><input type="text" name="user" size="20"></td>
</tr>
<tr>
<td width="36%" height="22">password</td>
<td width="64%" height="22">
<input type="password" name="passwd" size="20"></td>
</tr>
<tr>
<td width="36%" height="19"> </td>
<td width="64%" height="19">
<input type="submit" value="login" name="login"></td>
</tr>
</table>
</form>
</td>
</tr>
</table>
</body>
</html>
Now the php also goes like this
<?php
require_once('inc/config.php');
$user = $_GET['user'];
$pass = $_GET['passwd'];
//database connection
$conn = mysqli_connect(DBHOST,DBUSER,DBPASS,DB);
//mysql anti injection
$username = mysql_real_escape_string($_GET['user']);
$password = mysql_real_escape_string($_GET['passwd']);
$sql = "INSERT INTO people (`username`, `password`) VALUES ('$username', '$password');";
mysqli_close($conn);
echo "Inserted";
?>
Trouble is, I don't see what I insert into the mysql database, when viewed on local host; what is the problem?
Seems like the issue might be the use of ';' twice at the end of the following line:
$sql = "INSERT INTO people (username, password) VALUES ('$username', '$password');";
Try the following.
$sql = "INSERT INTO people (username, password) VALUES ('$username', '$password');"

How to improve this Enlive template?

I use this Enlive template to transform the HTML below it to the HTML below that. Based on a collection of twitter names I generate a table with links. How can I get rid of the Hiccup inside the enlive/clone-for?
(enlive/deftemplate usernames-table-body
"public/whoisnotfollowingme.html"
[usernames]
[:table.names :tbody :tr]
(enlive/clone-for [username usernames]
[:td]
(enlive/html-content
(html [:a {:href (str "https://twitter.com/intent/user?screen_name=" username)} username]))))
HTML input
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="/bootstrap/css/bootstrap.css"/>
</head>
<body>
<script type="text/javascript" src="//platform.twitter.com/widgets.js"></script>
<div class="container">
<div class="hero-unit">
<p class="names">These people who you are following are not following you back!</p>
</div>
<table class="names table table-striped">
<thead>
<tr>
<th>Username</th>
</tr>
</thead>
<tbody>
<tr>
<td>name</td>
</tr>
</tbody>
</table>
</div>
</body>
</html>
HTML output
<html>
<head>
<link href="/bootstrap/css/bootstrap.css" rel="stylesheet" />
</head>
<body>
<script src="//platform.twitter.com/widgets.js" type="text/javascript"></script>
<div class="container">
<div class="hero-unit">
<p class="names">These people who you are following are not following you back!</p>
</div>
<table class="names table table-striped">
<thead>
<tr>
<th>Username</th>
</tr>
</thead>
<tbody>
<tr>
<td> < a href =" https://twitter.com/intent/user?screen_name=foo " > foo </ a > </td>
</tr> <tr>
<td> < a href =" https://twitter.com/intent/user?screen_name=bar " > bar </ a > </td>
</tr>
</tbody>
</table>
</div>
</body>
</html>
You could change your tbody.tr template from:
<tr>
<td>name</td>
</tr>
to:
<tr>
<td>foo</td>
</tr>
Now your HTML resource is a working example of the output you want.
Then modify your deftemplate to support it:
(enlive/deftemplate usernames-table-body
"public/whoisnotfollowingme.html"
[usernames]
[:table.names :tbody :tr]
(enlive/clone-for [username usernames]
[:td :a]
(enlive/do->
(enlive/set-attr :href (str "https://twitter.com/intent/user?screen_name=" username))
(enlive/content username))))
Edited: If you want to get rid of the URL in the code, try change your href to ?screen_name= and then modify the code to something like:
(enlive/do->
(fn [node] (update-in node [:attrs :href] #(str % username)))
(enlive/content username))))
You could also make a function of it. See e.g. Append to an attribute in Enlive.