Help! Contact form with attachment..

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Sassci
Forum Newbie
Posts: 2
Joined: Fri May 21, 2010 12:06 pm

Help! Contact form with attachment..

Post by Sassci »

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:

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>
Can anyone offer me some support?
Last edited by Sassci on Tue May 25, 2010 12:43 pm, edited 1 time in total.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Help!

Post by social_experiment »

Sassci wrote: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.
php.ini is located in C:\xampp\php\
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Sassci
Forum Newbie
Posts: 2
Joined: Fri May 21, 2010 12:06 pm

Re: Help!

Post by Sassci »

Ummmm... I am not seeing a php.ini file in the Xampp/php area. It doesn't appear to exist in the version I have. Just the php.ini.h file.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Help!

Post by social_experiment »

ok, have you tried re-installing xampp?
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Help!

Post by Jonah Bron »

Forum Rules wrote:2. Use descriptive subjects when you start a new thread. Vague titles such as "Help!", "Why?" are misleading and keep you from receiving an answer to your question.
We realize you are frustrated with your problem, but please use relevant subject lines.
Post Reply