0

Using this code to send an attached file in email using PHP.

As a result, I am only getting my email message, but I am unable to receive it with an attached file.

Here is my PHP code:

<?php
if($_POST && isset($_FILES['attachment'])){   
$from_email = 'someone@gmail.com'; //sender email
$recipient_email = 'me@gmail.com'; //recipient email
$subject = 'Test mail'; //subject of email
$message = 'This is body of the message'; //message body 

$file_name = $_FILES['attachment']['name'];
$temp_name = $_FILES['attachment']['temp_name'];
$file_type = $_FILES['attachment']['type'];
$file_size = $_FILES['attachment']['size'];

//get base ext of file

$base = basename($file_name);
$extention = substr($base, strlen($base)-4, strlen($base));

//only these files type will allowed
$allowed_extentions = array(".doc", "docx", ".pdf", ".zip", ".png", ".xlsx");



/* check that file type is allowed  */ 

     if(in_array($extention, $allowed_extentions )) {

$user_email = filter_var($_POST["email"], FILTER_SANITIZE_EMAIL);

if($file_error>0)
{
    die('upload error');
}



/* things you need  */

$file = $temp_name ;
$content = chunk_split(base64_encode( file_get_contents($file) )) ;
$uid = md5(uniqid(time()));



    //header
    $header .= "MIME-Version: 1.0\r\n"; 
    $header .= "From:".$from_email."\r\n"; 
    $header .= "Reply-To: ".$user_email."" . "\r\n";
    $header .= "Content-Type: multipart/mixed; boundary = $boundary\r\n\r\n"; 

    /* declaring we  have multiple kinds of email */ 
    $header .= "Content-Type: multipart/mixed; boundary=\"" .$uid."\"\r\n\r\n";
    $header .= "This is multipart message in MIME formate.\r\n";             

    /* plain text part */ 
    $header .= "--".$uid."\r\n";
    $header .= "Content-Type:text/plain; charset=iso-8859-1\r\n";
    $header .= "Content-Transfer-Encoding : 7bit\r\n\r\n";
    $header .= $message."\r\n\r\n";

    /* file attachment  */ 
    $header .= "--".$uid."\r\n";
    $header .= "Content-Type: ".$file_type."; name=\"".$file_name."\"\r\n";
    $header .= "ContentTransfer-Encoding:base64\r\n";
    $header .= "Content-Disposition: attachment; filename=\"".$file_name."\"\r\n";
    $header .= $content."\r\n\r\n";

    require_once "Mail.php";

 /* mail setup recipients, subject etc */
 $name =  'sanju';       //$_POST['name'];
 $email =  $from_email ;     //         $_POST['email'];
 $subject = 'Test mail';   //$_POST['subject'];
 //$comments = 'This is body of the message';   //$_POST['message'];

  $recipients = 'me@gmail.com';

$header = array (
           'From' => "someone@gmail.com",
           'Content-type' => "text/html;charset=UTF-8",
           'Reply-To' => "someone@gmail.com",
           'To' => "contact@domain.com",

           );



 $mailmsg="Name : $name \n\n Email :$email  \n\n Message : $message   ";
 /* SMTP server name, port, user/passwd */
 $smtpinfo["host"] = "ssl://lnxind3.cloudhostdns.net";
 $smtpinfo["port"] = "465";
 $smtpinfo["auth"] = true;
 $smtpinfo["username"] = "contact@net-lytics.com";
 $smtpinfo["password"] = "sanjeev12@";
 /* Create the mail object using the Mail::factory method */
 $mail_object =& Mail::factory("smtp", $smtpinfo);
 /* Ok send mail */
 echo $mail_object->send($recipients, $header, $mailmsg);
 $msg = " Thank you for contacting us. .";;




 } else {
            echo " File type not allowed";
        }
}
?>

The HTML form:

<form name="form1" enctype="multipart/form-data" method="post" action="">
<label>Your Name
<input type="text" name="name" />
</label> 
<label>Your Email
<input type="email" name="email" />
</label> 
<label>Attachment
<input type="file" name="attachment" />
</label>
<label>
<input type="submit" name="button" value="Submit" />
</label>
</form>
Warren Sergent
  • 2,542
  • 4
  • 36
  • 42
sanjay
  • 37
  • 1
  • 5
  • See this.... http://stackoverflow.com/questions/24644436/php-mail-form-doesnt-complete-sending-e-mail – Marc Giroux Jun 17 '16 at 12:32
  • I never understood why people bother with filtering files as "allowed type" or not by means of checking the "file name extension". A `zip` archive is a dangerous thing that must not be allowed! (Unless you call it `xy.zip.gif`...) Why not check the real file type instead if you really think you have to? – arkascha Jun 17 '16 at 12:39
  • Can You try to use [PHP Mailer](https://github.com/PHPMailer/PHPMailer)? – nekiala Jun 17 '16 at 14:20
  • Mail is coming from this code, without attachment. – sanjay Jun 17 '16 at 16:46

0 Answers0