Unix sendmail - html embed image not working - html

With the prior posts in SO.com I have tried building my script to send email to my Outlook account with the Image inline in the email body. But the html contents are getting displayed in the html rather displaying the image. Please help.
Here is my snippet
{
echo "TO: XXX#YYY.com"
echo "FROM: TEST_IMAGE#YYY.com>"
echo "SUBJECT: Embed image test"
echo "MIME-Version: 1.0"
echo "Content-Type: multipart/related;boundary="--XYZ""
echo "--XYZ"
echo "Content-Type: text/html; charset=ISO-8859-15"
echo "Content-Transfer-Encoding: 7bit"
echo "<html>"
echo "<head>"
echo "<meta http-equiv="content-type" content="text/html; charset=ISO-8859-15">"
echo "</head>"
echo "<body bgcolor="#ffffff" text="#000000">"
echo "<img src="cid:part1.06090408.01060107" alt="">"
echo "</body>"
echo "</html>"
echo "--XYZ"
echo "Content-Type: image/jpeg;name="sathy.jpg""
echo "Content-Transfer-Encoding: base64"
echo "Content-ID: <part1.06090408.01060107>"
echo "Content-Disposition: inline; filename="sathy.jpg""
echo $(base64 sathy.jpg)
echo "' />"
echo "--XYZ--"
}| /usr/lib/sendmail -t
Email I received contains the below rather displaying the image,
--XYZ
Content-Type: text/html; charset=ISO-8859-15
Content-Transfer-Encoding: 7bit
<html>
<head>
<meta http-equiv=content-type content=text/html
</head>
<body bgcolor=#ffffff text=#000000>
<img src=cid:part1.06090408.01060107 alt=>
</body>
</html>
--XYZ
Content-Type: image/jpeg;name=sathy.jpg
Content-Transfer-Encoding: base64
Content-ID: <part1.06090408.01060107>
Content-Disposition: inline; filename=sathy.jpg
/9j/4QAYRXhpZgAASUkqAAgAAAAAAAAAAAAAAP/sABFEdWNreQABAAQAAAAoAAD/4QNxaHR0cDov
....base64 values.....
/>
--XYZ--
----XYZ--
Can you please assist me in what am i missing

The way you use echo to print the message headers it eats all double quotes - you need to escape them with a backslash (\") to make it work.
Also, your boundary is wrong. If you define boundary=--XYZ, then each message part needs to start with ----XYZ (you need to add two dashes), otherwise your boundary should be only XYZ. And the headers of the mime parts must be separated from the bodies by an empty line.
If you really need to generate a mail from a shell script, then my advise would be to get rid of all the echo and use a heredoc instead:
sendmail -t <<EOT
TO: XXX#YYY.com
FROM: <TEST_IMAGE#YYY.com>
SUBJECT: Embed image test
MIME-Version: 1.0
Content-Type: multipart/related;boundary="XYZ"
--XYZ
Content-Type: text/html; charset=ISO-8859-15
Content-Transfer-Encoding: 7bit
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-15">
</head>
<body bgcolor="#ffffff" text="#000000">
<img src="cid:part1.06090408.01060107" alt="">
</body>
</html>
--XYZ
Content-Type: image/jpeg;name="sathy.jpg"
Content-Transfer-Encoding: base64
Content-ID: <part1.06090408.01060107>
Content-Disposition: inline; filename="sathy.jpg"
$(base64 sathy.jpg)
--XYZ--
EOT

Related

CGI (shell script) not write in the file with echo command

I'm having problems to write in the file with my CGI.
I make a simple HTML formular to press a button and write a phrase in the .txt file, but it doesn't work.
The server is an Apache2.
The scrips works if executed in the terminal.
ps. The final line of CGI works, updating a HTML page URL.
file index.html
<html>
<title>BeagleBone</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
<body>
<div class="w3-container w3-green">
<h1>BeagleBone GPIO Control</h1>
<p>Acesso remoto a GPIO's em rede local</p>
</div>
<div class="w3-row-padding">
<div class="w3-third">
<h2>GPIO x</h2>
<p>Led blue</p>
<form method="POST" action="/cgi-bin/led.cgi">
<input type="submit" name="led" value="ON">
<input type="submit" name="led" value="OFF">
</form>
</div>
</body>
</html>
file led.gci
#!/bin/bash
LED_FILE="/home/gpcosta/Desktop/data.txt"
read CONTENT
if [ "$CONTENT" == "led=ON" ]; then
echo "Apertou botao ON" >> $LED_FILE
elif [ "$CONTENT" == "led=OFF" ]; then
echo "Apertou botao OFF" >> $LED_FILE
fi
echo "Content-type: text/html"
echo ""
echo '<html>'
echo '<meta http-equiv="refresh" CONTENT="0;url=http://192.168.15.20/index.html">'
echo '</html>'
The cgi permissions below
enter image description here
It's because the web server don't have permissions on the directory.
To solve this, run the following script on your cgi server:
echo “content-type: text/html”
echo “”
echo “<html>
<head>
<meta charset='utf-8'/>
<title>What permission?</title>
</head>
<body>
<p>Run this on your server terminal:</p>
<pre>chown $(whoami) ${PWD}
chmod 755 ${PWD}</pre>
</body>
</html>”
Then simply copy the script your web browser will display and paste it on the CGI Server terminal, remember to run it with a priviledged user.

Generating emails using blat.exe not working for every domain

I use blat.exe running from Powershell to generate e-mails with HTML. When I'm sending an e-mail to the Google domain everything is OK and code is generating fine. The headers and rest of code look like this:
<html xmlns=3D"http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv=3D"Content-Type" content=3D"text/html; charset=3Dutf-8">
<title>My email</title>
</head>
...
<tr style=3D"height:15pt;">
<td style=3D"border: 1pt solid; border-color:black; padding-left:3.5pt=
; height: 15pt;">
...
But when I send the same e-mail to my client's domain, the HTML code look much different:
<html xmlns="ttp://www.w3.org/1999/xhtml""><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>My email</title>
</head>
...
<tr style="eight:15pt;"">
<td style="order:" 1ptsolid;border-color:black;padding-left:3.5pt;height:15pt;?="">
...
Do you have any ideas why in second option first letters after quotes are missing or why quotes at the end of the line are changed to "" or ?="? Spaces in style are missing too.
My blat command looks like this: ./blat.exe 'C:\path\file.html' -f alert#clientsdomain.com -server mail.clients.server.addr -to 'my_email#google.com,other_email#clientsdomain.com' -subject 'email title' -html
Thanks in advance for any tips!
I found neither the reason nor the exact solution but I have some workaround.
I used just Powershell with a simple command and it worked:
$body= Get-Content "C:\path_to_html_file\file.html" | out-string
Send-MailMessage -From "alert#clientsdomain.com" -to 'my_email#google.com', 'other_email#clientsdomain.com' -Subject "email title" -Body $body -BodyASHtml -SmtpServer "mail.clients.server.addr" -Encoding ([System.Text.Encoding]::UTF8)

Scripting in Perl/cURL, can't get past HTML-form w/ two submits and click() onload

I'm making a script where a major part of it is to login through a system called Feide. I've used Perl and cURL, and I've managed to actually log in and make a session cookie, but due to the fact that cURL doesn't support Javascript, I have to press a button ("Since your browser does not support JavaScript, you must press the button below once to proceed.") to get to the content of the site I want to access. Unfortunately, the form is weird. When I run my script, it simply redirects me back to the same site, and I don't know why. Here's the HTML:
<body onload="document.getElementsByTagName('input')[0].click();">
<noscript>
<p><strong>Note:</strong> Since your browser does not support JavaScript, you must press the button below once to proceed.</p>
</noscript>
<form method="post" action="https://innsida.ntnu.no/c/portal/login">
<!-- Need to add this element and call click method, because calling submit()
on the form causes failed submission if the form has another element with name or id of submit.
See: https://developer.mozilla.org/en/DOM/form.submit#Specification -->
<input type="submit" style="display:none;" />
<input type="hidden" name="SAMLResponse" value="[very long value]" />
<input type="hidden" name="RelayState" value="https://innsida.ntnu.no" />
<noscript>
<input type="submit" value="Submit" />
</noscript>
</form>
</body>
Maybe there's something I'm missing, but I can't seem to submit this form in a proper manner. Here's my script:
use strict;
use warnings;
use WWW::Curl::Easy;
use WWW::Curl::Multi;
use WWW::Curl::Share;
use WWW::Curl::Form;
use DBI;
my $dbh = DBI->connect('DBI:mysql:feide', '[myusername]', '[mypassword]') || die "Could not connect to database: $DBI::errstr";
my $username = $dbh->prepare('SELECT username FROM credentials WHERE id=1');
my $password = $dbh->prepare('SELECT password FROM credentials WHERE id=1');
my $curl = WWW::Curl::Easy-> new;
$curl->setopt(CURLOPT_HEADER, 1);
#Make sure redirects don't fuck me
$curl->setopt(CURLOPT_FOLLOWLOCATION, 1);
#Where cookies are saved
$curl->setopt(CURLOPT_COOKIEJAR, 'cookies.txt');
#Where cookies are read from
$curl->setopt(CURLOPT_COOKIEFILE, 'cookies.txt');
$curl->setopt(CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1");
$curl->setopt(CURLOPT_URL, 'https://idp.feide.no/simplesaml/module.php/feide/login.php?asLen=225&AuthState=_3f669be75024644be95e67ec6845222aefa860db98%3Ahttps%3A%2F%2Fidp.feide.no%2Fsimplesaml%2Fsaml2%2Fidp%2FSSOService.php%3Fspentityid%3Durn%253Amace%253Afeide.no%253Aservices%253Ano.ntnu.innsida%26cookieTime%3D1456232047%26RelayState%3Dhttps%253A%252F%252Finnsida.ntnu.no');
my $response_body;
$curl->setopt(CURLOPT_WRITEDATA,\$response_body);
my $retcode = $curl->perform;
my $checkSession = 0;
my #split;
#check if there is a current session
if($response_body =~ /"AuthState" value="/){
#split = split /"AuthState" value="/, $response_body;
my #authState = split /"/, $split[1];
#make a form for a POST-request
my $curlf = WWW::Curl::Form->new;
$checkSession = 1;
$curlf->formadd("feidename", $username);
$curlf->formadd("password", $password);
$curlf->formadd("asLen", "225");
$curlf->formadd("AuthState", $authState[0]);
$curlf->formadd("org", "ntnu.no");
$curl->setopt(CURLOPT_HTTPPOST, $curlf);
$retcode = $curl->perform;
}
if ($checkSession){
#split = ();
#split = split /"SAMLResponse" value="/, $response_body;
}
else{
my #split = split /"SAMLResponse" value="/, $response_body;
}
my #SAML = split /"/, $split[1];
#Here I try to create a new form, where I add what
#I believe to be the correct values. They don't work,
#and I get redirected back to the "press the button"-website
my $noJS = WWW::Curl::Form->new;
$noJS->formadd("SAMLResponse", $SAML[0]);
$noJS->formadd("RelayState", "https://innsida.ntnu.no");
$curl->setopt(CURLOPT_HTTPPOST, $noJS);
$curl->setopt(CURLOPT_URL, 'https://innsida.ntnu.no/c/portal/login');
$curl->perform;
print $response_body, "\n";
Naturally, I can't put my username and password to Feide here, so you won't be able to try it out for yourselves. Hopefully someone sees a solution regardless.
I'm posting the entire HTML-code (the output of the script) here at the end, in case someone's interested:
HTTP/1.1 302 Found
Date: Wed, 24 Feb 2016 10:35:10 GMT
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 1
Pragma: no-cache
Cache-Control: no-cache, no-store
Expires: Thu, 01 Jan 1970 00:00:00 GMT
Location: https://idp.feide.no/simplesaml/saml2/idp/SSOService.php?SAMLRequest=[removed for security purposes]
Content-Length: 0
HTTP/1.1 200 OK
Date: Wed, 24 Feb 2016 10:35:10 GMT
Server: Apache/2.2.24 (Unix)
X-Powered-By: PHP/5.3.26
Content-Length: 7866
Content-Type: text/html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>POST data</title>
</head>
<body onload="document.getElementsByTagName('input')[0].click();">
<noscript>
<p><strong>Note:</strong> Since your browser does not support JavaScript, you must press the button below once to proceed.</p>
</noscript>
<form method="post" action="https://innsida.ntnu.no/c/portal/login">
<!-- Need to add this element and call click method, because calling submit()
on the form causes failed submission if the form has another element with name or id of submit.
See: https://developer.mozilla.org/en/DOM/form.submit#Specification -->
<input type="submit" style="display:none;" />
<input type="hidden" name="SAMLResponse" value="[ridiculously long value]==" />
<input type="hidden" name="RelayState" value="https://innsida.ntnu.no" />
<noscript>
<input type="submit" value="Submit" />
</noscript>
</form>
</body>
</html>
If I left out something, let me know, and I'll do my best to provide it.
EDIT: When I turned off Javascript in my browser and copied as cUrl, I got this:
curl 'https://innsida.ntnu.no/c/portal/login'
-H 'Host: innsida.ntnu.no'
-H 'User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:44.0) Gecko/20100101 Firefox/44.0' -H 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'
-H 'Accept-Language: en-US,en;q=0.5' --compressed -H 'DNT: 1' -H 'Referer: https://idp.feide.no/simplesaml/module.php/feide/login.php?asLen=225&AuthState= [removed]https%253A%252F%252Finnsida.ntnu.no'
-H 'Cookie: COOKIE_SUPPORT=true; GUEST_LANGUAGE_ID=nb_NO; JSESSIONID=[removed].innsidaprod01; LFR_SESSION_STATE_111384=[removed]; LFR_SESSION_STATE_10135=[removed] -H 'Connection: keep-alive'
--data 'SAMLResponse=[long value]&RelayState=https%3A%2F%2Finnsida.ntnu.no'
I suspect that the form is not validating, and you therefore are redirected to the same form. If they don't have any special kind of scripting protection you should be able to create a valid request by going to the Network Tab of Developer Tools on your browser, and select Copy as cURL. This will give you a list of all options being set on a valid request.
This should help you get past the redirect loop.

Generating an HTML page using a bash script, but image tags are broken

#!/bin/bash
echo "Content-type: text/html"
echo
echo "<h1>You Rolled 9 dice</h1>"
dice=( /images/die1.png /images/die2.png /images/die3.png /images/die4.png /images/die5.png /images/die6.png)
index=$RANDOM
let "index = index % 6"
href="${dice[$index]}"
echo "<a href=$href><img src=$href></a>"
This is what I have so far. I want the page to stay text/html because I'm going to be writing more on it. It keeps telling me that the requested URL /images/die5.png was not found on this server.
You simply need to remove the leading / character from your image file names:
#!/bin/bash
dice=(images/die1.png images/die2.png images/die3.png images/die4.png images/die5.png images/die6.png)
index=$RANDOM
let "index = index % 6"
href="${dice[$index]}"
echo '<!DOCTYPE html>'
echo '<html>'
echo ' <head>'
echo ' <meta charset="utf-8">'
echo ' <title>Example</title>'
echo ' </head>'
echo ' <body>'
echo ' <h1>You Rolled 9 dice</h1>'
echo " <a href='$href'><img src='$href' alt='image'></a>"
echo ' </body>'
echo '</html>'
After running the script like so:
thomas#yozu-thomas:~/Programming/bashbrowser$ ./makehtm.sh > out.htm
The output I get is:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Example</title>
</head>
<body>
<h1>You Rolled 9 dice</h1>
<a href='images/die3.png'><img src='images/die3.png' alt='image'></a>
</body>
</html>
...which looks like this:
Note: I'm assuming that the file structure is something like this:
makehtm.sh
out.htm
images/
die1.png
die2.png
die3.png
die4.png
die5.png
die6.png
Additionally, you don't need to echo "Content-type: text/html" within the HTML page itself; this is an HTTP header field, and will be handled by the web server that hosts the HTML file.

Display a table using Perl CGI

I have to display a file in HTML table format.
I tried this but I cannot get any output.
use CGI qw(:standard);
my $line;
print '<HTML>';
print "<head>";
print "</head>";
print "<body>";
print "<p>hello perl am html</p>";
print "</body>";
print "</html>";
A CGI program must output the HTTP headers before it outputs any content. At a minimum, it must supply an HTTP Content-Type header.
Add:
my $q = CGI->new;
print $q->header('text/html; charset=utf-8');
… before you output any HTML.
(You should also write valid HTML, so include a Doctype and <title>).
You should use the CGI module once you have loaded it. It makes it much simpler to follow the correct rules for an HTTP page.
As has been observed, you need to print an HTTP header before the HTML body, and you can do that with print $cgi->header which defaults to specifying a content type of text/html and a character set of ISO-8859-1, which is adequate for many simple HTML pages. It also generates a <meta> element within the HTML that contains the same information.
This short program shows the idea. I have added a trivial table that shows how you could include that in the page. As you can see, the CGI code is much simpler than the corresponding HTML.
use strict;
use warnings;
use CGI qw/ :standard /;
print header;
print
start_html('My Title'),
p('Hello Perl am HTML'),
table(
Tr([
td([1, 2, 3]),
td([4, 5, 6]),
])
),
end_html
;
output
Content-Type: text/html; charset=ISO-8859-1
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US" xml:lang="en-US">
<head>
<title>My Title</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
<body>
<p>Hello Perl am HTML</p><table><tr><td>1</td> <td>2</td> <td>3</td></tr> <tr><td>4</td> <td>5</td> <td>6</td></tr></table>
</body>
</html>
How about this:
use CGI;
use strict;
my $q = CGI->new;
print $q->header.$q->start_html(-title=>'MyTitle');
my $tableSettings = {-border=>1, -cellpadding=>0, -cellspacing=>0};
print $q->table($tableSettings, $q->Tr($q->td(['column1', 'column2', 'column3'])));
print $q->end_html;
Output:
Content-Type: text/html; charset=ISO-8859-1
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US" xml:lang="en-US">
<head>
<title>MyTitle</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
<body>
<table border="1" cellspacing="0" cellpadding="0"><tr><td>column1</td> <td>column2</td> <td>column3</td></tr></table>
</body>
</html>