Skip to content

Instantly share code, notes, and snippets.

@scottwalters
Last active August 29, 2015 14:12
Show Gist options
  • Save scottwalters/728de05436455eaa6e10 to your computer and use it in GitHub Desktop.
Save scottwalters/728de05436455eaa6e10 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
use strict;
use warnings;
# change these
my $pdf = '/path/to/pdf/file/filename_of_pdf_file.pdf';
my $to_email = '[email protected]';
my $email_subject = 'form completed';
my $from_email = '[email protected]'; # don't use a gmail.com or any other domain with "SPF" records for spam control; email will be rejected; doesn't have to be able to accept email but should be a valid domain
my $nice_filename = "filename_of_the_pdf_it_should_save_as_on_peoples_computer.pdf";
use CGI;
use CGI::Carp 'fatalsToBrowser';
use IO::Handle;
my $name = CGI::param('name');
my $email = CGI::param('email');
if( $name and $email ) {
open my $mail, '|-', '/usr/sbin/sendmail', '-t' or die "sendmail missing: $!";
$mail->print("To: $to_email\n");
$mail->print("Subject: $email_subject\n");
$mail->print("From: $from_email\n");
$mail->print("\n");
$mail->print("name: $name\n");
$mail->print("email: $email\n");
# ... add more fields here as necessary
close $mail or die "mail send failed: $! $?";
# spit out the pdf
open my $fh, '<', $pdf or die "pdf missing: $!";
print "Content-type: application/pdf\r\n";
print qq{Content-Disposition: attachment; filename="$nice_filename";\r\n};
print "\r\n";
read $fh, my $buf, -s $fh;
print $buf;
} else {
print "Location: http://back.to.some/page/on/the/site/to/try/again\r\n\r\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment