I am creating a .eml file with the following code to create a "draft" email that can be open as draft in Outlook and ready to be sent.
I got it working 99%, but the email generated has the attachments with .dat extension.
For example, and excel file will be name myfile.xlsx.dat ; if I take the file and remove the .dat extension i can open it in Excel. Same thing for pdf attachment, gets renamed .dat, but remove the .dat and it opens as a valid PDF.
How can I get rid of of the .dat extension?
$part1["type"] = TYPEMULTIPART;
$part1["subtype"] = "mixed";
$fp = fopen($full_filename2, "r");
$contents = fread($fp, filesize($full_filename2));
fclose($fp);
$part2["type"] = TYPEAPPLICATION;
$part2["encoding"] = ENCBINARY;
$part2["subtype"] = "octet-stream";
$part2["description"] = basename($full_filename2);
$part2["contents.data"] = $contents;
$fp = fopen($full_filename3, "r");
$contents = fread($fp, filesize($full_filename3));
fclose($fp);
$part3["type"] = TYPEAPPLICATION;
$part3["encoding"] = ENCBINARY;
$part3["subtype"] = "octet-stream";
$part3["description"] = basename($full_filename3);
$part3["contents.data"] = $contents;
$fp = fopen($full_filename, "r");
$contents = fread($fp, filesize($full_filename));
fclose($fp);
$part5["type"] = TYPEAPPLICATION;
$part5["encoding"] = ENCBINARY;
$part5["subtype"] = "octet-stream";
$part5["description"] = basename($full_filename);
$part5["contents.data"] = $contents;
$part4["type"] = TYPETEXT;
$part4["subtype"] = "html";
$part4["description"] = "body";
$part4["contents.data"] = $email_body;
$body[1] = $part1;
$body[2] = $part2;
$body[3] = $part3;
$body[4] = $part4;
$body[5] = $part5;
$singlefile = uniqid(rand(), true) . '.eml';
$imapfile = 'C:\\wamp\\www\\billing\\emails\\'.$singlefile;
file_put_contents($imapfile , imap_mail_compose($envelope, $body));
Related
How would I save an array to a binary file and then read that binary file back to an array in php?
This is where I am so far, but it doesn't work:
$arr = array("key1"=>"val1","key2"=>"val2","key3"=>"val3");
//save file
$file_w = fopen('binint', 'w+');
$bin_str = pack('i', $arr);
fwrite($file_w, $bin_str);
fclose($file_w);
//load file
$filename = "file.bin";
$handle = fopen($filename, "rb");
$contents = fread($handle, filesize($filename));
fclose($handle);
$newarr = unpack('i*', $contents);
print_r($newarr);
I am trying to make a script to take a downloadable file and put it onto my ftp server to then update a database. I cannot figure out what the url extention should be to able to make this work.
<?php
$src = fopen('https://www.atf.gov/firearms/docs/0516-ffl-listxlsx/download', 'r');
$dest1 = fopen('first1k.xlsx', 'w');
$dest2 = fopen('remainder.xlsx', 'w');
echo stream_copy_to_stream($src, $dest1, 19759460) . " bytes copied to first1k.txt\n";
echo stream_copy_to_stream($src, $dest2) . " bytes copied to remainder.txt\n";
?>
This is where I am trying to get the data from https://www.atf.gov/firearms/listing-federal-firearms-licensees-ffls-2016
<?php
$file = "0516.xlsx";
$url = 'https://www.atf.gov/firearms/docs/0516-ffl-listxlsx/download' ;
$handle = fopen("0516.xlsx", "w+");
fwrite($handle, file_get_contents($url));
rewind($handle);
$read = htmlentities(fread($handle, filesize($file)));
?>
fixed it out
Now I am fed up with this issue, have been trying to fix it since 2-3 days.
The problem:
I am downloading certain images, and then writing them on to disk with php script.
The images are high resolution may be around 7000 pixels. The data is being downloaded properly.
When I write this data on to file, some time 1 image gets write, some times 2,3 etc.
The script is not showing any error and just breaks.
I don't have access to server logs and can't check those either.
It breaks after curl_get_contents , means where I write file, if I comment that section it works properly.
Below is the code:
<?php
ini_set ("display_errors", "1");
error_reporting(E_ALL);
ini_set('max_execution_time', 3000);
include_once("../config.php");
include_once("../utils.php");
$DIR = "../wallpapers";
$URL = "Website url";
$info = array();
$dom = new domDocument;
#$dom->loadHTML(file_get_contents($URL));
$dom->preserveWhiteSpace = false;
$links = $dom->getElementsByTagName('img');
$i = 0;
foreach ($links as $tag){
$iurl = $tag->getAttribute('src');
$lastHyphenAt = strrpos($iurl, "-");
$iurl = substr ($iurl, 0, $lastHyphenAt).".jpg";
$info[$i]["url"] = $iurl;
$info[$i]["name"] = basename($iurl);
$i++;
}
foreach($info as $item){
$url = $item["url"];
$name = $item["name"];
if(!file_exists($DIR."/".$name)){
echo "Downloading: ".$url."<br><br>";
$data = curl_get_contents($url);
$file = fopen($DIR."/".$name,"w") or die('Cannot open file: '.$my_file);
fwrite($file,$data);
fclose($file);
}else
echo "Exists ".($DIR."/".$name)."<br><br>";
}
?>
you can write high resolution images by applying chunk functionality, check below code for the same :-
$chunk = 102400;
$filePointer = fopen($url, "rb");
if ($filePointer!=false){
while (!feof($filePointer))
{
if($chunk<TOTALFILESIZE)
{
$fileData = fread($filePointer, 102400); //102400 is chunk size
$myFile = $DIR."/".$_REQUEST['filename'].'.'.$_REQUEST['ext']; //store the file into temp. location
chmod($myFile, 0777);
$fp = fopen($myFile, 'a+');
fwrite($fp, $fileData);
fclose($fp);
}
}
}
PHP script who open and search data from .txt is:
function explodeRows($data) {
$rowsArr = explode("\n", $data);
return $rowsArr;
}
function explodeTabs($singleLine) {
$tabsArr = explode("\t", $singleLine);
return $tabsArr;
}
$filename = "/txt/name.txt";
$handle = fopen($filename, 'r');
$data = fread($handle, filesize($filename));
$rowsArr = explodeRows($data);
for($i=0;$i<count($rowsArr);$i++) {
$lineDetails = explode("|",$rowsArr[$i]);
if ($kodas == $lineDetails[2]) {
$link3=$lineDetails[4];
echo "";
} }
fclose($handle);
It's works well, but now I transfer name.txt to another folder (folder name txt). How to make, first open this folder and search open name.txt
$filename = "txt/name.txt";
$handle = fopen($filename, 'r');
$data = fread($handle, filesize($filename));
$rowsArr = explodeRows($data);
I recently had an issue... I wanted to transfer my contacts fro my LG U990 Viewty to my iPhone 3GS
I exported the contacts from my LG to a vCard File containing all the addresses all in one as
BEGIN:VCARD
VERSION:2.1
N;CHARSET=UTF-8:A;
TEL;CELL;CHARSET=UTF-8:*121#
REV:20120720T081000Z
END:VCARD
Now the above format repeated itself for all contacts... in that single file...
I wanted to convert this single file to original vcf files... not knowing that they could not be imported to iPhone also :(
Actualy Solution : I needed to upload the original bulk vCard file to a new gmail's accounts contacts and sync my contacts to that list in iTunes... which finally I did
However, I made this code which is in PHP generally available as a paid software which people buy to decipher the contacts... here is the below code... FREE
contacts.txt is that file ... open that vCard file in text editor and copy the contents and make this contacts.txt file
$filename = "contacts.txt";
$fd = fopen ($filename, "r");
$contents = fread ($fd,filesize ($filename));
fclose ($fd);
$delimiter = "BEGIN:VCARD";
$splitcontents = explode($delimiter, $contents);
$counter = "";
$write = "";
$finalName = "";
foreach ( $splitcontents as $color )
{
$counter = $counter+1;
$first = strpos($color, "UTF-8:");
$second = strpos($color, "TEL;");
$name = substr($color, $first+6, $second-$first-7);
$wordChunks = explode(";", $name);
for($i = 0; $i < count($wordChunks); $i++)
{
if(trim($wordChunks[0])!="" || trim($wordChunks[1])!="" )
$finalName .= " ".$wordChunks[$i];
}
$finalName= trim($finalName);
$fileName = $finalName.".vcf";
$ourFileHandle = fopen($fileName, 'w') or die("can't open file");
$stringData = "BEGIN:VCARD ".$color;
fwrite($ourFileHandle, $stringData);
fclose($ourFileHandle);
$finalName = "";
}
this will finally make miltuple .vcf files of format given above...
MY QUESTION : WHAT I DID WAS VERY SIMPLE AND HAS LOOPHOLES - CAN WE DO THE ABOVE ITERATION AND FILTERING IN A PHP REGULAR EXPRESSION ?
it would be a great learning ?
Read the file and get a string of the context
NSData *String_Data = [NSData dataWithContentsOfFile:yourfilepath];
NSString *theString = [[NSString alloc] initWithData:String_Data encoding:NSUTF8StringEncoding];
Get array of all the lines in the context
NSArray *lines = [theString componentsSeparatedByString:#"\n"];
Run for Loop through each line: Check prefix for BEGIN: or END:
NSString * stringForVCard=#"";
for (int i=0; i<[lines count];i++){
[stringForVCard stringByAppendingString:[lines objectAtIndex:i]
if ([line hasPrefix:#"END"])
[stringForVCard writeToFile:[NSString stringWithFormat:#"Make your own file path for each VCF"] atomically:TRUE encoding:NSUTF8StringEncoding error:nil ];
//Empty the string
stringForVCard=#"";}
file structure:
index.php
contacts.vcf
a(folder)
$filename = "contacts.vcf";
$file = file($filename);
$i=1;
$content = "";
foreach ( $file as $line )
{
if($line == 'BEGIN:VCARD'."\r\n"){
$content = "";
}
$content .= $line;
if($line == 'END:VCARD'."\r\n"){
$fileName = $i.".vcf";
$ourFileHandle = fopen('a/'.$fileName, 'w') or die("can't open file");
fwrite($ourFileHandle, $content);
fclose($ourFileHandle);
$i++;
echo '<br>' . $i .'
';
}
}