codeigniter mail attachment corrupted - php

using codeigniter 1.5.2 framework,i try to upload file send as an attachment in mail but when mail send it shows .dat file and not relevant to file I send.
$attachfilepath = $_FILES['userfile']['tmp_name'];
$attachfilename = $_FILES['userfile']['name'];
$CI->load->library('email');
$CI->email->from($adminemail, 'Client Name');
$CI->email->to($to);
$CI->email->subject($subject);
$CI->email->message($message);
$CI->email->attach($attachfilepath,$attachfilename);
$CI->email->send();
P.S- i heard this versions attachment function is not stable but attachment function is working when give local file

#bhugy I think this is some bus of the codeigniter version.
we cant use temp file save path in this version of codeigniter.but we can use this logic when we use pure php. (may be new version of codeigniter)

I don't think you can attach files in that way.
I can't specifically find the old 1.5.2 code so I cannot 100% confirm this
attach($filename, $disposition = 'attachment')
^^ this is the method signature from the Email library 2.x
attach($attachfilepath, $attachfilename);
^^ You are not doing it correctly.
I think you need to do.
$attachedFileLocation = $attachfilepath . $attachfilename
$CI->email->attach($attachedFileLocation);
^^ you may need to add a forward slash between the 2 so that it creates a full path but that is something you will have to debug.
Hopefully this sorts your issue.
P.S. I would also suggest upgrading to CI 2 because there are not that many breaking changes between the 2 version but many minor issues were addressed.

Related

PhpMailer not embedding image in mail body but attaching

I am sending email using PHPmailer and tying to embed image in mail body (using CID method <img src="cid:qrcode" />) but it always attaching image instead of embedding.Can anyone tell me what's wrong with my code (commented lines in code, already tried. ).
Here is the screenshot of my code
First of all you're using a very old version of PHPMailer, and have based your code on a very old example. Get the latest.
The other obvious problem is that while you're putting HTML into Body, you've commented out the call to isHTML(), so your message is being sent as plain text, which has no concept of displaying images inline. Uncomment this line:
$mail->isHTML();
Also bear in mind that in MIME there is essentially no difference between attachments and inline images - everything is an attachment, it's just that some attachments may be referred to from HTML parts, and HTML-capable clients can make use of that linkage.
If Outlook is removing src attributes, that's clearly not your sending code's problem. Outlook does some very unpleasant things to email.
One other minor thing: instead of dirname(__FILE__) you can use __DIR__ in any current version of PHP.
just as an assistance I encountered the same problem and after much mumbling arrived at a solution which might help others.
Tried dumping the image in the same folder - nope
Tried DIR variable to dynamicalyly pull in the image - nope
Finally, hardcoded the path to the folder in which my embedded image lived, hurrah
Magic forumla for me (using PHPmailer v5.5) - note I'm using Plesk so your definitive path may differ, use mine as a guide...
$mail->AddEmbeddedImage("/var/www/vhosts/{domainname}/httpdocs/{foldername)/image.jpg", "emailimg", "image.jpg");
I note that when calling in the image as an embedded image that i had to use the same filename as I think PHPmailer uses the structure:
embedded-img-name source, reference id, embedded-img-name within the internal AddEmbeddedImage call
Hope it helps someone!
Try this code i think it will work for you
$mail->AddEmbeddedImage('img/2u_cs_mini.jpg', 'logo_2u');
and on the tag put src='cid:logo_2u'

Apigility file upload

I'm new to Zend's Apigility and I have problem with file upload.
I've created a new rest service and configured fields in the admin UI as described in Apigility documentation: https://apigility.org/documentation/recipes/upload-files-to-api
When trying to obtain any data from InputFilter i get only null values.
Resource controller
public function create($data)
{
$inputFilter = $this->getInputFilter();
$data = $inputFilter->getValues();
var_dump($data);
//return $this->attachments->create($data);
}
var_dump result
array(1) {
["filedata"]=>
NULL
}
For testing purposes I'm using Postman extension for Chrome with Content-Type header set to 'multipart/form-data', and attached file to key: filedata.
I'm pretty sure, I can send files using json and base64_encode, but I would rather hold with it until it would be absolutely necessary.
For those who aren't aware, Apigility is a Zend Framework 2 based framework specifically made for Rest/Rpc API's.
To do file uploads, please refer to their documentation on the recent updates as noted by Jon Day.
Credit : https://apigility.org/documentation/recipes/upload-files-to-api
How can you allow uploading files via your API?
Answer
Zend Framework 2 provides a variety of classes surrounding file upload
functionality, including a set of validators (used to validate whether
the file was uploaded, as well as whether it meets specific criteria
such as file size, extension, MIME type, etc.), a set of filters (used
to allow renaming an uploaded file, as well as, more rarely, to
manipulate the contents of the file), and file-upload-specific inputs
for input filters (because validation of files needs to follow
different rules than regular data).
Currently the limitation is that Apigility will only accept multipart/form-data
Using Xdebug I am getting the following out :
$data_array = $inputFilter->getValues();
$image = $data_array['images_data'];
The $image array looks like this :
name = MemeCenter_1400658513231_337.jpg
type = image/jpeg
tmp_name = /tmp/phpzV3mWA
error = 0
size = 379580
Try this
Update apigility with composer. File upload is working in version 1.0.3
Use Postman to send files but with no headers.Just select form-data.It worked for me.
To move uploaded file use rename instead of move_uploaded_file.
You can use the option ('magicFile' => false) for the MimeType validator which fixes the problem without any modification at the zf library.

Digital signed PDF with PHP [duplicate]

I'm trying to build a simple PDF document signing routine, using PHP, openssl, and the Zend framework (for pdf redering/handling).
I've found this, but it simply won't work, Zend is not able to open any pdf's, not even Zend's own test pdf and Zend will not report why, only that it 'cannot'.
I'm pretty sure I would be able to create the keys/certs effectively as that is well documented, but is there a solid approach to attaching the generated certificate to the PDF as the above Zend extension suggests it once did?
function DigiSignPDF ($pdf_sign_request) {
if (get_magic_quotes_gpc()) {
$new_pdf = stripslashes($pdf_sign_request['raw_pdf']);
} else {
$new_pdf = $pdf_sign_request['raw_pdf'];
}
$test_pdf = stripslashes(file_get_contents('path/Some.pdf'));
$test_pdf2 = ('path/Some.pdf');
$pdf = Zend_Pdf::load($new_pdf2);
//below is the signing code, from another library, it works as long as Zend_Pdf works
$certificate = file_get_contents('path/certificate.p12');
$certificatePassword = 'test123';
if (empty($certificate)) {
throw new Zend_Pdf_Exception('Cannot retrieve/generate the certificate.');
}
$pdf->attachDigitalCertificate($certificate,$certificatePassword);
$eSig_pdf = $pdf->render();
file_put_contents('path/signed_pdf.pdf', $eSig_pdf);
}
Edit, adding code: The above only works if I use 'test_pdf2' as the input for Zend_Pdf. It recognizes the cert as binary with no problems, but I need to be able to pass the PDF without ever writing it to disk.
TCPDF supports signing of pdf files. Maybe you find something useful in the source code.
Adding my solution as answer, per halfer's advice: Solved this one, because I was passing the content to Zend_Pdf as a string, i should have been using Zend_Pdf::parse($new_pdf);, as it very likely says in the manual. (oops)
Further; I solved pretty much ALL of my problems with digitally signing PDFs of various versions and form constituents by moving to TCPDF, as several of the articles here suggest. A similar caveat was met with TCPDF though, when using strings, ensure that you are using TCPDF's 'writeHTMLCell' instead of 'writeHTML'. And watch for PHPs 'magic_quotes', errant whitespace, encoding, and goblins.

Digitally signing a PDF, using PHP, Zend, and openssl

I'm trying to build a simple PDF document signing routine, using PHP, openssl, and the Zend framework (for pdf redering/handling).
I've found this, but it simply won't work, Zend is not able to open any pdf's, not even Zend's own test pdf and Zend will not report why, only that it 'cannot'.
I'm pretty sure I would be able to create the keys/certs effectively as that is well documented, but is there a solid approach to attaching the generated certificate to the PDF as the above Zend extension suggests it once did?
function DigiSignPDF ($pdf_sign_request) {
if (get_magic_quotes_gpc()) {
$new_pdf = stripslashes($pdf_sign_request['raw_pdf']);
} else {
$new_pdf = $pdf_sign_request['raw_pdf'];
}
$test_pdf = stripslashes(file_get_contents('path/Some.pdf'));
$test_pdf2 = ('path/Some.pdf');
$pdf = Zend_Pdf::load($new_pdf2);
//below is the signing code, from another library, it works as long as Zend_Pdf works
$certificate = file_get_contents('path/certificate.p12');
$certificatePassword = 'test123';
if (empty($certificate)) {
throw new Zend_Pdf_Exception('Cannot retrieve/generate the certificate.');
}
$pdf->attachDigitalCertificate($certificate,$certificatePassword);
$eSig_pdf = $pdf->render();
file_put_contents('path/signed_pdf.pdf', $eSig_pdf);
}
Edit, adding code: The above only works if I use 'test_pdf2' as the input for Zend_Pdf. It recognizes the cert as binary with no problems, but I need to be able to pass the PDF without ever writing it to disk.
TCPDF supports signing of pdf files. Maybe you find something useful in the source code.
Adding my solution as answer, per halfer's advice: Solved this one, because I was passing the content to Zend_Pdf as a string, i should have been using Zend_Pdf::parse($new_pdf);, as it very likely says in the manual. (oops)
Further; I solved pretty much ALL of my problems with digitally signing PDFs of various versions and form constituents by moving to TCPDF, as several of the articles here suggest. A similar caveat was met with TCPDF though, when using strings, ensure that you are using TCPDF's 'writeHTMLCell' instead of 'writeHTML'. And watch for PHPs 'magic_quotes', errant whitespace, encoding, and goblins.

help with inline images/mail with cron - php?

I've got mailings that need to be sended using cron. When I load the script manualy all works fine. With cron i get broken images.
to change the src of my img i used:
$body = eregi_replace("managersrc_logo","images/managers/acertainlogo.jpg",$body);
Because i thaught that it is importent to use absolute paths i also tried:
$body = eregi_replace("managersrc_logo","http://www.site.com/images/managers/acertainlogo.jpg",$body);
In that case i even do not see the images when i run the cronscript manualy. Nor the automated cron will display me the images.
When i check the source of the mail that is received i always see "cid:encryptedstuff" even if i use absolute paths? Why is that? I just want my absolute paths being printed in the src attribute of the img tag. Who changes my absolute path to cid: ? is it php, phpmailer or outlook itself?
Any help someone?
can you post a sample html before and after being replaced (but before being sent)? maybe it has additional characters that break the url, ie src="/managersrc_logo". Also, maybe your mailing program has an option to integrate the images inside the message and can't find them?
The problem was an older version of phpmailer. I updated to the new version and the images are displayed perfectly now!!

Categories