Jump to content

generating a vcard file with a form submission (and e-mailing it)


mktgbill

Recommended Posts

I'm using dreamweaver with an extension (Kaos Mailer) to e-mail a submitted form's content to our e-mail address.

What I'd like to try to do is use the submitted data to generate a vCard (vcf) file for importing into Outlook and our contact manager package.  Though I don't consider myself an expert on this I'm pretty sure I can work up the PHP code generate the vCard file but would be stuck on how to attach it to the e-mail we receive.

Has anyone here done this (or something similar) before?
Link to comment
Share on other sites

don't know where I found this, but here you go:

NOTE: it works for me, but hollar if it doesn't work for you (as it may lose or gain something in this post).

[code=php:0]<?php
ini_set("memory_limit","128M");
$thisfile = 'backup.zip';
$file_content =file_get_contents($thisfile);
$encoded = chunk_split(base64_encode($file_content));
$headers = 'MIME-Version: 1.0' . "\n";
$headers .= "Content-Type: multipart/mixed;
   boundary=\"----MessageBoundary\"\n";

$message = '

This is a multi-part message in MIME format.

------MessageBoundary
Content-Type: text/plain;
       charset="us-ascii"
Content-Transfer-Encoding: 7bit

Hello

Backup file is attached.

Regards

------MessageBoundary
Content-Type: application/octet-stream;  name="';

$message .= "$thisfile";
$message .= '"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="';
$message .= "$thisfile";
$message .= '"

';
$message .= "$encoded";
$message .= '

------MessageBoundary--

';

// now send the email
mail("name@example.com","subject", $message, $headers);

?>
[/code]
Link to comment
Share on other sites

  • 2 months later...
Michael, thanks for the help. The form has been working great and it's nice to not have to input the data into Outlook.

Now I'm wondering how I'd go about adding a second attachment with delimited data to input into my contact management package. Generating a delimited text document is the easy part.

Any thoughts on how to accomplish a second attachment? 
Link to comment
Share on other sites

Michael: Again, thanks for your input. Here's what I have and it's working to a certain extent....

**************************************************************************
<?php
// Email Message content
$message_content = "MESSAGE CONTENT GOES HERE";
$message_to = "ME@MYEMAIL.com";
$message_subject = "MESSAGE SUBJECT";

// message assembly vcf attachment
$thisfile = $vcf_dir.$vcf_filename ;
$file_content =file_get_contents($thisfile);
$encoded = chunk_split(base64_encode($file_content));
// second attachment txt doc
$thisfile2 = $txt_dir.$txt_filename ;
$file_content2 =file_get_contents($thisfile2);
$encoded2 = chunk_split(base64_encode($file_content2));
// headers
$headers = 'MIME-Version: 1.0' . "\n";
$headers .= "Content-Type: multipart/mixed;
  boundary=\"----MessageBoundary\"\n";
// additional headers
$headers .= "From:".$_POST['email'];

$email_message = '

This is a multi-part message in MIME format.

------MessageBoundary
Content-Type: text/plain;
      charset="us-ascii"
Content-Transfer-Encoding: 7bit

'.$message_content.'


------MessageBoundary
Content-Type: application/octet-stream;  name="';

$email_message .= "$vcf_filename";
$email_message .= '"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="';
$email_message .= "$vcf_filename";
$email_message .= '"

';
$email_message .="$encoded";
$email_message .= '

------MessageBoundary
Content-Type: application/octet-stream;  name="';

$email_message .= "$txt_filename";
$email_message .= '"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="';
$email_message .= "$txt_filename";
$email_message .= '"
';
$email_message .= "$encoded2";
$email_message .= '

------MessageBoundary--

';

// do the mailing
mail($message_to, $message_subject, $email_message, $headers);
header ("location: RECEIVED.php");
unlink ($thisfile);
// unlink ($thisfile2);
?>
*******************************************************************
I've commented out the second unlink statement (just above) for testing purposes.
The txt document gets generated properly (as I can read the content) but as attachment it comes through as an empty document.

I have a feeling I'm messing up in the encode/chunk_split portion of the code.  Any suggestions?

Link to comment
Share on other sites

OK, I've changed the text document to a CSV document and the multiple attachments seem to work. CSV is fine for me for this planned use (importing into a database). I'm assuming there is something different about the way the chunk_split / encode needs to be stated though I've not found any resources for how to specify for proper chunk_split / encode for a text only document. -- hope this last part makes sense -- if anyone has a way to deal with TXT files please post here. 

Anyway here's my code that's working right now.
***************************************************
<?php
// this file gets named something like mailer.php and is referenced in your form with something like <form action="mailer.php" method="POST" ....> $_POST data below is from the form

$message_to = "me@mydomain.com";
$message_subject = "some subject for the message"; // here's it's also valuable to customize based on form submitted data like request type (billing, tech support, complaint) etc.

$removes = array("\n", "\r");
$posted_message = str_replace($removes, "  ", $_POST['message']); // replaces new lines and returns with space
$posted_message = str_replace(",", ";", $posted_message); // replace the commas for the csv file - feel free to rename this variable for csv only use

/// Generate and save a vcard
$vcf_dir = "saved_files/vcf/"; // make sure you've got write privileges here
$vcf_filename = $_POST['first_name']." ".$_POST['last_name'].".vcf";
$vcf_content = ("BEGIN:VCARD \nVERSION:2.1 \nN:".$_POST['last_name']."; ".$_POST['first_name']." \nFN:".$_POST['first_name']." ".$_POST['last_name']."\nORG:".$_POST['company']." \nTITLE:".$_POST['Title']." \nNOTE;ENCODING=QUOTED-PRINTABLE:".$posted_message." \nTEL;WORK;VOICE:".$_POST['phone']." \nTEL;CELL;VOICE:".$_POST['cellular']." \nTEL;WORK;FAX:".$_POST['fax']." \nADR;WORK:;".$_POST['city'].";".$_POST['address1'].",".$_POST['address2'].";".$_POST['city'].";".$_POST['state'].";".$_POST['zip'].";".$_POST['country']." \nURL;WORK:".$_POST['website']."\nEMAIL;PREF;INTERNET:".$_POST['email']." \nREV: \nEND:VCARD \n") ;
$vcf_go = fopen($vcf_dir."/".$vcf_filename,"w");
fwrite($vcf_go, $vcf_content);
fclose($vcf_go);

/// Generate and save a delimited text file - remove/replace commas used in message field
$csv_dir = "saved_files/csv";  // make sure you've got write privileges here
$csv_filename = $_POST['first_name']." ".$_POST['last_name'].".csv";
$csv_content = ($_POST['last_name'].",".$_POST['first_name'].",".$_POST['first_name']." ".$_POST['last_name'].",".$_POST['company'].",".$_POST['Title'].",".$posted_message.",".$_POST['phone'].",".$_POST['cellular'].",".$_POST['fax'].",".$_POST['address1'].",".$_POST['address2'].",".$_POST['city'].",".$_POST['state'].",".$_POST['zip'].",".$_POST['country'].",".$_POST['email'])." \n " ; // add here any other form fields you would like to keep track of that correspond to your database fields - and of course add them to your form
$csv_go = fopen($csv_dir."/".$csv_filename,"w");
fwrite($csv_go, $csv_content);
fclose($csv_go);


// Email Message content
$message_content = "Name=              ".$_POST['first_name']." ".$_POST['last_name']." \nTitle=              ".$_POST['Title']." \nCompany=            ".$_POST['company']."  \nWeb Site=          ".$_POST['website']." \nOwner=              ".$_POST['Owner']." \nAddress 1=          ".$_POST['address1']." \nAddress 2=          ".$_POST['address2']." \nCity=              ".$_POST['city']." \nState=              ".$_POST['state']." \nZip=                ".$_POST['zip']." \nCountry=            ".$_POST['country']." \nPhone=              ".$_POST['phone']." \nFax=                ".$_POST['fax']." \nCellular=          ".$_POST['cellular']." \n\nEmail=        ".$_POST['email']."message=".$-POST['message']." ";

// message assembly vcf attachment
$thisfile = $vcf_dir.$vcf_filename ;
$file_content =file_get_contents($thisfile);
$encoded = chunk_split(base64_encode($file_content));

// second attachment csv doc
$thisfile2 = $csv_dir.$csv_filename ;
$file_content2 =file_get_contents($thisfile2);
$encoded2 = chunk_split(base64_encode($file_content2));

// headers
$headers = 'MIME-Version: 1.0' . "\n";
$headers .= "Content-Type: multipart/mixed;
  boundary=\"----MessageBoundary\"\n";

// additional headers
$headers .= "From:".$_POST['email'];

$email_message = '

This is a multi-part message in MIME format.

------MessageBoundary
Content-Type: text/plain;
      charset="us-ascii"
Content-Transfer-Encoding: 7bit

'.$message_content.'

------MessageBoundary
Content-Type: application/octet-stream;  name="';

$email_message .= "$csv_filename";
$email_message .= '"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="';
$email_message .= "$csv_filename";
$email_message .= '"

';
$email_message .= "$encoded2";
$email_message .= '

------MessageBoundary
Content-Type: application/octet-stream;  name="';

$email_message .= "$vcf_filename";
$email_message .= '"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="';
$email_message .= "$vcf_filename";
$email_message .= '"

';
$email_message .="$encoded";
$email_message .= '

------MessageBoundary--
';

// do the mailing
mail($message_to, $message_subject, $email_message, $headers);
header ("location: thanks.php");
unlink ($thisfile); // to keep the file on the server simply comment out this line
unlink ($thisfile2); // to keep the file on the server simply comment out this line
?>
*************************************************************

I customize the message subject depending on the input for the form including a "scoring system" to determine which emails are best for me to respond to right away. You could even redirect to a different e-mail address depending on a form submitted value (billing, tech support, etc.).


Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.