The Web Design Group

... Making the Web accessible to all.

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> code for sending mail with attachment
rrn
post May 27 2009, 12:55 AM
Post #1


Novice
**

Group: Members
Posts: 21
Joined: 15-April 09
Member No.: 8,330



in my website there is a form for entering details and attaching resume.

code is given below

CODE

$fileatt =/.'resume/'.$_FILES['userfile']['name']; // Path to the file
$fileatt_type = "application/octet-stream"; // File Type
$fileatt_name = "CV".$_POST['name'].".doc"; // Filename that will be used for the file as the attachment
$email_from = $_POST['name']." ".$_POST['email']; // Who the email is from
$email_subject = 'Application';
$email_message ='
            <table cellpadding="3" cellspacing="1" border="0" width="600">
                                                                                    
                                                                                      <tr>
                    <td width="120">Name</td>
                    <td>'.$_POST[name].'</td>
                </tr>
                <tr>
                    <td width="120">Age</td>
                    <td>'.$_POST[age].'</td>
                </tr>
                <tr>
                    <td width="120">Date of Birth</td>
                    <td>'.$_POST[date].'/'.$_POST[month].'/'.$_POST[year].'</td>
                </tr>
                <tr>
                    <td width="120">Gender</td>
                    <td>'.$_POST[gender].'</td>
                </tr>
                                                                                         <tr>
                    <td width="120">Nationality</td>
                    <td>'.$_POST[nation].'</td>
                </tr>
                <tr>
                    <td width="120">Country of Residence</td>
                    <td>'.$_POST[country].'</td>
                </tr>
                                                                                       <tr>
                    <td width="120">Contact Telephone</td>
                    <td>'.$_POST[phone].'</td>
                </tr>
                                                                                        <tr>
                    <td width="120">Email</td>
                    <td>'.$_POST[email].'</td>
                </tr>
                <tr>
                    <td width="120">Applying For</td>
                    <td>'.$_POST[applyfor].'</td>
                </tr>
                           </table>
                ';
$email_to  = 'abc@xxx.com';

//adds the e-mail address of the sender
$headers = "From: ".$_POST[email];
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";

$email_message .= "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type:text/html; charset=\"utf-8\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .
$email_message . "\n\n";

$file = fopen($fileatt,'rb');
$data = fread($file,filesize($fileatt));
fclose($file);


$data = chunk_split(base64_encode($data));

$email_message .= "--{$mime_boundary}\n" .
"Content-Type: {$fileatt_type};\n" .
" name=\"{$fileatt_name}\"\n" .

"Content-Transfer-Encoding: base64\n\n" .
$data . "\n\n" .
"--{$mime_boundary}\n";
unset($data);
unset($file);
unset($fileatt);
unset($fileatt_type);
unset($fileatt_name);

@mail($email_to, $email_subject, $email_message,  $headers);


the code is working fine ,details along with the attachment is received in the mail.
but the problem is , users can only attach word document . if any pdf file or txt file is attached , it is not able to open the file from the mail id.

tried many ways , didnt work.

please helpe to solve this..
any help will be appreciated.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Frederiek
post May 27 2009, 03:47 AM
Post #2


Programming Fanatic
********

Group: Members
Posts: 5,146
Joined: 23-August 06
From: Europe
Member No.: 9



The Sitepoint article at http://www.sitepoint.com/article/advanced-email-php/5/ may be of help.
Or simply google for "php file attachment".
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Brian Chandler
post May 27 2009, 04:16 AM
Post #3


Jocular coder
********

Group: Members
Posts: 2,460
Joined: 31-August 06
Member No.: 43



FWIW, the script doesn't appear to do any checking whatsoever, so it is totally open to spam injections and so on.

Are you having these attachments sent to yourself? So it is you who has the problem with opening a pdf or txt file?

CODE

$fileatt_name = "CV".$_POST['name'].".doc"; // Filename that will be used for the file as the attachment


Well, you are adding .doc to the end of the filename, which is unlikely to help if it's not a M$-doc file.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
rrn
post May 27 2009, 05:33 AM
Post #4


Novice
**

Group: Members
Posts: 21
Joined: 15-April 09
Member No.: 8,330



thanks to all who gave suggestions. now its working..

but another problem , i am receiving one more attachment along with the real attachment . i dont know from where it is coming.

is there any problem with the code??
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post

Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 



- Lo-Fi Version Time is now: 26th April 2024 - 09:03 PM