# PHP - Attach image to mail() function?

**URL:** https://forum.kirupa.com/t/php-attach-image-to-mail-function/262113
**Category:** programming
**Created:** [June 3, 2008, 1:56pm UTC](https://forum.kirupa.com/t/php-attach-image-to-mail-function/262113 "2008-06-03T13:56:54Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![hairytea](https://avatars.discourse-cdn.com/v4/letter/h/13edae/32.png) [@hairytea](https://forum.kirupa.com/u/hairytea)
#### Post date: [June 3, 2008, 1:56pm UTC](https://forum.kirupa.com/t/php-attach-image-to-mail-function/262113/1 "2008-06-03T13:56:54Z")

</div>

Hello,

Sorry im a newbie to PHP…

My script below takes user input from my html form and emails the results to me…

On my form i have an upload form field, and wish for the end user to be able to upload an image which i want either attached to, or preferabally to be part of the body on the email message!

(As you can se i have managed to get the PHP file to take the uploaded image and place it in a directory…my problem is…how to send the mail with that picture attached or part of the email body?)

```php

<?php
  $file_dir = $_SERVER['DOCUMENT_ROOT'] . '/terry/uploads/';
  $name = $_REQUEST['name'] ;
  $phone = $_REQUEST['telephone'] ;
  
  foreach($_FILES as $file_name => $file_array) {
 if (is_uploaded_file($file_array["tmp_name"])) {
  move_uploaded_file($file_array["tmp_name"], "$file_dir/".$file_array["name"]) or die ("Couldn't copy");
 }
  }
  
  $string = "T-Shirt order form....

";
  $string .= "Name: ".$name."
";
  $string .= "Telephone Number: ".$phone."
";
  $string .= "T-Shirt Design"."

";
  
 mail("root@thanweb.co.uk", "Website - T-Shirt Order", $string, "From: root@thanweb.co.uk", "-froot@thanweb.co.uk"); 
 header( "Location: http://www.thanweb.co.uk/thankyou.htm" );
?>

```

---

<div class="post-metadata">

### Author: ![project107](https://avatars.discourse-cdn.com/v4/letter/p/eada6e/32.png) [@project107](https://forum.kirupa.com/u/project107)
#### Post date: [June 3, 2008, 4:45pm UTC](https://forum.kirupa.com/t/php-attach-image-to-mail-function/262113/2 "2008-06-03T16:45:33Z")

</div>

Add the image URL into the message itself?

```auto

  $string = "T-Shirt order form....

";
  $string .= "Name: ".$name."
";
  $string .= "Telephone Number: ".$phone."
";
  $string .= "T-Shirt Design"."

";
  $string. = "<img src=\"imageURL\" >";

```

---

<div class="post-metadata">

### Author: ![hairytea](https://avatars.discourse-cdn.com/v4/letter/h/13edae/32.png) [@hairytea](https://forum.kirupa.com/u/hairytea)
#### Post date: [June 4, 2008, 9:23am UTC](https://forum.kirupa.com/t/php-attach-image-to-mail-function/262113/3 "2008-06-04T09:23:14Z")

</div>

Thank you ahmednuamen i will try that!

---

<div class="post-metadata">

### Author: ![hairytea](https://avatars.discourse-cdn.com/v4/letter/h/13edae/32.png) [@hairytea](https://forum.kirupa.com/u/hairytea)
#### Post date: [June 4, 2008, 9:25am UTC](https://forum.kirupa.com/t/php-attach-image-to-mail-function/262113/4 "2008-06-04T09:25:49Z")

</div>

Project 107…

The image is going to be one that will be uploaded by the end user so i will not know the name of the image to be displayed…hence why im asking here how to extract the most recently submittted image other than simply displaying a static image on the server if you see what i mean.

Thank you anyhow.

🙂
