Solved

Simple email script

Posted on 2006-11-21
10
210 Views
Last Modified: 2008-02-01
I need to try and get a simple script to email a form and attach a file to it, if they choose to attach a file. Basically its a simple vacancy form where you can attach a CV. I have no experience in coding fom and stuff, so any help would be good. I usually pay to get this done, but the  budget does not allow for this, so i might have to try do it myself :0/

Thanks for any help.
0
Comment
Question by:Sideas
  • 4
  • 4
  • 2
10 Comments
 
LVL 9

Accepted Solution

by:
lucki_luke earned 475 total points
ID: 17991071
Hey,

Heres a simple form:

<form enctype="multipart/form-data" action="email.php" method="post">
Name: <input type="text" name="name" /><br />
E-Mail: <input type="text" name="email" /><br />
Subject: <input type="text" name="subject" /><br />
Body:  <textarea name="body" cols="30" rows="10"></textarea><br />
<input type="hidden" name="MAX_FILE_SIZE" value="30000" />
CV: <input type="file" name="cv" /><br />
Name: <input type="button" />
</form>

For the mailing funciton I would suggest you to download a library like PHPMailer because attachments and other stuff are not so easy.
http://phpmailer.sourceforge.net/
Then the email.php:
<?php
// every input inside the form is now available in the $_POST variable (except the file)

require("class.phpmailer.php");

$attachment = false;
if($_FILES['cv']['name'] =! ''){
      $uploaddir = '/var/www/uploads/';
      $uploadfile = $uploaddir . basename($_FILES['cv']['name']);
      
      if (move_uploaded_file($_FILES['cv']['tmp_name'], $uploadfile)) {
            // everything worked fine up to now
            $attachment = true;
      } else {
            // somethings wrong
            echo "Failed uploading file!";
      }
}

$mail = new PHPMailer();

$mail->From     = $_POST['email'];
$mail->FromName = $_POST['name'];
$mail->Mailer   = "mail";
$mail->AddAddress("yourname@site.com", "Receiver"));
$mail->Subject = $_POST['subject'];
$mail->Body    = $_POST['body'];
if($attachment === true)
      $mail->AddAttachment($uploadfile, $_FILES['cv']['name']);
if(!$mail->Send())
{
   echo "There was an error sending the message";
   exit;
}
?>

Using the PHPMailer class to send the mail is quite easy as seen above.
If you have any questions left please ask.

Lukas
0
 
LVL 14

Expert Comment

by:Aamir Saeed
ID: 17991802
If you find it difficult to use any library, you can use this simple code


      <div style="height:15px"></div>

      <table width="80%" align="center" border="1" class="Tb1">
            <tr>
                  <td>
                        <form action="email.php?submit=yes" method="post" enctype="multipart/form-data" name="compose">
                        <table width="93%" align="center" border="1" class="Tb1">
                              <tr class="headerrow">
                                    <td colspan="2">
                                          Send Email                                    </td>                  
                              </tr>
                              <tr class="oddrow">
                                    <td>
                                          To:                                    </td>
                                    <td>
                                          <input type="text" tabindex="1" name="receiver" size="50">                                    </td>
                              </tr>
                              <tr class="evenrow">
                                    <td>
                                          Subject:                                    </td>
                                    <td>
                                          <input type="text" name="subject" size="50">                                    </td>
                              </tr>
                              <tr class="oddrow">
                                    <td>
                                          Message:                                    </td>
                                    <td>
                                          <textarea name="message" cols="45" rows="10"></textarea>                                    </td>
                              </tr>
                              <tr class="oddrow">
                                <td>Attatchment: </td>
                                <td><input id="fileatt" type="file" name="fileatt"  tabindex="" /></td>
                          </tr>
                              <tr class="evenrow">
                                    <td align="center" colspan="2">
                                          <input type="submit" name="send" value="Send" class="buttons">                                    </td>                                    
                              </tr>
                        </table>                        
                        </form>                  </td>
                  <form name="combo_box" action="send_email.php" method="post">
                  </form>
            </tr>
</table>      
<?
      if($_GET['submit'] == 'yes')
      {
            // Message Receiver
            $reciever = $_POST['receiver'];
            
            // Message Sender
            $subject = $_POST['subject'];
            
            // Message
            $message = $_POST['message'];
            
            // Obtain file upload vars
            $fileatt            = $_FILES['fileatt']['tmp_name'];
            $fileatt_type      = $_FILES['fileatt']['type'];
            $fileatt_name      = $_FILES['fileatt']['name'];            // Converting String of Receiver Name into Array

            $recievernames = explode(",", $reciever);

            // Iterating Receiver Name's Array
            $i = 0;
            while($recievernames[$i])
            {
                  $email = $recievernames[$i];
                  
                  $headers = "From: aamir@post.com";

                   if (is_uploaded_file($fileatt)) {
                        $file = fopen($fileatt,'rb');
                        $data = fread($file,filesize($fileatt));
                        fclose($file);
                  
                        // Base64 encode the file data
                        $data = chunk_split(base64_encode($data));
                    }                  

                    // Generate a boundary string
                    $semi_rand = md5(time());
                    $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
                    
                    // Add the headers for a file attachment
                    $headers .= "\nMIME-Version: 1.0\n" .
                                      "Content-Type: multipart/mixed;\n" .
                                      " boundary=\"{$mime_boundary}\"";
                  
                    // Add a multipart boundary above the plain message
                    $message = "This is a multi-part message in MIME format.\n\n" .
                                     "--{$mime_boundary}\n" .
                                     "Content-Type: text/plain;\n charset=\"iso-8859-1\"\n" .
                                     "Content-Transfer-Encoding: 7bit\n\n" .
                                     $message . "\n\n" .
                                     "--{$mime_boundary}\n";

                    // Add file attachment(s) to the message
                    if ($fileatt_name > "") {
                        $message .= "Content-Type: {$fileatt_type};\n" .
                                           " name=\"{$fileatt_name}\"\n" .
                                            "Content-Transfer-Encoding: base64\n" .
                                           "Content-Disposition: attachment;\n" .
                                           " filename=\"{$fileatt_name}\"\n\n" .
                                            $data . "\n" .
                                            "--{$mime_boundary}\n";
                        }
                  
                  // Mail it
                  mail($email, $subject, $message, $headers);                  

                  $i++;
            }            
                        
      }      
?>            
0
 
LVL 2

Author Comment

by:Sideas
ID: 17994118
Hi guys,

Got so far on both of these but didn't get them working :-/

lucki_luke i got to here
http://www.idealogyltd.com/Email-form/email-form.html
I did put the sourceforge script up you can see it is there. http://www.idealogyltd.com/Email-form/

i_m_aamir i got to this point
http://www.idealogyltd.com/Email-form/email-form-2.html

Any help on both be great. I not sure what might be going wrong so don't know where to look. I guess its simple but as i don't know this stuff i cant figure it out.

Thanks again for trying to help a noob.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 9

Assisted Solution

by:lucki_luke
lucki_luke earned 475 total points
ID: 17994644
Hi,

sorry, had a small mistake in my script:

$mail->AddAddress("yourname@site.com", "Receiver")); // otherwise parse error
must be
$mail->AddAddress("yourname@site.com", "Receiver");

and in the form I made something very stupid:
Name: <input type="button" />
should be
<input type="submit" value="Submit" />

The problem with your other form (i_m_aamirs approach) is that the file extension has to be .php not .html .

Lukas

btw: you mixed the links ;) mine was email-form-2.html and i_m_aamirs is email-form.html
0
 
LVL 14

Assisted Solution

by:Aamir Saeed
Aamir Saeed earned 25 total points
ID: 17994973
lucki_luke: is right

->The problem with your other form (i_m_aamirs approach) is that the file extension has to be .php not .html .

I think you are facing problem in using my code. If you would like i can configure this on your site.

For this you have to send me FTP Info.

ucitianz@yahoo.com

BOL!
0
 
LVL 2

Author Comment

by:Sideas
ID: 17995485
I think i have amended them both correctly, now i get an error like so.

Warning: main(class.phpmailer.php): failed to open stream: No such file or directory in /data/webroot/default/Email-form/email.php on line 4
Fatal error: main(): Failed opening required 'class.phpmailer.php' (include_path='.:') in /data/webroot/default/Email-form/email.php on line 4

Think this is down to my URL or something. I guess this is quite hard.

Thanks for the offer ucitianz i cannot get you FTP access, company would not let it happen. Hopefully you guys can figure it out from there!
0
 
LVL 9

Assisted Solution

by:lucki_luke
lucki_luke earned 475 total points
ID: 17996353
No, not hard.
Well this is because your phpmailer class is in a different directory, so you should write instead of
require("class.phpmailer.php");

require("phpmailer/class.phpmailer.php");

as your phpmailer stuff is in this directory.
0
 
LVL 2

Author Comment

by:Sideas
ID: 17996552
Hi,

Changed this, error on line 35 now

Parse error: parse error, unexpected $ in /data/webroot/default/Email-form/email.php on line 35

Sorry about this!
0
 
LVL 9

Assisted Solution

by:lucki_luke
lucki_luke earned 475 total points
ID: 17996647
Hm shouldnt be... (i dont get this error)

your email.php should look like this:

<?php
// every input inside the form is now available in the $_POST variable (except the file)

require("phpmailer/class.phpmailer.php");

$attachment = false;
if($_FILES['cv']['name'] != ''){ // accidentally i mixed the operators in previous code
     $uploaddir = '/var/www/uploads/';
     $uploadfile = $uploaddir . basename($_FILES['cv']['name']);
     
     if (move_uploaded_file($_FILES['cv']['tmp_name'], $uploadfile)) {
          // everything worked fine up to now
          $attachment = true;
     } else {
          // somethings wrong
          echo "Failed uploading file!";
     }
}

$mail = new PHPMailer();

$mail->From     = $_POST['email'];
$mail->FromName = $_POST['name'];
$mail->Mailer   = "mail";
$mail->AddAddress("yourname@site.com", "Receiver");
$mail->Subject = $_POST['subject'];
$mail->Body    = $_POST['body'];
if($attachment === true)
     $mail->AddAttachment($uploadfile, $_FILES['cv']['name']);
if(!$mail->Send())
{
   echo "There was an error sending the message";
   exit;
}
?>
0
 
LVL 2

Author Comment

by:Sideas
ID: 17997478
Hi,

That seems to have fixed it :0) not sure why it wasn't working, code looked the same.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question