Sending inline strings through SES API of Amazon - amazon

I am trying to send barcode along with my html mail content through amazon.I am using SES service .
In mail img tag is added with src="cid:barcodeImage"
and ses code is
if (is_array($files)) {
$count = count($files);
foreach ($files as $file) {
$attach .= "\n";
$attach .= "--$this->boundary\n";
$attach .= "Content-Transfer-Encoding: base64\n";
$attach .= "Content-ID: <$file[cid]>\n";
$clean_filename = self::clean_filename($file["name"], self::MAX_ATTACHMENT_NAME_LEN);
$attach .= "Content-Type: application/octet-stream; name=$clean_filename;\n";
$attach .= "Content-Disposition: inline; filename=$clean_filename;\n";
$attach .= "\n";
$attach .= chunk_split(base64_encode($file['string']), 76, "\n");
$attach .= "\n--$this->boundary";
}
// close email
$attach .= "--\n";
}
but instead of coming as an embedded string its coming as an attachment in mail
Is it possible to send embedded images in amzon ses?

I fixed this problem by providing the image source as base64encoded data. In python i am doing like this:
x = 'data:image/png;base64,{}'.format(base64.b64encode(open('x.png', 'rb').read()).decode())
Hope this will fix your issue and let me know if any issues

Related

Converting XLSX to CSV with Perl while maintaining the encoding

I'm a BI developer working with perl scripts as my ETL - I receive data over email, take the file, parse it and push it into the DB.
Most of the files are CSV, but occasionally I have an XLSX file.
I've been using Spreadsheet::XLSX to convert, but I've noticed that the CSV output comes out with the wrong encoding (needs to be UTF8, because accents and foreign languages).
That's the sub I'm using ($input_file is an Excel file), but I keep getting the data with the wrong characters.
WHAT am I missing?
Thanks a lot all!
sub convert_to_csv {
my $input_file = $_[0];
my ( $filename, $extension ) = split( '\.', $input_file );
open( format_file, ">:**encoding(utf-8)**", "$filename.csv" ) or die "could not open out file $!\n";
my $excel = Spreadsheet::XLSX->new($input_file);
my $line;
foreach my $sheet ( #{ $excel->{Worksheet} } ) {
#printf( "Sheet: %s\n", $sheet->{Name} );
$sheet->{MaxRow} ||= $sheet->{MinRow};
foreach my $row ( $sheet->{MinRow} .. $sheet->{MaxRow} ) {
$sheet->{MaxCol} ||= $sheet->{MinCol};
foreach my $col ( $sheet->{MinCol} .. $sheet->{MaxCol} ) {
my $cell = $sheet->{Cells}[$row][$col];
if ($cell) {
my $trimcell;
$trimcell = $cell->value();
print STDERR "cell: $trimcell\n"; ## Just for the tests so I don't have to open the file to see if it's ok
$trimcell =~ s/^\s+|\s+$//g; ## Just to make sure I don't have extra spaces
$line .= "\"" . $trimcell . "\",";
}
}
chomp($line);
if ($line =~ /Grand Total/){} ##customized for the files
else {
print format_file "$line\n";
$line = '';
}
}
}
close format_file;
}
My knowledge is from using ETL::Pipeline and it uses Spreadsheet::XLSX for reading .xlsx-files.
But I know which fields are UTF-8
I wrote a Local ETL::Pipeline module to handle output for Excel files
use Encode qw(decode encode);
$ra_rec->{name} = decode( 'UTF-8', $ra_rec->{name}, Encode::FB_CROAK );

PHPMailer - How do I unlink files in temp dir.?

I am looping through the files array as shown in the middle chunk of code, rest of the code is just for better context.
$email = new PHPMailer();
$email->From = 'John#example.com';
$email->FromName = 'John Doe';
$email->Subject = $name_talent;
$body = '<strong>ADDRESS</strong> : ' .$address_talent.'<br><br>';
if ($_FILES) {
foreach ($_FILES as $file => $array) {
$tmp_path = $_FILES[$file]['tmp_name'];
$tmp_name = basename($_FILES[$file]['name']);
$email->AddAttachment($tmp_path, $tmp_name);
}
}
$email->Body = $body;
$email->IsHTML(true);
$email->AddAddress( '######gmail.com' );
$email->Send();
This sends the attachments with values in the $body as well. Now, I would like to unlink the files either after the mail is sent or the attachments are successful. As inspired from this answer, If I do it like this below, no files are attached and any variables set for the $body eg. $address_talent do not appear in the email.
if ($_FILES) {
foreach ($_FILES as $file => $array) {
$tmp_path = $_FILES[$file]['tmp_name'];
$tmp_name = basename($_FILES[$file]['name']);
if($email->AddAttachment($tmp_path, $tmp_name)){
unlink("$tmp_path");
}
}
}

Joomla Exporting Or Downloading Reports To CSV

I have the following code in my inventory section when i run this code it save an excel file but the result showing the whole html file not my data.
<?php
$csv = NULL;
$arr = array("product_name","product_sku","product_in_stock","virtuemart_product_id","product_price_display","product_instock_value");
$csv = "Product Name, Product SKU, In Stock, Booked ordered products, Cost Price, Stock Value \n";
$c=0;
while(list($key,$value)=each($arr)){
$c++;
$cc=1;
foreach ($this->inventorylist as $key => $product){
$cc++;
$csv .= join(',',array($product->product_name.",".$product->product_sku.",".$product->product_in_stock.",".$product->virtuemart_product_id.",".$product->product_price_display.",".$product->product_instock_value))." \n";
}
}
JResponse::clearHeaders();
JResponse::setHeader('Content-Type', 'application/vnd.ms-excel', true);
JResponse::setHeader('Content-Disposition', 'csv; filename=inventory_report.csv; size='.strlen($csv), true);
JResponse::sendHeaders();
Any help will be appreciated.
Thanks
Khalique
Here's the brute force solution just to get you going:
header("Content-type: text/csv");
header("Content-Disposition: attachment; filename=file.csv");
header("Pragma: no-cache");
header("Expires: 0");
// Echo the csv content.
echo "foo,bar\n1,2";
// Close the application gracefully.
JFactory::getApplication()->close();
A slightly cleaner way without using views, but including &format=raw in the URL is:
$app = JFactory::getApplication();
$doc = JFactory::getDocument();
$doc->setMimeEncoding('application/vnd.ms-excel');
$app->setHeader(
'Content-disposition',
'attachment; filename="inventory_report.csv"',
true
);
echo "foo,bar\n1,2";
But the best way is to use a download view based on the raw document type. You can find an example of this in com_banners:
Controller: https://github.com/joomla/joomla-cms/blob/staging/administrator/components/com_banners/controllers/tracks.raw.php
View: https://github.com/joomla/joomla-cms/blob/staging/administrator/components/com_banners/views/tracks/view.raw.php
You might also gain some extra insight from this discussion https://groups.google.com/forum/#!topic/joomla-dev-general/dRa_39AGDBY

htaccess url rewrite but the ID changes

I wondering if it is possible to rewrite a url that might look something like this
www.example.com/item.php?id=1 to a www.example/item.php without the `?id=1`
Please note that 1 is for the product id so it might change to 2 or any number depending on what product the user choose
My current htaccess
My current .htaccess looks like this:
RewriteEngine on
RewriteBase /
RewriteRule ^item/(.*)$ test/main/pages/account/$1 [L] // **the item.php file is in the item folder**
ErrorDocument 404 /test/main/pages/general/index.php
Options -Indexes
AuthName "main"
I did it like
<?php
include('global.php'); ///database connection
function create_guid()
{
$microTime = microtime();
list($a_dec, $a_sec) = explode(" ", $microTime);
$dec_hex = dechex($a_dec* 1000000);
$sec_hex = dechex($a_sec);
ensure_length($dec_hex, 5);
ensure_length($sec_hex, 6);
$guid = "";
$guid .= $dec_hex;
$guid .= create_guid_section(3);
$guid .= '-';
$guid .= create_guid_section(4);
$guid .= '-';
$guid .= create_guid_section(4);
$guid .= '-';
$guid .= create_guid_section(4);
$guid .= '-';
$guid .= $sec_hex;
$guid .= create_guid_section(6);
return $guid;
}
$stmt = $conn->prepare("INSERT INTO `Product (`pid`, `Guid`, `price`) VALUES
(13, '".$guid."', 13)");
$stmt->execute();
?>
also i did it using PDO because am I not a big fan of mysql
Since your main motive here to keep people form guessing the id in url and since as pointed out by lucas william that the way you want it is not possible in .htaccess instead you can store the id of each product in the database as guid format(this format of id storage into database is used by sugarCRM) which is also a proper substitute to satisfy you required and you can use that id to uniquely identify you product table each records:
The functions to create guid is as follows:
function create_guid()
{
$microTime = microtime();
list($a_dec, $a_sec) = explode(" ", $microTime);
$dec_hex = dechex($a_dec* 1000000);
$sec_hex = dechex($a_sec);
ensure_length($dec_hex, 5);
ensure_length($sec_hex, 6);
$guid = "";
$guid .= $dec_hex;
$guid .= create_guid_section(3);
$guid .= '-';
$guid .= create_guid_section(4);
$guid .= '-';
$guid .= create_guid_section(4);
$guid .= '-';
$guid .= create_guid_section(4);
$guid .= '-';
$guid .= $sec_hex;
$guid .= create_guid_section(6);
return $guid;
}
function create_guid_section($characters)
{
$return = "";
for($i=0; $i<$characters; $i++)
{
$return .= dechex(mt_rand(0,15));
}
return $return;
}
function ensure_length(&$string, $length)
{
$strlen = strlen($string);
if($strlen < $length)
{
$string = str_pad($string,$length,"0");
}
else if($strlen > $length)
{
$string = substr($string, 0, $length);
}
}
Now using the above function you can generate the id as:
$guid = create_guid(); //guid is of the format 79cb3604-e634-a142-d9cb-5113745b31e2 which you can see is quite impossible to guess.
Also I would sugest that you keep the auto increment field in your product table.
Because it always a good idea to maintain a auto incremented field in a table to uniquely identity the records.
I hope this can be of some help
Edit :
what you need to do is add a field in you database product table named "guid"
so say your current database product table structure has the following fields:
id, name, price //where id is the auto incremented
after adding the field guid it becomes
id, guid, name, price //where id is auto incremented field and guid uniquely identifies each row in the product table
and when you do the insert of the product data in the database product table you generate the guid using the above code and insert it. ie
for example
$sql = "Insert into product_table('guid','product_name',product_price) values('".$guid."','product1','59.00');
so an example data in your product table will look like this:
1, 79cb3604-e634-a142-d9cb-5113745b31e2, product1, 59.00
Now in the product.php page with url say
yoursite.com/product.php?guid=79cb3604-e634-a142-d9cb-5113745b31e2
instead of using the url
yoursite.com/product.php?id=1
you can easily query the data from the database product table in relation to "guid" which of course also uniquely identifies each row in your product table in the database there by elimiting the risk of user guessing your id in url of the webpage.
I hope this gives you an idea of what i am trying to explain.
Using .htaccess, rewrite "www.example.com/item.php?id=1" to "www.example/item.php" is not possible because, how can we know, with an url like "www.example/item.php", if the product id is 1 or 2 ? It's impossible. So, with .htaccess, it's not possible.
However, you can cheat and use, simply, PHP and session variables, to do this rewriting, even if it's not a good solution. So, let your links under their current shape, with the get id parameter, and just add in your item.php file, a condition that will save the id value in a session variable and redirect to item.php if the id parameter is not empty.

Webforms in excel instead of e-mail

A client of mine asked me if i can find a solution for this problem.
His website (still a WIP) http://welkommagazine.nl/luuk/ has a form. The form obviously uses a sendmail script to send the form to e-mail. From thereon he manually copy/pastes all the submissions to excel.
What he wants is that the forms online automaticcaly are added to an excel document to save him a lot of work.
Now i am not a programmer, but a designer.. I think this can be done, but i have absolutely no clue how. I googled alot for it and the only thing i found was a dreamweaverplugin.
Is there a way to do this, if so, how?
Not a programmer's response, but...
I think an easy solution is to use Google docs. You can set-up a Google Spreadsheet and associate a form to it. Whenever a user fills the form , his data is added to the spreadsheet.
Your client may download that anytime.
There are some other providers on the market, some free, some not. E.g: wufoo.com
Found the answer myself. I wrote a PHP code snippet which actually stores the fields comma seperated in a CSV file and sends an email to a desired adress with the filled in fields.
if(isset($_POST['Submit'])){
$pakket = $_POST['pakket'];
$extragidsen = $_POST['extragidsen'];
$naambedrijf = $_POST['naambedrijf'];
$err = '';
if(trim($pakket)==''){
$err .= '-Please enter a name';
}
if(empty($extragidsen)){
$err .= '-Please enter an email address';
}
if(strlen($naambedrijf)==0){
$err .= '-Please enter a comment';
}
if($err!=''){
echo $err;
}
else{
$filename = 'file.csv';
$somecontent = $pakket . ',' . $extragidsen . ',' . $naambedrijf . "\n";
// Let's make sure the file exists and is writable first.
if (is_writable($filename)) {
// In our example we're opening $filename in append mode.
// The file pointer is at the bottom of the file hence
// that's where $somecontent will go when we fwrite() it.
if (!$handle = fopen($filename, 'a')) {
echo "Cannot open file ($filename)";
exit;
}
// Write $somecontent to our opened file.
if (fwrite($handle, $somecontent) === FALSE) {
echo "Cannot write to file ($filename)";
exit;
}
//--------------------------Set these paramaters--------------------------
// Subject of email sent to you.
$subject = 'Inschrijving welkom';
// Your email address. This is where the form information will be sent.
$emailadd = 'luuk#luukratief.com';
// Where to redirect after form is processed.
$url = 'http://www.google.com';
// Makes all fields required. If set to '1' no field can not be empty. If set to '0' any or all fields can be empty.
$req = '0';
// --------------------------Do not edit below this line--------------------------
$text = "Results from form:\n\n";
$space = ' ';
$line = '
';
foreach ($_POST as $key => $value)
{
if ($req == '1')
{
if ($value == '')
{echo "$key is empty";die;}
}
$j = strlen($key);
if ($j >= 20)
{echo "Name of form element $key cannot be longer than 20 characters";die;}
$j = 20 - $j;
for ($i = 1; $i ';
fclose($handle);
} else {
echo "The file $filename is not writable";
}
}
}
Maybe the code aint that clean as it can be, but eh it works.
Feel free to clean up the code if you want to :)
I guessed I would answer this myself for the community...
BTW u need to set "write" rights to "file.csv"
cheers

Resources