Help! Contact form with attachment..
Posted: Fri May 21, 2010 12:09 pm
I am new to PHP and have been trying for a couple of weeks now to succeed at writing a code for sending an email with an attachment. I am able to load the code in a browser, fill in the necessary info and attach the file yet I am constantly getting error codes once submit is pressed. My latest code is, after being told I needed to install a mail server,
Quote:
Warning: mail() [function.mail]: SMTP server response: 530 SMTP authentication is required. in C:\xampp\htdocs\email.php on line 46 Failed.
I installed hMailServer onto my pc which is running on Windows Vista HP. I am clueless on how to alter anything dealing with SMTP. I could not find the php.ini file that quite a few people are talking about, all I could locate is a php.ini.h file. Anyway here's the code I am using:
Can anyone offer me some support?
Quote:
Warning: mail() [function.mail]: SMTP server response: 530 SMTP authentication is required. in C:\xampp\htdocs\email.php on line 46 Failed.
I installed hMailServer onto my pc which is running on Windows Vista HP. I am clueless on how to alter anything dealing with SMTP. I could not find the php.ini file that quite a few people are talking about, all I could locate is a php.ini.h file. Anyway here's the code I am using:
Code: Select all
<?php
if(isset($_POST) && !empty($_POST)) {
if(!empty($_FILES['attachment']['name'])) {
$file_name = $_FILES['attachment']['name'];
$temp_name = $_FILES['attachment']['tmp_name'];
$file_type = $_FILES['attachment']['type'];
$base = basename($file_name);
$extension = substr($base, strlen($base)-4, strlen($base));
$allowed_extensions = array(".doc","docx",".pdf",".png",".jpg");
if(in_array($extension,$allowed_extensions)) {
$from = $_POST['email'];
$to = "myemail@here.com";
$subject = "test with attachment";
$message = "This is a test";
$file = $temp_name;
$content = chunk_split(base64_encode(file_get_contents($file)));
$uid = md5(uniqid(time()));
$header = "From: ".$from."\r\n";
$header .= "Reply-To: ".$replyto."\r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
$header .= "This is multi-part message in MIME format. \r\n";
$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";
$header .= "--".$uid."\r\n";
$header .= "Content-Type: ".$file_type."; name=\"".$file_name."\"\r\n";
$header .= "Content-Transfer-Encoding: base64\r\n";
$header .= "Content-Disposition: attachment; filename=\"".$file_name."\"\r\n\r\n";
$header .= $content."\r\n\r\n";
if (mail($to, $subject, "", $header)) {
echo "Success";
} else {
echo "Failed";
}
exit();
} else {
echo "File type not allowed";
}
}
}
?>
<html>
<body>
<form method="post" action="email.php" enctype="multipart/form-data">
<input type="text" name="email" value="Enter Email"/>
<br>
<input type="file" name="attachment"/>
<br>
<input type="submit" value="Send Mail"/>
</form>
</body>
</html>