Iphone web app download attachment function in php - php

I have a iphone app which use uiwebview to include a mobile site, the mobile site is developed in PHP, some of the page is allow people to download some attachments by using header force download, since the file will be convert to some binary and finally output as a download file by made use of php header function, im wondering will it be work in iphone app uiwebview too?

This site provides a script that they say works.
Here's my code sending vcard as attachement with no message:
//create a boundary string. It must be unique
$random_hash = md5(date('r', time())); $alt_random_hash = md5(date('r', time()+1));
//read the atachment file contents into a string,
//encode it with MIME base64,
//and split it into smaller chunks
$attachment = (chunk_split(base64_encode(file_get_contents($url)));
$filename = "Alain_Leblanc.vcf";
// Write the email message (which will become an attachement)
$headers .= "
MIME-Version: 1.0
From: info#AlainLeblancAssurances.ca
Reply-To: info#AlainLeblancAssurances.ca
Content-Type: text/x-vcard; charset=utf-8; name=$filename; boundary=$random_hash
Content-Disposition: attachment; filename=$filename
Content-Transfer-Encoding: base64
--$random_hash
$attachment
--$random_hash--
";
//send the email
$mail_sent = #mail($_POST['userEmail'], "Fiche contacts d'Alain Leblanc", "", $headers);
// display email result in browser
echo $mail_sent ? "rand: ".$random_hash ."<br/>Alt: ".$alt_random_hash."<br/>".$headers : "Mail failed";

Related

Add Picture In Email Body

i am not too good with PHP and programming but still i am trying to learn it in every possible way. I need help from experts. My code to send email is as below. i am able to send email, but the problem is, picture is not displaying in email body and it's showing a cross sign. any possible idea and suggestion is appreciated.
<?php
function send_email($from, $to, $subject, $message){
$headers = "From: ".$from."\r\n";
$headers .= "Return-Path: ".$from."\r\n";
$headers .= "CC: abc#gmail.com\r\n , xyz#gmail.com\r\n";
$headers .= "BCC: \r\n";
//set content type to HTML
$headers .= "Content-type: text/html\r\n";
if ( mail($to,$subject,$message,$headers) )
{
header("location:contact-us.php?msg=msgsent");
}
else
{
header("location:contact-us.php?msg=notsent");
}
}
//Function ends here
$from_email="";
$to_email="LMN#gmail.com";
$message.="<html><body background='images/bg11.jpg'>";
$message.="<p style=' color: #006789;'>Dear Team,</p> ";
$message.= "<p style=' color: #006789;'>You have received a feedback from $name & below are the details!!! </p>";
$message.= "<img src=images/c22.jpg>";
$message.="</body></html> ";
send_email(from_email,$to_email,$subject,$message);
?>
You are using a relative path:
<img src=images/c22.jpg>
When you open your mail, that path will mean nothing in a mail client and it is highly unlikely to exist on a webmail client. And if it does, it is not your image...
If you have that image stored on a web-server, you should use the absolute path to that image:
<img src="http://www.your-server.com/images/c22.jpg">
You're using a relative link, "images/c22.jpg", but you're sending this html in an email - there's no images/ directory relative to it to link to. If you wish to link to an image on the web, you can include a full url (http://someplace.com/images/bg11.jpg">). By default, those will usually not be displayed either - most email readers will filter them so as to not let you cause it to load remote content (tipping off that the email was opened, attempting funny business with malformated images, etc).
You can also attach an image to the email by setting the type to multipart/mixed and defining a boundary. Then send an image, defining a content-id in its headers, then your html linking to the content id as "cid:id-you-defined". Looks like this once in the email:
Content-Type: multipart/mixed; boundary="NOTUSEDINTEXT"
This text only shows if the reader can't deal with MIME
--NOTUSEDINTEXT
Content-Id: id_for_later
Content-Type: image/jpeg
Content-Transfer-Encoding: base64
/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAYEBQYFBAYGBQYHBwYIChAKCgkJChQODwwQFxQYGBcU
FhYaHSUfGhsjHBYWICwgIyYnKSopGR8tMC0oMCUoKSj/2wBDAQcHBwoIChMKChMoGhYaKCgoKCgo
KCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCj/wAARCAABAAEDASIA
AhEBAxEB/8QAFQABAQAAAAAAAAAAAAAAAAAAAAj/xAAUEAEAAAAAAAAAAAAAAAAAAAAA/8QAFAEB
AAAAAAAAAAAAAAAAAAAAAP/EABQRAQAAAAAAAAAAAAAAAAAAAAD/2gAMAwEAAhEDEQA/AJUAB//Z
--NOTUSEDINTEXT
Content-Type: text/html
<html><h1>Hi</h1><p>Here's an image <img src="cid:id_for_later"></html>
--NOTUSEDINTEXT--
.. but as you see it's slightly involved - might want to see if there's a php library that'll implement that (the mime headers and types) rather than pound them in yourself. But if you feel like dealing with it, inserting them yourself is ok too. And.. here too, some clients will filter them for safety unless you've clicked "Always show images" or something.
You have to upload your image on web server and give the absolute path to that image e.g:
<img src="http://www.yourwbesite.com/images/c22.jpg" alt="logo" />

Sending attachment from file input in form

Hello I have a simple form which collect files. What I mean is that user should be able to put a file into the field and then by submitting the form mail should be sent to the address which is predefined and hardcoded.
here is my form:
<form action='/?page=admin-send' method='post' class='asholder' enctype=\"multipart/form-data\">\n";
<input type='file' name='file' id ='file'/><button name='accept' value='".$ssl->id."' type='submit'>Send</button>
</form>
And now using Php I want to collect this file and put it into email attachment.
$file = $_FILES["file"]["name"];
$filename = basename($file);
$file_size = filesize($file);
$content = chunk_split(base64_encode(file_get_contents($file)));
$uid = md5(uniqid(time()));
$msg = "Hello, this is email with attachment!";
$mail = new HTMLMail();
$mail->from = 'DO NOT REPLY';
$mail->to = 'tstmail#testhost.com';
$mail->subject = 'admin warrning';
$mail->importance = 'Low';
$mail->body = "<P><FONT SIZE=2 FACE=\"Tahoma\">$msg</FONT></P>";
$mail->headers = "From: ".$from."\r\n"
."MIME-Version: 1.0\r\n"
."Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n"
."This is a multi-part message in MIME format.\r\n"
."--".$uid."\r\n"
."Content-Type: application/octet-stream; name=\"".$filename."\"\r\n"
."Content-Transfer-Encoding: base64\r\n"
."Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n"
.$content."\r\n\r\n"
."--".$uid."--";
$mail->send();
And here is the scenario I have problem with:
I put file with name of xyz.pdf which is some document the most important thing is that this document is all right. When I submit my form I am getting the email. Email has attachment which name is xyz.pdf but when I am trying to open this file I am getting the message that the file is borken.
Can anyone point me what I am doing wrong?
I have done the same kind of task, i expect that my answer will help you for sure.
You are letting user 1. upload a file > *2.* process it at your server side using PHP > *3.* and them you are using HTML mail class to send your mail.
The problem is in third step. If the class does not provide you with sufficient function of mail, then it is not worth using. Writing mail HEADERS by yourself poses a lot of complexity and we as a developer should not go that inside.
I recommend you to use PHPMailer 5.1.2, to do this job. This is very simple and is of not more than 80KB (the core files, excluding demos, guides).
With this you can attach multiple files of any mime type(pdf, jpeg, doc etc.) without worrying about headers. This is very simple and i have tested , the attachments are readable at the mailbox.
If you download complete package with demos, you will see how easy it is to implement with PHP.
PHPMailer is the best free php class to send mails on the net(i think) with no bugs.
I haven't used HTML Mail class what you are using, so don't know whether it has functions for attaching files. But writing mail headers is not good. If you are using it by your wish after knowing all this, then i am sorry for posting this answer.

Apple Mail does not display PDF sent using Zend_Mail

My web application creates PDF documents using Zend_Pdf and sends them using Zend_Mail. It also attaches some user uploaded documents (also PDF). The attachments show up in all general used mail programs, except in Apple Mail. The created PDF is about 30 KB and the message is sent using an external mail server.
In Apple Mail the message list shows the message with a paper clip (indicating that it has attachments), but when the message is opened no attachments are visible. When I click 'Details' in the message head, it shows the attachments and the option to save them.
This is the (stripped down) code that sends the e-mail:
<?php
$mail = new Zend_Mail('utf-8');
$mail->setFrom('niels#example.com', 'Niels')
->setSubject('Subject')
->addTo('niels#example.com', 'Niels')
->setBodyHtml('Hi there', 'utf-8', Zend_Mime::ENCODING_8BIT);
$a = new Zend_Mime_Part($pdfContent);
$a->type = 'application/pdf';
$a->filename = 'my_pdf.pdf';
$a->disposition = Zend_Mime::DISPOSITION_ATTACHMENT;
$a->encoding = Zend_Mime::ENCODING_BASE64;
$mail->addAttachment($a);
$mail->setHeaderEncoding(Zend_Mime::ENCODING_BASE64);
$mail->send();
The mail I receive has the following header for the message
Content-Type: multipart/mixed; boundary="=_f6a669390c6713f60a851af814fe897f"
Content-Disposition: inline
Content-Transfer-Encoding: 7bit
MIME-Version: 1.0
The HTML mail content:
--=_f6a669390c6713f60a851af814fe897f
Content-Type: text/html; charset="utf-8"
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable
The attachment content:
--=_f6a669390c6713f60a851af814fe897f
Content-Type: application/pdf
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="my_pdf.pdf"
Is there a way that the attachments show up in the message in Apple Mail? Now some people respond with 'there are no attachments'. Does Apple do some 'smart' things to hide/show the attachments? Or should I use another Content-Disposition etc? I've searched quite some time to find a solution, but am running out of clues.
This is not Zend issue but Apple Mail issue. Install Thunderbird ;-)
Here are few tips:
problem like this can occur if you are sending inline files and attachments at the same time
mail app settings are defaulted to inline attachments
Close Mail
Open Terminal
enter command defaults write com.apple.mail DisableInlineAttachmentViewing -bool yes,
Open Mail and try again
you should not be explicitly specifying encoding in setting body, your mail is initialized with default 'UTF-8' encoding by the way you
you should not be encoding header unless you are sending emails in languages that use not Roman letters-based character set
Try attaching the file inline and
$mail = new Zend_Mail('utf-8');
$mail->setFrom('niels#example.com', 'Niels')
->setSubject('Subject')
->addTo('niels#example.com', 'Niels')
->setBodyHtml('Hi there');
// add attachment
$mail->createAttachment(file_get_contents('my_pdf.pdf'), 'application/pdf', Zend_Mime::DISPOSITION_ATTACHMENT , Zend_Mime::ENCODING_BASE64);
// try sending attachment inline... maybe this will work (not sure if supported by all mail clients)
// $mail->createAttachment(file_get_contents('my_pdf.pdf'), 'application/pdf', Zend_Mime::DISPOSITION_INLINE , Zend_Mime::ENCODING_BASE64);
$mail->send();
The answer that Alex provided was indeed the solution: it is an Apple Mail issue. When the attachment size increases, it shows up in the e-mail. For example: I just sent a PDF document that is bigger (79KB, shows up as 112KB in the message list) and that is visible. If I send the same kind of PDF (but smaller) it is hidden.

generating PDF on the fly using TCPDF

i'm using TCPDF library to generate a PDF (bill) on the fly and send it via email. It all works but i have a weird problem. When i send the email to a gmail account everything is fine, but when i send it to my mail server i get the email with the pdf but when i open it it doesn't open and i get a message "Adobe reader could not open file.pdf because it's either not a supported format or because the file has been damaged." (the pdf in the email is blank).
I save the PDF into a string like so:
$attachment = $pdf->Output("mypdf.pdf","E");
$attachment = chunk_split($attachment);
and send it via email like so:
$header .= "--".$separator.$eol;
$header .= "Content-Type: application/pdf; name='mypdf.pdf'".$eol;
$header .= "Content-Transfer-Encoding: base64".$eol;
$header .= "Content-Disposition= attachment".$eol.$eol;
$header .= $attachment;
i'm sending the email with php mail function.
The funny thing is if i force the download of the pdf, like so:
$attachment = $pdf->Output("mypdf.pdf","D");
the file is OK and opens without a problem! But if i change it back to "E" it doesn't work.
The other weird thing is that some times i can open the pdf (that i got on my mail server) without a problem, but the next time it wont work (even if i send the exact same email).
Does any one have any idea what is going on? I would like to avoid saving the pdf on the local server.
Why you avoid saving pdf file? You can save it with "F" parameter. After mail has sent you can delete it with unlink function
I see some minor flaws:
Content-Disposition= should be Content-Disposition:
You should end your attachment with "--".$separator."--"
I'm not sure, if this fixes your problems. Anyway I think it's quite complicated to create all the headers manually. I use PEARs Mail_Mime for this task since years and you will find a lot of simple ready to use solutions.

Generate csv file from form data on fly then email to address

I have a form that users are able to fill in and want to be able to generate an Excel file and populate it with the data from the form. I then want it to email me the file with the completed data. How can this be done?
This is currently generating the email:
$body = "You have a new member account application from the website, please see the details below:
\n
Membership Type: $group1_field\n\n
Contact Details\n
Organisation: $member_organ_field\n
Address: $member_address_field\n
Postcode: $member_post_field\n
Tel: $member_tel_field\n
Fax: $member_fax_field\n
Email address: $member_email_field\n
Website: $member_website_field\n\n
Contacts \n
Member 1 \n
Name: $member1_name1_field\n
Position: $member1_position1_field\n
Direct Email: $member1_email1_field\n
Direct Tel: $member1_tel1_field\n\n
Member 2 \n
Name: $member1_name2_field\n
Position: $member1_position2_field\n
Direct Email: $member1_email2_field\n\n
Member 3 \n
Name: $member1_name3_field\n
Position: $member1_position3_field\n
Direct Email: $member1_email3_field\n\n
Type Of Organisation: ".$checkbox1results."\n
Type Of Activity: ".$checkbox2results."$member3_other_field\n\n
Summary Description \n
$member_summary_field \n\n
Company Information \n
Total no. of employees (Welsh site): $member4_total_field\n
Turnover (from Welsh site): $member4_turnover_field\n
Date of company formation: $member4__formation\n
Do you export aborad? Where? $member4_export_field\n\n
Member consent: ".$checkbox3results."\n\n
Completed by: $member4_com_field\n
Company position: $member4_com_pos_field\n
Invoice required: $CheckboxGroup4_field\n\n
Please follow up this request asap.\n\n
";
mail($to, $subject, $body);
echo "Thank you for your account application, we will contact you shortly.";
} ?>
The easiest way would be can create a csv, which is readable by Excel.
It's essentially a comma seperated list of values. Each line representing a row.
The first line can optionally be the column headings, eg:
$string = '"Organisation","Address","Postcode"' . PHP_EOL;
$string .= "\"$member_organ_field\",\"$member_address_field\",\"$member_post_field\"" . PHP_EOL;
file_put_contents('myfile.csv', $string); // write file
Be sure to quote field names and values as a stray unquoted , (comma) would mess up your columns.
Other wise there is an PEAR library for write excel files.
You can them email the file as an attachment using the built-in mail function, see example here
Or using Swift Mailer, see example here
UPDATE
This is the general gist of what you need, the code may not be perfect to please read over it and don't copy and paste.
<?php
$random_hash = md5(date('r', time()));
$csvString = "..."; // your entire csv as a string
$attachment = chunk_split(base64_encode($csvString));
$to = "to#me.com";
$from = "from#you.com";
$subject = "my subject";
$body = "... what ever you want to appear in the body";
$headers = "From: $from\r\nReply-To: $from";
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";
$output = "
--PHP-mixed-$random_hash;
Content-Type: multipart/alternative; boundary='PHP-alt-$random_hash'
--PHP-alt-$random_hash
Content-Type: text/plain; charset='iso-8859-1'
Content-Transfer-Encoding: 7bit
$body
--PHP-mixed-$random_hash
Content-Type: text/csv; name=mycsv.csv
Content-Transfer-Encoding: base64
Content-Disposition: attachment
$attachment
--PHP-mixed-$random_hash--";
mail($to, $subject, $output, $headers);
echo "Thank you for your account application, we will contact you shortly.";
?>
There are two options:
Create a CSV file from the data above
Create an Excel spreadsheet from the data above
By default, windows will associate CSV files with Excel (if it's installed) so most people will open a CSV in Excel (if they have it installed).
For each case, read here:
http://www.dreamincode.net/forums/topic/10131-working-with-excel-files-comma-delimited-or-csv/
http://phpexcel.codeplex.com/
It's also worth noting that if you're generating a .xls file then your PHP will need to run on windows with a copy of Excel installed (I wouldn't advise this, and if you want to go with the Excel option then you might want to consider creating .xlsx files with the PHPExcel tool I linked you to, this does not require an installed copy of Excel and so it can be deployed on Linux)
EDIT: To email the files as attachments you can user PHPMailer (an example of how easy this is can be found here, and you can download the class here)
Pear could be useful here. I found: Spreadsheet_Excel_Writer. Basic documentation and usage for it is here.

Categories