So:
// Setup mail class, recipients and body
$mailer->AddAttachment('/home/mywebsite/public_html/file.zip', 'file.zip');
The AddAttachment function has four arguments:
AddAttachment(PATH_TO_FILE, FILENAME, ENCODING, HEADER_TYPE)
I used to use xmail() and when I added a attachment here, I passed the filename and the content, that should be in it.
Like this:
$xmail->addAttachment('myamazingfile.pdf', $content);
How can I make it work the same way, so when i call AddAttachment() from the PHPmailer class, I can either pass the same or something like it, so I dont need to have a actual file on my server to send?
AddStringAttachment($string,$filename,$encoding,$type)
eg
$mail = new PHPMailer();
$mail->AddStringAttachment($string,$filename,$encoding,$type);
https://phpmailer.github.io/PHPMailer/classes/PHPMailer-PHPMailer-PHPMailer.html#method_addStringAttachment
since that AddAttachment() function is expecting a path rather than byte data, you should do a php convert to temp file function and then pass that path string into your function
$prefix = 'ConvertMediaArgs_'.time().'_';
$tempfile = tempnam( $this->tempdir, $prefix );
// Args file create failure: kill script with TEMPFILEFAIL error
if($tempfile === false) {
die('file could not be created');
}
// Write args as Key=Val (\n) to file
$fullpath = $this->tempdir.$tempfile;
$content = $someContent // <---------------- this is your file's data
$handle = fopen( $tempfile, "w");
fwrite( $handle, $content );
// $fullpath is the path you wanna pass to your function
$xmail->addAttachment( $fullpath, $content );
Related
I have two scripts: one of them writes the value of a variable to a file. In another script, I try to read it. It is written without problems, but it is not readable.
Here I write to a file:
$peer_id=2000000001;
$fileLocation = getenv("DOCUMENT_ROOT") . "/peer_id.txt";
$file = fopen($fileLocation,"a+");
fwrite($file, $peer_id);
fclose($file);
Here I read the file:
$fileLocation = getenv("DOCUMENT_ROOT") . "/peer_id.txt";
$file = fopen($fileLocation,"r");
if(file_exists($fileLocation)){
// Result is TRUE
}
if(is_readable ($file)){
// Result is FALSE
}
// an empty variables, because the file is not readable
$peer_id = fread($file);
$peer_id = fileread($file);
$peer_id = file_get_contents($file);
fclose($file);
The code runs on "sprinthost" hosting, if that makes a difference. There are suspicions that this is because of that hosting.
file_get_contents in short runs the fopen, fread, and fclose. You don't use a pointer with it. You should just use:
$peer_id = file_get_contents($fileLocation);
That is the same for is_readable:
if(is_readable($fileLocation)){
// Result is FALSE
}
So full code should be something like:
$fileLocation = getenv("DOCUMENT_ROOT") . "/peer_id.txt";
if(file_exists($fileLocation) && is_readable($fileLocation)) {
$peer_id = file_get_contents($fileLocation);
} else {
echo 'Error message about file being inaccessible here';
}
The file_get_contents has an inverse function for writing; https://www.php.net/manual/en/function.file-put-contents.php. Use that with the append constant and you should have the same functionality your first code block had:
file_put_contents($fileLocation, $peer_id, FILE_APPEND | LOCK_EX);
I am uploading files that I need to attach to email via office365 API.
What I need is the content of the file in a variable WITHOUT storing/saving the file, how can I do that?
foreach ($request->filesToUpload as $file) {
$originalName = $file->getClientOriginalName();//working
$content = $file->getContent();//<- I need this, but not working
//$this->addAttachment($content, $originalName) //logic for later
}
Access the contents of the file like so:
$content = file_get_contents(Input::file('nameAttribute')->getRealPath());
or in other words inside that loop
$contents = file_get_contents($file->getRealPath());
Get the real path with object methods, and you may interact with it like any other file.
Waqas Bukhary,
You get that result, beucause, getContent is not a method of uploadedFiles, check the documentation.
But you have the path, so you can always read the content, like:
$path = $file->path();
$handle = fopen($path, 'r');
$content = fread($handle, filesize($path);
fclose($handle);
You can also use the Request File method if you know the name of the file field, check it here.
So:
// Setup mail class, recipients and body
$mailer->AddAttachment('/home/mywebsite/public_html/file.zip', 'file.zip');
The AddAttachment function has four arguments:
AddAttachment(PATH_TO_FILE, FILENAME, ENCODING, HEADER_TYPE)
I used to use xmail() and when I added a attachment here, I passed the filename and the content, that should be in it.
Like this:
$xmail->addAttachment('myamazingfile.pdf', $content);
How can I make it work the same way, so when i call AddAttachment() from the PHPmailer class, I can either pass the same or something like it, so I dont need to have a actual file on my server to send?
AddStringAttachment($string,$filename,$encoding,$type)
eg
$mail = new PHPMailer();
$mail->AddStringAttachment($string,$filename,$encoding,$type);
https://phpmailer.github.io/PHPMailer/classes/PHPMailer-PHPMailer-PHPMailer.html#method_addStringAttachment
since that AddAttachment() function is expecting a path rather than byte data, you should do a php convert to temp file function and then pass that path string into your function
$prefix = 'ConvertMediaArgs_'.time().'_';
$tempfile = tempnam( $this->tempdir, $prefix );
// Args file create failure: kill script with TEMPFILEFAIL error
if($tempfile === false) {
die('file could not be created');
}
// Write args as Key=Val (\n) to file
$fullpath = $this->tempdir.$tempfile;
$content = $someContent // <---------------- this is your file's data
$handle = fopen( $tempfile, "w");
fwrite( $handle, $content );
// $fullpath is the path you wanna pass to your function
$xmail->addAttachment( $fullpath, $content );
How to write in between content of existing file?
I tried to use fseek() to seek a new position and write the new content, but it replaces old content to the new string after seek position.
My aim is to put new contents after 5 characters of existing file.
Old content: AAAAABBBBB, desired content: AAAAAnewcontentBBBBB
$file_handler = fopen('putty.log','w');
$new_content = 'this is new content';
fseek($file_handler,5);
echo ftell($file_handler); //5
fwrite($file_handler,$new_content);
old content replaced with NULLNULLNULLNULLNULLthis is new content
You can't do it that way.
You can only truncate the content with ftruncate and write later the old content
A not so clean example
<?php
$file_handler = fopen('putty.log','rb+');
$new_content = 'this is new content';
fseek($file_handler,5);
$restOfContent = fread($file_handler,filesize('putty.log')-5);
ftruncate($file_handler,5);
fseek($file_handler,5);
echo ftell($file_handler); //5
fwrite($file_handler,$new_content);
fwrite($file_handler,$restOfContent);
fclose($file_handler)
Load contents to variable using file_get_contents().
Do this on your buffer.
Save your contents to this file using file_put_contents();
if you're using fseek() to write data to a file, remember to open the file in "r+"
mode, example:
$fp=fopen($filename,"r+");
Writing in the middle of the file won't cause it to stretch and push the content forward (like when inserting content in a text editor) but rather to overwrite the content at the position you start writing.
What you need to do is:
read the first part of the old data until the point where you need to write the new content & write that part to a temporary file.
write the new content to the temporary file (the stuff you wish to add).
read the rest of the content from the old file & write it to the temporary file.
delete the old file.
rename the temporary file to the name of the old file.
Example:
$original_file_name = '/tmp/putty.log';
$temp_file_name = '/tmp/putty.tmp';
$temp_file = fopen( $temp_file_name, 'w' );
$file_handler = fopen( $original_file_name, 'r' );
$old_data_size = 5;
fwrite( $temp_file, fread( $file_handler, $old_data_size ) );
$new_content = 'this is new content';
fwrite( $temp_file, $new_content, strlen( $new_content ) );
fwrite( $temp_file, fread( $file_handler, filesize( $original_file_name ) - $old_data_size ) );
fclose( $file_handler );
fclose( $temp_file );
unlink( $original_file_name );
rename( $temp_file_name, $original_file_name );
Make sure that putty.log has read/write permissions for the user used by your webserver (apache/lighttpd etc.) process or that it's accessible for everyone (not recommended).
I'm trying to send a .CSV file with PHP. The file is written to disk before it's sent but when I try to attach the file with file_get_contents(); the structure of the .CSV isn't preseved yet when try and send the file that's created before it's sent I get a resource id (#183) so how can i attach a file which the user can open as a .CSV file? I've made sure the mime type and headers are correct
EDIT
if(!file_exists(_PS_ORDERS_DIR_.$orderDate.'/'.$file_name.'.csv'))
{
if($file = fopen (_PS_ORDERS_DIR_.$orderDate.'/'.$file_name.'.csv', 'x+'))
{
foreach ($list as $fields)
{
fputcsv($file, $fields);
}
$attachment['mime'] = 'application/vnd.ms-excel';
$attachment['content'] = file_get_contents(_PS_ORDERS_DIR_.$orderDate.'/'.$file_name.'.csv');
$attachment['name'] = $order.'order';
Mail::Send(1, 'order_conf', 'Order CSV Attachment', $success, 'dan.farr#gmail.com', CakeToppers, NULL, NULL, $attachment);
return true;
}
If you are using Swift Mailer, there is no need for file_get_contents(), you can just attach the file directly.
From the Swift Mailer documentation:
//Create the attachment
// * Note that you can technically leave the content-type parameter out
$attachment = Swift_Attachment::fromPath('/path/to/image.jpg', 'image/jpeg');
//Attach it to the message
$message->attach($attachment);
So for you that would be:
$attachment = Swift_Attachment::fromPath(_PS_ORDERS_DIR_.$orderDate.'/'.$file_name.'.csv', 'application/vnd.ms-excel');
//Attach it to the message
$message->attach($attachment);