I am a complete novice with php, so forgive me if this is something very obvious! I have tried to create an html form on my website which, when submitted, generates an email with a csv attachement which is sent to me with the relevant information.
I have been using code found online (some from this forum) to try to create a solution and seem to be vey close - however there is a problem somewhere. I am receiving 2 emails when submitted - the first is incorrect in that it is missing the senders' email address and the csv has no information - the second email is exactly right however.
This is the code I am using:
<?php
$email=$_POST['email'];
$customer=$_POST['customer'];
$quantity=$_POST['quantity'];
$prefix=$_POST['prefix'];
$itemno=$_POST['itemno'];
$format=$_POST['format'];
$to = "me#myemail.com";
$subject = "Form 01";
//Message Body
$text = "Form 01 attached";
//The Attachment
$cr = "\n";
$data .= "$email" . $cr;
$data .= "$customer" . $cr;
$data .= "$quantity" . $cr;
$data .= "$prefix" . $cr;
$data .= "$itemno" . $cr;
$data .= "$format" . $cr;
$fp = fopen('form01.csv','a');
fwrite($fp,$data);
fclose($fp);
$attachments[] = Array(
'data' => $data,
'name' => 'form_01.csv',
'type' => 'application/vnd.ms-excel'
);
//Generate a boundary string
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
//Add the headers for a file attachment
$headers = "MIME-Version: 1.0\n" .
"From: {$from}\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";
//Add a multipart boundary above the plain message
$message = "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type: text/html; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$text . "\n\n";
//Add attachments
foreach($attachments as $attachment){
$data = chunk_split(base64_encode($attachment['data']));
$name = $attachment['name'];
$type = $attachment['type'];
$message .= "--{$mime_boundary}\n" .
"Content-Type: {$type};\n" .
" name=\"{$name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" ;
}
$message .= "--{$mime_boundary}--\n";
mail($to, $subject, $message, $headers);
?>
I am also trying to add in reCaptcha, this is going in fine except it only seems to be allowing the first email to get through - which is the incorrect one. So I hope if the problem is in the code above and it can be fixed, then this should be solved too.
Thanks in advance!
The code you are giving us is (I guess) incomplete. It is (obviously) not the complete HTML page. I guess this PHP script is at the top of your HTML page right above your form. Am I right?
So when you open the page your PHP script sends the first (empty) email. After the form is submitted and your $_POST array is filed with data the second (and correct) email will be sent.
To fix this you must check if the $_POST array is not empty:
<?php
if (!empty($_POST["email"])) {
//your code here
}
?>
Like this here: If isset $_POST
I've seen the other questions/answers in StackOverflow but I cannot for the life of me get this to work correctly, and I'm really curious to see what I'm doing wrong.
An insurance company has PDFs that they'd like their customers to fill online and submit via their site. The PDF form will be sent via email as a PDF attachment all filled out.
I have a PHP script that sends the PDF, but it's not saving form data (names, addresses, etc.). It's only sending the blank PDF. Here's the code:
<?php
$fileatt = "file-name.pdf";
$fileatt_type = "application/pdf";
$fileatt_name = "file-name.pdf";
$email_from = "From"; // Who the email is from
$email_subject = "Form"; // The Subject of the email
$email_message = "Text.";
$email_to = "someemail#gmail.com"; // Who the email is to
$headers = "De: ".$email_from;
//no need to change anything else under this point
$file = fopen($fileatt,'rb');
$data = fread($file,filesize($fileatt));
fclose($file);
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";
$email_message .= "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type:text/html; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$email_message .= "\n\n";
$data = chunk_split(base64_encode($data));
$email_message .= "--{$mime_boundary}\n" .
"Content-Type: {$fileatt_type};\n" .
" name=\"{$fileatt_name}\"\n" .
//"Content-Disposition: attachment;\n" .
//" filename=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data .= "\n\n" .
"--{$mime_boundary}--\n";
$ok = #mail($email_to, $email_subject, $email_message, $headers);
if($ok) {
echo 'Sent...';
//unlink($fileatt); //NOW WE DELETE THE FILE FROM THE FOLDER pdfs
}
else {
echo 'Error...';
}
?>
I appreciate any input/feedback. I've thought about using PHPMailer but I think this is like 99% done and I'm curious to see what it's missing?
(The PDF file was created with Word then using Adobe Acrobat Pro all the input fields and the submit button was created, the submit button points to the PHP script that sends the PDF)
Facing issue : Try to send email using PHP up to 20 MB data as email attachment all files are .pdf files. I can successfully send a email up to 5 MB but if try send more data than 5 MB i never got email in my inbox and no error is shown by PHP. I have search about this issue on many forum and i try all the solutions they provided. Like settings in php.ini file.
php.ini Settigns :
memory_limit = 256M
max_execution_time = 60
max_file_uploads = 25
post_max_size = 25M
upload_max_filesize = 10M
Code i am using :
$to = $settings->factoring_email;
$from = ucfirst($settings->company_name)."<".$settings->company_billing_email.">";
$subject = ucfirst($settings->company_name).' Invoice Manifest '.$post['inv_date'];
$message = "Please see attached documents.";
$headers = "From: $from";
// boundary
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
// headers for attachment
$headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";
// multipart boundary
$message = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n";
$message .= "--{$mime_boundary}\n";
// preparing attachments
$count = count($files);
for($x=0;$x<$count;$x++){
if(stristr($files[$x],'manifest')) {
$path = BASE_PATH.DS.'docs'.DS.'tmp'.DS.$files[$x];
$name = $files[$x];
}
else {
$path = BASE_PATH.DS.'docs'.DS.'load_info'.DS.$files[$x].'.pdf';
$name = $files[$x].'.pdf';
}
if(file_exists($path)) {
$file = fopen($path,"rb");
$data = fread($file,filesize($path));
fclose($file);
$data = chunk_split(base64_encode($data));
$message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$name\"\n" .
"Content-Disposition: attachment;\n" . " filename=\"$name\"\n" .
"Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
$message .= "--{$mime_boundary}\n";
unset($file, $data);
}
}
// send
$ok = mail($to, $subject, $message, $headers);
if ($ok) {
echo "<p>mail sent to $to!</p>";
} else {
echo "<p>mail could not be sent!</p>";
}
If you're getting smaller mail successfully sent, I'd wager it's more likely an issue with your mail service configuration. Someone on your mail route isn't allowing "big" messages through. Check your reply-to address and see if any bounces are coming back.
You never recieve the email in your inbox, because the email is never sent on the way. The email is rejected either by the SMTP server, that the mail() function contacted, or by the program, that the mail() function spawned.
Which of these two is the case depends on your mail-related PHP configuration.
i want to send mail and mail is going to send but problem is that mail is empty in mail box. What is the problem in my code?
$afile = $_FILES['myFile'];
//Getting info about above taken file
$fileatttype = $afile['type'];
$fileattname = $afile['name'];
$filesize=$afile['size'];
if($fileatttype=="application/octet-stream" or $fileatttype=="text/plain" or $fileatttype=="application/msword" or $fileatttype=="application/pdf")
{
$otherData="**********************Personal Details*********************\n
Position Applied For :$Position\n
First Name :$First\n
Last Name:$LastName\n
Date Of Birth :$DD-$MM-$yy\n
Correspondence Address :$Caddress1 $Caddress2 $Caddress2\n
Permanent Address :$Paddress1 $Paddress2 $Paddress2\n
Contact No(HOME):$Home\n
Mobile :$Mobile\n
E-Mail :$EMail\n
**********************Education*********************\n
Quallification :$Quallification\n
OtherCourse:$OtherCourse\n
**********************Previous Working Details*********************\n
Company Name-$WCompany1\n
Title-$WTitle1\n
CTC-$WCTC1\n
Reasons of Leaving-$WReason1\n
Company Name-$WCompany2\n
Title-$WTitle2\n
CTC-$WCTC2\n
Reasons of Leaving-$WReason2\n
Company Name-$WCompany3\n
Title-$WTitle3\n
CTC-$WCTC3\n
Reasons of Leaving-$WReason3\n
Company Name-$WCompany4\n
Title-$WTitle4\n
CTC-$WCTC4\n
Reasons of Leaving-$WReason4\n
Current CTC:$CCTC\n
Expected CTC:$ECTC\n
Notice Period Required :$NoticePeriod\n
Total no of years experience :$TotalExperience Years\n
Current Location :$Clocation\n
Preferred Location:$Plocation\n
Resume:$Resume\n
**********************Family Details*********************\n
Father / Guardian Name :$Fname\n
Father / Spouse: Occupation:$Focc\n
Kids:$Kids\n
Age of Kids:$AOK\n
";
$to="renu_activa#yahoo.co.in";
$from=$_POST['First'];
$subject=$Position;
$headers = "From: $First";
if($filesize>0)
{
$file = fopen($afile['tmp_name'],'rb');
$data = fread($file,$afile['size']);
fclose( $file );
}
//Making a random number to use afterwards with help of current time
$semi_rand = md5( time() );
//Defining the type of email as Mime email
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
//Appending to headers, telling that its multipart message with text and attachment
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";
//The text part of message in variable $otherData with other details
$message = "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type: text/plain; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$otherData . "\n\n";
//Encoding all the data read from file to "base64" the standard for email attachments
$data = chunk_split(base64_encode($data));
//Appending the file data to the email including the file name etc
$message .= "--{$mime_boundary}\n" .
"Content-Type: {$fileatttype};\n" .
" name=\"{$fileattname}\"\n" .
"Content-Disposition: attachment;\n" .
" filename=\"{$fileattname}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mime_boundary}--\n";
$sentmail=mail($to, $subject, $message, $headers);
if($sentmail)
{
header('location: career-with-us.php');
}
else
{
echo "Mail Not Send Successfully....";
}
}
else
{
echo "Wrong format: Please upload only PDF/DOC/DOCX/TXT file format...";
}
I am not getting why its empty neither textbox value nor file attachment is showing in mail. Its totally empty.
You define a new variable for the body of the email.
$mess = 'Please check it';
$messa = '*****...
I'm assuming you meant
$mess = 'Please check it';
$mess .= '*****...
I'm trying to generate a VCard via PHP, and them email it out to the user. I wrote an initial script with hard-coded data, but the end-result will fill in the VCard from MySQL.
When viewing the VCard side-by-side with a legitimate VCard (downloaded and tested from another site) they look pretty much identical, but when I try and import my generated VCard it shows up with no data. Actually, if I open it on my phone, it doesn't even recognize that it is a vcard, and instead just sends me to a broken Google Doc.
I've borrowed some code from wikipedia for formatting the vcard, and everything seems fine. Do you see any errors in my formatting? I've tried different line breaks - to no avail. Ideas?
Here is the code for my generation / mail:
<?php
$content = "BEGIN:VCARD\r";
$content .= "VERSION:3.0\r";
$content .= "CLASS:PUBLIC\r";
$content .= "FN:Joe Wegner\r";
$content .= "N:Wegner;Joe ;;;\r";
$content .= "TITLE:Technology And Systems Administrator\r";
$content .= "ORG:Wegner Design\r";
$content .= "ADR;TYPE=work:;;21 W. 20th St.;Broadview ;IL;60559;\r";
$content .= "EMAIL;TYPE=internet,pref:__munged__#wegnerdesign.com\r";
$content .= "TEL;TYPE=work,voice:__munged__\r";
$content .= "TEL;TYPE=HOME,voice:__munged__\r";
$content .= "URL:http://www.wegnerdesign.com\r";
$content .= "END:VCARD";
mail_attachment("Joe Wegner.vcf", $content, "__munged__#wegnerdesign.com", "__munged__#wegnerdesign.com", "Wegner Design Contacts", "__munged__#wegnerdesign.com", "Joe Wegner's Contact Info", "");
function mail_attachment($filename, $content, $mailto, $from_mail, $from_name, $replyto, $subject, $message) {
$fileatt_type = "application/octet-stream";
$headers = "FROM: ".$from_mail;
$data = chunk_split(base64_encode($content));
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";
$message .= "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type:text/html; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$message . "\n\n";
$message .= "--{$mime_boundary}\n" .
"Content-Type: {$fileatt_type};\n" .
" name=\"{$filename}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mime_boundary}--\n";
echo "sending message";
mail($mailto, $subject, $message, $headers);
}
?>
Update 1: This makes me more confused, but perhaps it will help you debug. I've downloaded my (bad) generated VCard to my computer, as well as a good VCard downloaded from a different website. As expected, my generated one opens with no data, but the good one works fine. I then created a third empty file with a .vcf extension, and copied the text from my (bad) file into that empty file. I opened that file, and all the data showed perfectly. To test even further, I copied the text from the good VCard file into my bad file, and it still opened with no data. So, it appears it's something about encoding or some other file thing that I don't understand. It's not permissions - that's all identical.
Update 2: I changed my PHP so that it would force me to download the VCard as well as email it. The downloaded file opens perfectly fine, so the error is either happening in how I'm encoding (right word?) the file, or how GMail is interpretting it.
Update 3: Fixed : Figured it out. I'm not sure why this is - because every other tutorial I can find out there says the opposite - but there were a few key changes. First, I changed the encoding on the email from base64 to 8bit, and I changed the attachment content to just be the string passed to the email function (so that it is in 8bit form, not 64). That made the VCard valid and readable on my desktop. To get it to read on my android I had to change the $fileatt_type variable to "text/x-vcard", otherwise Gmail thinks it is a document.
You are missing a \n at the end of each line. If you look at a normal vCard (with notepad++ for example), it has a CR and LF at the end of each line, the one you create only has CR ('\r'). this works for me:
$content = "BEGIN:VCARD\r\n";
$content .= "VERSION:3.0\r\n";
$content .= "CLASS:PUBLIC\r\n";
$content .= "FN:Joe Wegner\r\n";
$content .= "N:Wegner;Joe ;;;\r\n";
$content .= "TITLE:Technology And Systems Administrator\r\n";
$content .= "ORG:Wegner Design\r\n";
$content .= "ADR;TYPE=work:;;21 W. 20th St.;Broadview ;IL;60559;\r\n";
$content .= "EMAIL;TYPE=internet,pref:joe#wegnerdesign.com\r\n";
$content .= "TEL;TYPE=work,voice:7089181512\r\n";
$content .= "TEL;TYPE=HOME,voice:8352355189\r\n";
$content .= "URL:http://www.wegnerdesign.com\r\n";
$content .= "END:VCARD\r\n";
I had to get vcards working in android and iphone recently. I used the following function which is a modified version of the one listed above. This will send vcards that both gmail and mail on the iphone will be able to open. They work in Thunderbird as well.
function mail_attachment($filename, $content, $mailto, $from_mail, $from_name, $replyto, $subject, $message) {
$fileatt_type = "text/x-vcard";
$headers = "FROM: ".$from_mail;
$data = $content;
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";
$message = "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type:text/html; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$message . "\n\n";
$message .= "--{$mime_boundary}\n" .
"Content-Type: {$fileatt_type};\n" .
" name=\"{$filename}\"\n" .
"Content-Transfer-Encoding: 8bit\n" .
"Content-Disposition: attachment;\n" .
" filename=\"{$filename}\"\n\n" .
$data . "\n\n" .
"--{$mime_boundary}--\n";
//echo "sending message";
mail($mailto, $subject, $message, $headers);
}
I wrote a vCard validator based on the RFC which could help. It's not complete, but the cleaned files are at least nicely compatible with the tools and services I've tried (Gmail, gnokii and some others I can't remember). HTH.