Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mohsinrasool/b7d80a593f5e32b4faa6a4b096004579 to your computer and use it in GitHub Desktop.
Save mohsinrasool/b7d80a593f5e32b4faa6a4b096004579 to your computer and use it in GitHub Desktop.
VCF attachment in Contact Form 7
/**
* Generate VCF from form's data and send as attachment for admin, in Contact Form 7
*
*/
add_action('wpcf7_before_send_mail', 'wpcf7_add_attachment');
function wpcf7_add_attachment($contact_form) {
global $_POST;
$submission = WPCF7_Submission::get_instance();
$vcf_dir = WP_CONTENT_DIR.'/uploads/2017/04/';
if ( $submission ) {
$posted_data = $submission->get_posted_data();
$card_data['first_name'] = $posted_data['first-name'];
$card_data['last_name'] = $posted_data['last-name'];
$card_data['company'] = $posted_data['your-company'];
$card_data['email1'] = $posted_data['your-email'];
$card_data['office_tel'] = $posted_data['your-phone'];
$card_data['title'] = $posted_data['your-title'];
$card_data['display_name'] = $card_data['first_name'] . ' ' . $card_data['last_name'];
$card = "BEGIN:VCARD\r\n";
$card .= "VERSION:3.0\r\n";
$card .= "CLASS:PUBLIC\r\n";
$card .= "PRODID:-//class_vCard from WhatsAPI//NONSGML Version 1//EN\r\n";
$card .= "REV:" . date('Y-m-d H:i:s') . "\r\n";
$card .= "FN:" . $card_data['display_name'] . "\r\n";
$card .= "N:"
. $card_data['last_name'] . ";"
. $card_data['first_name'] . ";"
. '' . ";"
. '' . ";"
. ''. "\r\n";
$card .= "TITLE:" . $card_data['title'] . "\r\n";
$card .= "ORG:" . $card_data['company'];
$card .= "EMAIL;type=INTERNET,pref:" . $card_data['email1'] . "\r\n";
$card .= "TEL;type=WORK,voice:" . $card_data['office_tel'] . "\r\n";
$filename = $card_data['display_name'].'-'.rand(99,9999999999).'.vcf';
file_put_contents($vcf_dir.$filename, $card);
$mail = $contact_form->prop('mail');
$mail['attachments'] = 'uploads/2017/04/'.$filename;
$contact_form->set_properties(array('mail' => $mail));
}
}
add_action('wpcf7_mail_sent', 'after_sent_mail');
function after_sent_mail($wpcf7)
{
$mail = $wpcf7->prop('mail');
if(file_exists(WP_CONTENT_DIR.'/'.$mail['attachments']))
unlink(WP_CONTENT_DIR.'/'.$mail['attachments']);
}
@MattDbn
Copy link

MattDbn commented Jul 3, 2018

Hi there,

I've been looking for something like this for a while. Where does this code go? I've been reviewing the plugin folder but I'm not sure which file this gets added to.

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment