Jump to content

Sending email with attacment


mrkhan
 Share

Recommended Posts

Hello,

from the day i am using PW every day i am loving it more and more, its really great. i am trying to create a page for website where people can apply for job and send CV as attachment too.

i was searching forums from last few days and create one page with combinations of many posts in form, my code is bellow.

<style>
#email2 { display:none; }
#message-error { color:#F00;}
#message-success { color:#09F;}
</style>
<?php 
// check if the form was submitted
$spam_check=trim($input->post->email2);
if($input->post->submit && $spam_check == '') {
   $sent = false;
   $error = "";
   $emailTo = $page->email;

// sanitize form values or create empty
    $form = array(
    'fullname' => $sanitizer->text($input->post->fullname),
    'job_title' => $sanitizer->text($input->post->job_title),
    'job_id' => $sanitizer->text($input->post->job_id),
     'contact' => $sanitizer->text($input->post->contact),
     'email' => $sanitizer->email($input->post->email),
    'comments' => $sanitizer->textarea($input->post->comments),
    ); 
    
$msg = "Full Name: $form[fullname]\n" . 
        "Contact number: $form[contact]\n" . 
         "Job Title: $form[job_title]\n" . 
         "Job Id: $form[job_id]\n" . 
         "Email: $form[email]\n" . 
         "Comments: $form[comments]"; 

// attachment
if(isset($_POST['submit']) && !empty($_FILES['file']['size'])) {

    $filename = $_FILES["file"]["name"];
    $filetype = $_FILES["file"]["type"];
    $filesize = $_FILES["file"]["size"];
    $filetemp = $_FILES["file"]["tmp_name"];

    if($filesize < 10) throw new Exception("File too small");
    if($filesize > 1000000) throw new Exception("File too big"); 

    $ext = strtolower(pathinfo($filename, PATHINFO_EXTENSION)); 
    if(!in_array($ext, array('doc', 'pdf', 'docx'))) throw new Exception("Invalid file type"); 
    $destination = $config->paths->cache . $user->name . '.' . $ext; 
    if(!move_uploaded_file($filetemp, $destination)) throw new Exception("Unable to move uploaded file"); 

$fp = fopen($destination, "rb");
    $file = fread($fp, $filesize);
    $file = chunk_split(base64_encode($file));
unlink($destination); 
}

// This two steps to help avoid spam
$headers .= "Message-ID: <".gettimeofday()." TheSystem@".$_SERVER['SERVER_NAME'].">\r\n";
$headers .= "X-Mailer: PHP v".phpversion()."\r\n";         

// With message
   
$headers .= "Content-Type: text/html; charset=utf-8\r\n";
$headers .= "Content-Transfer-Encoding: 8bit\r\n";
$headers .= "".$message."\n";
$headers .= "--".$num."\n"; 

// Attachment headers
$headers  .= "Content-Type:".$filetype." ";
$headers  .= "name=\"".$filename."\"r\n";
$headers  .= "Content-Transfer-Encoding: base64\r\n";
$headers  .= "Content-Disposition: attachment; ";
$headers  .= "filename=\"".$filename."\"\r\n\n";
$headers  .= "".$file."\r\n";
$headers  .= "--".$num."--"; 

if(mail($emailTo, "Job Application", $msg, "From: $form[email]",$headers))
{
// populate body with success message, or pull it from another PW field
         $messageok = "<div id='message-success'><p>Thanks, your message has been sent.<b>We will Contact you back</p>        </div>"; 
        $sent = true;
}else{
$error= "<div id='message-error'><p>Error! Mail Not Sent.</p></div>"; 
$sent = false; 
} 

}

include("./head.inc"); 
?>
 

here is my form code

Please enter your contact details below.<br /><br />

    <?php
if(strlen($error)>1)
{
echo $error; 
}

// to get JOB title and id
$job=$pages->get($input->urlSegment1);
if(!$sent) {
?> 
<form action="./" method="post" class="contact-form" id="contactform">

<input id="job_title" name="job_title" type="hidden" value="<?php echo $job->title;?>" />

<input id="job_id" name="job_id" type="hidden" value="<?php echo $job->job_id;?>" />

<input Placeholder="Please Enter Your Name" name="fullname" id="fullname" value="<?php echo $form[fullname];?>" />

<input type="text" Placeholder="Please Enter Your Contact Number" name="contact" id="contact" value="<?php echo $form[contact];?>" />

<input type="text" name="email" id="email" Placeholder="Please Enter Your Email"  />

<input type="file" name="file" id="file" value="Uplode Cv" />

<textarea id="comments" name="comments"  cols="60" rows="3" Placeholder="Please Enter Your Message"><?php echo $form[comments];?>

<input type="text" name="email2" id="email2">

<input name="submit" id="submit" type="submit" value="Send"  />
</form> 
<?php
}else{
echo $messageok;   
}
include("./foot.inc");
?> 

form is displaying properly but emails are not going and i am getting error  "Error! Mail Not Sent."

what i want to do is send email with attachment and then delete uploaded file from server and of course secure.

really appreciate your update.

Thanks

  • Like 1
Link to comment
Share on other sites

mail() is a native PHP function. So if it is not working take a look at your server configuration.

wireMail() is the ProcessWire implementation of this function. Try to use this instead as it offers some more features: https://processwire.com/talk/topic/5693-new-module-type-wiremail/

Anyway I would suggest you to take a look on FormBuilder as it is made exactly for your use case :)

https://processwire.com/talk/store/product/2-form-builder-single/

  • Like 1
Link to comment
Share on other sites

Hello,

thanks for update i really like the function wiremail() but how to attach form uploaded file with email using this function ?

as for security i want to make it as much secure i can like

1. check file on uploading or attaching with email.

2. delete file after emails send.

or any thing other option you can think for security.

Thanks

Link to comment
Share on other sites

Limited time, so just a quick answer - I would recommend using SwiftMailer - check out this page on how to send a message and include attachments:

http://swiftmailer.org/docs/messages.html

You might be able to make use of the swiftmailer files from teppo's module: http://modules.processwire.com/modules/wire-mail-swift-mailer/ but I don't think there is anyway to use wireMail() directly to send attachments.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...