I finally found a free mail form with attachment, but it doesn’t work for 100%
The hardest part (I think), sending the attachment works, but the form contains few errors, so the content isn’t visible when you recive the mail.
I don’t really know where in the body of the mail, i have to echo the actuall message(form is now exactly as i found it, just few minor changements like i edited the names of the vars, but that’s not the problem)
Can someone help me please?
Thanks!!!
<?
/* Mailer with Attachments */
$action = $_REQUEST['action'];
global $action;
function showForm() {
?>
<form id="sendproject" enctype="multipart/form-data" name="send" method="post" action="<?=$_SERVER['PHP_SELF']?>">
<input type="hidden" name="action" value="send" />
<label>Name:</label>
<input class="text" name="name" size="50" />
<label>Subject:</label>
<input class="text" name="subject" size="50" />
<label>Email:</label>
<input class="text" name="email" size="50" />
<label>Project:</label>
<input class="text" name="project" size="50" />
<label>Attachment:</label>
<input class="text" type="file" name="attachment" size="50" />
<label>Message:</label>
<textarea name="body" rows="10" cols="50"></textarea>
<input type="submit" name="submit" value="Submit" /></p>
</form>
<?
}
function sendMail() {
$MAX_FILE_SIZE = "10000000";
if (!isset ($_POST['email'])) { //Oops, forgot your email addy!
die ("<p>Oops! You forgot to fill out the email address! Click on the back arrow to go back</p>");
}
else {
$name = stripslashes($_POST['name']);
$subject = stripslashes($_POST['subject']);
$body = stripslashes($_POST['body']);
$email = $_POST['email'];
$attachment = $_FILES['attachment']['tmp_name'];
$attachment_name = $_FILES['attachment']['name'];
if (is_uploaded_file($attachment)) { //Do we have a file uploaded?
$fp = fopen($attachment, "rb"); //Open it
$data = fread($fp, filesize($attachment)); //Read it
$data = chunk_split(base64_encode($data)); //Chunk it up and encode it as base64 so it can emailed
fclose($fp);
}
//Let's start our headers
$headers = "From: $from_name<" . $email . ">\
";
$headers .= "Reply-To: <" . $email . ">\
";
$headers .= "MIME-Version: 1.0\
";
$headers .= "Content-Type: multipart/related; type=\\"multipart/alternative\\"; boundary=\\"----=MIME_BOUNDRY_main_message\\"\
";
$headers .= "X-Sender: $from_name<" . $_POST['from_email'] . ">\
";
$headers .= "X-Mailer: PHP4\
";
$headers .= "X-Priority: 3\
"; //1 = Urgent, 3 = Normal
$headers .= "Return-Path: <" . $_POST['from_email'] . ">\
";
$headers .= "This is a multi-part message in MIME format.\
";
$headers .= "------=MIME_BOUNDRY_main_message \
";
$headers .= "Content-Type: multipart/alternative; boundary=\\"----=MIME_BOUNDRY_message_parts\\"\
";
$message = "------=MIME_BOUNDRY_message_parts\
";
$message .= "Content-Type: text/plain; charset=\\"iso-8859-1\\"\
";
$message .= "Content-Transfer-Encoding: quoted-printable\
";
$message .= "\
";
/* Add our message, in this case it's plain text. You could also add HTML by changing the Content-Type to text/html */
$message .= " Het geschreven bericht: \
<br />".$body;
$message .= "\
";
$message .= "------=MIME_BOUNDRY_message_parts--\
";
$message .= "\
";
$message .= "------=MIME_BOUNDRY_main_message\
";
$message .= "Content-Type: application/octet-stream;\
\ name=\\"" . $attachment_name . "\\"\
";
$message .= "Content-Transfer-Encoding: base64\
";
$message .= "Content-Disposition: attachment;\
\ filename=\\"" . $attachment_name . "\\"\
\
";
$message .= $data; //The base64 encoded message
$message .= "\
";
$message .= "------=MIME_BOUNDRY_main_message--\
";
// send the message
mail("altin.t@gmail.com", $subject, $message, $headers);
}
}
switch ($action) {
case "send":
sendMail();
showForm();
break;
default:
showForm();
}
?>
Ok well I donwnloaded tha class, but I really don’t understand how to add a contact form to it.
I’d like to send the mails with the php mail function (i think this is the default option on the script) but I don’t see how to use the class.
I supose I ned to include it in the html form to start with, but then what?
I think this is the last question;
I understand how to link this with a form etc, but not the file attachment.
This is the php code: AddAttachment($path);
But it just asks for the path.
Will I need another script to upload it first, and then get the path or does this class support the uploading too?
The html would look something like this:
<input class=“text” type=“file” name=“attachment” size=“50” />
Or do i only need to do something like $path = $_POST[‘attachment’]; ?
Hey,
thanks again for replying, but it still doesn’t work.
This is the code I have now:
<?php
require("class.phpmailer.php");
if($_POST['submit']){
$mail = new PHPMailer();
$mail->IsMail(); // telling the class to use SMTP
$mail->From = "test@testing.com";
$mail->AddAddress("altin.t@gmail.com", "Altin Toooola");
$mail->Subject = "testing phpmailer thing";
$mail->Body = "Hi! \
\
This is my first e-mail sent through PHPMailer.";
$mail->WordWrap = 50;
$path = $_FILES['userfile']['tmp_name'];
$name = $_FILES['userfile']['name'];
$encoding = "base64";
$type = "image/gif";
$mail->AddStringAttachment($string,$filename,$encoding,$type);
if(!$mail->Send())
{
echo 'Message was not sent.';
echo 'Mailer error: ' . $mail->ErrorInfo;
}
else
{
echo 'Message has been sent.';
}
}
else{
?>
<form action="test.php" method="post" enctype="multipart/form-data">
<input class="text" type="file" name="attachment" size="50" />
<input type="submit" name="submit" value="Submit" class="form-button" />
</form>
<?
}
?>
I tryed it with the 3 different possibilitys:
AddAttachment($path);
AddAttachment($path, $name);
AddStringAttachment($string,$filename,$encoding,$type);
With the 2 first ones, I get the message that the mail is sent, but I can’t see an attachment.
With the last one, I sort of get an attachment:
I see there’s an attachment, but I don’t see a preview, it’s called “noname” and when i try to see it, it doesn’t load neether.