1

I have a form:

<form id='contactus' action='send.php' method='post' enctype="multipart/form-data" accept-charset='UTF-8'>
<fieldset>
<legend>My Form</legend>
<table><tbody>
<tr valign="top">
<td nowrap="nowrap" style="text-align: right;"><label for='name' >Name: </label></td>
<td><input type='text' name='name' id='name' value=''/></td></tr>
<tr valign="top">
<td nowrap="nowrap" style="text-align: right;"><label for='email' >Email: </label></td>
<td><input type='text' name='email' id='email' value=''/></td></tr>
<tr valign="top">
<td nowrap="nowrap" style="text-align: right;"><label for='link' >Link: </label></td>
<td><input type='text' name='link' id='link' value=''/></td></tr>
<tr valign="top">
<td nowrap="nowrap" style="text-align: right;"><label for='subject' >Subject: </label> 
</td><td><input type='text' name='subject' id='subject' value=''/></td></tr>
<tr valign="top">
<td nowrap="nowrap" style="text-align: right;"><label for='description' >Description: 
</label></td>
<td><textarea rows="10" cols="50" name='description' id='description'></textarea>
</td></tr>
<tr valign="top">
<td nowrap="nowrap" style="text-align: right;"><label for='photo' >Photo: </label></td>
<td><input type="file" name='photo' id='photo' /></td></tr>
</tbody></table>
<td nowrap="nowrap" style="text-align: center;"><input type='submit' value='Send' /></td>
</fieldset>
</form>

and my 'send.php' is

<?php
if(isset($_POST['email'])) {

$email_to = "mymail@gmail.com";
$subject = $_POST['subject'];
$name = $_POST['name'];
$email = $_POST['email'];
$link = $_POST['link'];
$description = $_POST['description'];
$photo = $_POST['photo'];

$email_message .= "Name: ".($name)."\n";
$email_message .= "Email: ".($email)."\n";
$email_message .= "Link: ".($link)."\n";
$email_message .= "Subject: ".($subject)."\n";
$email_message .= "Description: ".($description)."\n";
$email_message .= "Photo: ".($photo)."\n";

// create email headers
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $subject, $email_message);
}
header("Location: thank-you.html");
?>

My question is:

1) Everything works good.. except 'photo', i know it suppose to be an attachment. But i don't know how to make it?

2) And how can i see this bold when i receive the email?

$email_message .= "Name: ".($name)."\n";

emp: Name: John

I have try to do like this but didn't work.

$email_message .= "<b>Name: </b>".($name)."\n";
$email_message .= "<b>Email: </b>".($email)."\n";
$email_message .= "<b>Link: </b>".($link)."\n";
$email_message .= "<b>Subject: </b>".($subject)."\n";
$email_message .= "<b>Description: </b>".($description)."\n";

Thanks in advance

Suzylee
  • 31
  • 2
  • 8
  • for attachment follow this http://stackoverflow.com/questions/17923667/i-want-to-send-email-with-attachments-using-phpmailer – chirag ode Aug 10 '13 at 09:22

2 Answers2

0

Use the $_FILES super global array instead of $_POST to handle files.

 $photo = $_FILES['photo']['name'];
    ...

 $email_message .= "Photo: ".$photo."\n";
Bere
  • 1,627
  • 2
  • 16
  • 22
  • @Suzylee I thought you need the file name. If you want the file use `$_FILES["file"]["tmp_name"]`. But is it not straight forward to send an attachment with mail() function. Look http://stackoverflow.com/questions/6275070/php-mail-attachment-problems – Bere Aug 10 '13 at 09:18
0

You have to specify the file with $_FILES.

 $name_of_uploaded_file =
        basename($_FILES['photo']['name']); // get name of file uploaded

    //get the file extension of the file
    $type_of_uploaded_file =
        substr($name_of_uploaded_file,
        strrpos($name_of_uploaded_file, '.') + 1);

    $size_of_uploaded_file =
        $_FILES["photo"]["size"]/1024;//size in KBs

    //copy the temp. uploaded file to uploads folder

    $path_of_uploaded_file = $upload_folder . $name_of_uploaded_file;
    $tmp_path = $_FILES["photo"]["tmp_name"];

    if(is_uploaded_file($tmp_path))
    {
      if(!copy($tmp_path,$path_of_uploaded_file))
      {
        $errors .= '\n error while copying the uploaded file';
      }
    }

First, we need to include the pear library files for these classes.

include_once('Mail.php');
include_once('Mail_Mime/mime.php');


$message = new Mail_mime();

$message->setTXTBody($text);

$message->addAttachment($path_of_uploaded_file);

$body = $message->get();

$extraheaders = array("From"=>$from, "Subject"=>$subject,"Reply-To"=>$visitor_email);

$headers = $message->headers($extraheaders);

$mail = Mail::factory("mail");

$mail->send($to, $headers, $body);
SarathSprakash
  • 4,614
  • 2
  • 19
  • 35