Jump to content

Mail with document attachment from form


defeated

Recommended Posts

Hi,

I've got a problem.  I have a job website and need people to be able to apply for a job and send their cv.  I CANT get anything to attach and send from a form.

 

I will then be using mySQL data to insert selected job details into the email. I have lost count of how many scripts I have tried using.  I don't know if i'm just not getting something.  I am using <input type="file" name="attachment"> in the form and can't seem to get a script to get me from there to an e-mail in my inbox with a cv attached.  The closest I got was a script that sent a jpeg from another site.  It was the only thing that worked for me and was useless.

 

I have little hair left and am pulling what remains out like a madman.

I have worked on this for weeks trying every search I can think of. 

Did I mention that this stuff is WAY beyond me at the moment?

HEEEELLLLLLPPPPP!!

Cheers in advance

Ian.

P.S. Tried the sourceforge phpmailer.  Couldn't get it to work like all the others but if somebody can babystep me through the attachment part from the form as described above I'd be happy to use it.

 

pps. ok I'm rambling now so I'll just push post..... hard to see the button through the tears!

Link to comment
Share on other sites

<?php

//access mailer

require ('/home/j43012/public_html/cgi-bin/class.phpmailer.php');

 

 

 

$mail = new PHPMailer();

 

$subject=$_POST['subject'];

$attachment=(is_array($_FILES['attachment']))? implode($_FILES['attachment']):$_FILES['attachment'];

 

 

 

 

 

$mail->From = "from@example.com";

 

$mail->FromName = "The Website";

 

$mail->AddAddress("me@mycompany.ie", "JBM.ie");

 

$mail->AddAddress("ellen@example.com");                  // name is optional

 

$mail->AddReplyTo("info@example.com", "Information");

 

 

 

$mail->WordWrap = 50;                                // set word wrap to 50 characters

 

$mail->AddAttachment("$attachment");        // add attachments

 

 

$mail->AddAttachment("$attachment");    // optional name

 

$mail->IsHTML(true);                                  // set email format to HTML

 

 

 

$mail->Subject = "$subject";

 

$mail->Body    = "This is the HTML message body <b>in bold!</b>";

 

$mail->AltBody = "This is the body in plain text for non-HTML mail clients";

 

 

 

if(!$mail->Send())

 

{

 

  echo "Message could not be sent. <p>";

 

  echo "Mailer Error: " . $mail->ErrorInfo;

 

  exit;

 

}

 

 

 

echo "Message has been sent";

 

?>

 

 

 

 

Link to comment
Share on other sites

I'm guessing that

$attachment=(is_array($_FILES['attachment']))? implode($_FILES['attachment']):$_FILES['attachment'];

 

is where the problem.... or at least one of the problems is.  I put the line in because $attachment was returning as "array" when I echoed it.

Ian

Link to comment
Share on other sites

:o Ok... so I did that.  It failed to materialise in my webmail but when I sent it to another account it came up as a dat file with a different name (php435363632) or something like that..... I can smell success. The original file was a mycv.doc, but it was created on an outdated mac not Word and saved as a word file so there may be issues there. 

 

So priority now is to get the original file name to send

followed by recieving as a .doc, rtf and txt would be nice too since it will only be cv's that should be sent.

 

Thank you so so much for the input so far... getting there.

Link to comment
Share on other sites

using the

$attachment = $_FILES['attachment']['tmp_name'];

in your attachment is a security risk since you exposing the temporary saved file name. If someone uploads a php file with malicious code, they can run it directly in your server.

 

You'll need to move the uploaded file (after you varifiy it's safe) to another folder in your site, and than send that file. More details on how to upload a file can be found here

 

http://us3.php.net/manual/en/features.file-upload.php

 

 

 

Link to comment
Share on other sites

I just read up on the phpmailer script, you should use the following:

 

<?php
//access mailer
require ('/home/j43012/public_html/cgi-bin/class.phpmailer.php');

$mail = new PHPMailer();

$subject=$_POST['subject'];

$mail->From = "from@example.com";
$mail->FromName = "The Website";
$mail->AddAddress("me@mycompany.ie", "JBM.ie");
$mail->AddAddress("ellen@example.com");                  // name is optional
$mail->AddReplyTo("info@example.com", "Information");

$mail->WordWrap = 50;                                 // set word wrap to 50 characters

//Attachment
$tmp_file =  '.tmpupload_'.getmypid();
if(!move_uploaded_file($_FILES['attachment']['tmp_name'], $tmp_name))
  die("Failed to move uploaded file");
$mail->AddAttachment($tmp_name,$_FILES['attachment']['name']);         // add attachments

$mail->IsHTML(true);                                  // set email format to HTML

$mail->Subject = "$subject";
$mail->Body    = "This is the HTML message body in bold!";
$mail->AltBody = "This is the body in plain text for non-HTML mail clients";

if(!$mail->Send())
{
   echo "Message could not be sent. <p>";
   echo "Mailer Error: " . $mail->ErrorInfo;
   exit;
}

//Delete temp attachment
unlink($tmp_file);

echo "Message has been sent";
?>

Link to comment
Share on other sites

:-[ Nope..... getting colder.

that returned

"Warning: move_uploaded_file() [function.move-uploaded-file]: open_basedir restriction in effect. File() is not within the allowed path(s): (/home/j43012:/usr/lib/php:/usr/local/lib/php:/tmp) in /home/j43012/public_html/postjob/mailer.php on line 35

Failed to move uploaded file"

 

I'm still really appreciating all the help though :)

Link to comment
Share on other sites

 

I tried looking at the above.  I tried a couple of things but couldn't get anything to work.  I'm afraid it's all a little beyond me.  I don't really follow whats happening there..... maybe I will down the line but that's then and this is now.  I'm so lost.  6 months ago I didn't know any HTML let alone anything else. I've bitten off more than I can chew I think.  Still, now that I've bitten I WILL get there.  Besides this stuff is great when it works.  Even decided I'm going to college this year to learn software dev properly.

Meanwhile.......... ???

 

 

Link to comment
Share on other sites

Look at w3schools example of PHP email forms

 

w3schools.com/php/default.asp

 

I am DETERMINED to help you. I haven't been programming long with PHP, 2nd worst language, and I know how it feels to be hopeless. Also If your include isn't working. Try an absolute file path.

Link to comment
Share on other sites

Anybody? Plllleeeeeaaaaassssseeeee!!!!!!

I've just had a bad experience trying to implement a different mailer with SMTP. Got nowhere AGAIN and screwed up something.  Now the inbox in outlook says there is a new message when there isn't.  I really really need the phpmailer to work!

 

I know I'm probably one of these people that should leave what I don't know about alone but I made a promise that I'd get the site up and running after the original (bought) product looked crap and did nothing.  I can't let them down. Please someone help.

Link to comment
Share on other sites

Ok....I wrote and tested this, so it should work for you. I tested it with phpMailer v2.0.0 rc3

 

<?php
  require ('/home/j43012/public_html/cgi-bin/class.phpmailer.php');

  $mail = new PHPMailer();

  $mail->From = "from@example.com";
  $mail->FromName = "The Website";
  $mail->AddAddress("to@example.com");
  $mail->AddReplyTo("info@example.com", "Information");
  $mail->IsHTML(true);

  $mail->WordWrap = 50;

  //Attachment
  if($_FILES['attachment']){
    $uploadErrors = array(
      UPLOAD_ERR_INI_SIZE => 'The uploaded file exceeds the upload_max_filesize directive in php.ini.',
      UPLOAD_ERR_FORM_SIZE => 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.',
      UPLOAD_ERR_PARTIAL => 'The uploaded file was only partially uploaded.',
      UPLOAD_ERR_NO_FILE => 'No file was uploaded.',
      UPLOAD_ERR_NO_TMP_DIR => 'Missing a temporary folder.',
      UPLOAD_ERR_CANT_WRITE => 'Failed to write file to disk.',
      UPLOAD_ERR_EXTENSION => 'File upload stopped by extension.',
    );
    //Check for error
    if($_FILES['attachment']['error'])
      die("Upload error: {$uploadErrors[$_FILES['attachment']['error']]}");
    //Move uploaded file
    $tmp_file =  '.tmp_upload_'.getmypid(); //Make unique filename
    if(!move_uploaded_file($_FILES['attachment']['tmp_name'], $tmp_file))
      die("Failed to move uploaded file to $tmp_file");
    $mail->AddAttachment($tmp_file,$_FILES['attachment']['name']);
  }
  
  $mail->Subject = $_POST['subject'];
  $mail->Body    = "This is the HTML message body in bold!";
  $mail->AltBody = "This is the body in plain text for non-HTML mail clients";

  if(!$mail->Send()){
   echo "Message could not be sent. <p>";
   echo "Mailer Error: " . $mail->ErrorInfo;
   exit;
  }

  //Delete temp attachment
  unlink($tmp_file);

  echo "Message has been sent";
?>

Link to comment
Share on other sites

Hi Rhodesa,

Thanks once again for your input. unfortunately it is returning the following:-

 

Warning: move_uploaded_file(.tmp_upload_24310) [function.move-uploaded-file]: failed to open stream: Permission denied in /home/j43012/public_html/postjob/mailer.php on line 30

Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/phpHsJwxw' to '.tmp_upload_24310' in /home/j43012/public_html/postjob/mailer.php on line 30
Failed to move uploaded file to .tmp_upload_24310

 

I don't know if that means anything to you.... it certainly doesn't to me!

Tried moving it to the root folder too but no difference. (it's only in the postjob folder because that is where I put stuff I'm working on ..... it's password protected and keeps out googlebots.

I know it wasn't the right way of doing things and maybe wasn't very secure but at least the first way worked.... appart from showing up with the tmp name instead of the file name. I would do or try anything at this stage. Thanks again.

 

Link to comment
Share on other sites

I assume you are managing these files through an FTP client? There should be a way in there to add WRITE permission to the /home/j43012/public_html/postjob/ folder.

 

If you have the ability to make a folder outside of public_html that is writable, you may want to store the temporary uploads there instead. Let me know if you want to go that route.

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.