Forum Moderators: coopster

Message Too Old, No Replies

Adding an Attachment to an Contact Form using PHP

I'm trying to add an attachment to my contact form using PHP

         

havexohope

12:38 am on Feb 22, 2009 (gmt 0)

10+ Year Member



I have an email form and I need to add an attachment. I used the same exact form that I use for a regular contact form that works perfectly fine and just added what I've been seeing on other sites. However, now it says "sent" but it won't send it to my email. I didn't change anything just added a few things.. Perhaps someone can tell me what is wrong with the script?

<?php

$mailto = 'alexa@example.com' ;

// $subject - set to the Subject line of the email, eg
//$subject= "Feedback Form" ;

$subject = "CONTACT FORM" ;

// the pages to be displayed, eg
//$formurl= "http://www.example.com/feedback.html" ;
//$errorurl= "http://www.example.com/error.html" ;
//$thankyouurl= "http://www.example.com/thankyou.html" ;

$formurl = "index.html" ;
$errorurl = "index.html" ;
$thankyouurl = "thankyou.html" ;

$attachment = file_get_contents($filename);

$uself = 0;
$email_is_required = 0;
$name_is_required = 0;

// -------------------- END OF CONFIGURABLE SECTION ---------------

$headers = "Content-Type: multipart/mixed;\n";
$headers .= " boundary=\"simple-boundary\"\n";
$headers .= "From: $from";

$Name = $_POST['Name'] ;
$Email = $_POST['Email'] ;
$Telephone = $_POST['Telephone'] ;
$Request = $_POST['Request'] ;
$http_referrer = getenv( "HTTP_REFERER" );

if (!isset($_POST['Email'])) {
header( "Location: $formurl" );
exit ;
}
if (($Email_is_required && (empty($Email) ¦¦ !ereg("@", $Email))) ¦¦ ($Name_is_required && empty($Name))) {
header( "Location: $errorurl" );
exit ;
}
if ( ereg( "[\r\n]", $Name ) ¦¦ ereg( "[\r\n]", $Email ) ) {
header( "Location: $errorurl" );
exit ;
}
if (empty($Email)) {
$Email = $mailto ;
}

if (get_magic_quotes_gpc()) {
$Request = stripslashes( $Request );

}

$message = "\n--simple-boundary\n";
$message .= "Content-Type: text/plain\n\n";
$message .= "This message was sent from:\n" .
"$http_referrer\n" .
"------------------------------------------------------------\n" .
"Name of sender: $Name\n" .
"Email of sender: $Email\n" .
"Question or Comments: $Request" .
"\n------------------------------------------------------------\n" ;

// Attachment part
$message .= "--simple-boundary\nContent-Type: text/plain\n";
$message .= "Content-Disposition: attachment;\n";
$message .= " filename=\"$filename\"\n\n";
$message .= "$attachment\n\n";

mail($to, "Your attachment", $message, $headers);
header( "Location: $thankyouurl" );
exit ;

?>

[edited by: eelixduppy at 7:59 am (utc) on Feb. 22, 2009]
[edit reason] exemplified [/edit]

VarX

3:58 am on Feb 22, 2009 (gmt 0)

10+ Year Member



this will help you out to send mails with attachements copy the script and test.

<?php

function SendMail($to,$subject,$MessageBody,$from,$cc,$attachmentname,$attachmenttype)
{

$random_hash = md5(date('r', time()));
$headers = "From: $from\r\n";
if($cc!=null)
{
$headers .= "Cc: $cc";
}
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";
if($attachmentname==null)
{
$attachment=null;
}
else
{
$attachment = chunk_split(base64_encode(file_get_contents($attachmentname)));
}
ob_start();
?>
--PHP-mixed-<?php echo $random_hash; ?>
Content-Type: multipart/alternative; boundary="PHP-alt-<?php echo $random_hash; ?>"

--PHP-alt-<?php echo $random_hash; ?>
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

<? echo $MessageBody;?>

--PHP-alt-<?php echo $random_hash; ?>--
<? if($attachment!=null){ ?>
--PHP-mixed-<?php echo $random_hash; ?>
Content-Type: <? echo $attachmenttype?>; name=<? echo $attachmentname; ?>
Content-Transfer-Encoding: base64
Content-Disposition: attachment

<?php echo $attachment; ?>

--PHP-mixed-<?php echo $random_hash; ?>--

<?php
}
$message = ob_get_clean();
$mail_sent = @mail( $to, $subject, $message, $headers );

if($mail_sent)
{
return true;
}
else
{
return false;
}
}
#edit bellow here and test
$MessageBody = 'This is the text included in Message Body';
if(SendMail('yourmail@domain.com','My Test Mail',$MessageBody,'MyTest',null,'yourattachement.ext','your attachement MIME eg.image/png')) {
echo 'Mail Sent';
}
else {
echo 'Not Done';
}

?>

havexohope

8:21 pm on Feb 22, 2009 (gmt 0)

10+ Year Member



but it says attachment=null. What do I name the input on the other page? I have it set to attachment right now.

havexohope

8:25 pm on Feb 22, 2009 (gmt 0)

10+ Year Member



Warning: file_get_contents(yourattachement.ext) [function.file-get-contents]: failed to open stream: No such file or directory in /hermes/bosweb/web140/b1400/d5.dagostinohair/public_html/blackrhino/diction/test.php on line 19
Mail Sent

this is what it's saying to me...

havexohope

8:26 pm on Feb 22, 2009 (gmt 0)

10+ Year Member



I atuallyu got the email but without an attachment and I had this as my content:

Content-Type: multipart/mixed; boundary="PHP-mixed-8d585797103165262d373602f96dbddd"

--PHP-mixed-8d585797103165262d373602f96dbddd
Content-Type: multipart/alternative; boundary="PHP-alt-8d585797103165262d373602f96dbddd"

--PHP-alt-8d585797103165262d373602f96dbddd
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

This is the text included in Message Body
--PHP-alt-8d585797103165262d373602f96dbddd--

--PHP-mixed-8d585797103165262d373602f96dbddd
Content-Type: your attachement MIME eg.image/png; name=yourattachement.ext
Content-Transfer-Encoding: base64
Content-Disposition: attachment

--PHP-mixed-8d585797103165262d373602f96dbddd--

havexohope

8:48 pm on Feb 22, 2009 (gmt 0)

10+ Year Member



So okay I used a different script and I got it to attach SOMETHING but it wasn't want I wanted to attach.

Warning: fopen(file.ext) [function.fopen]: failed to open stream: No such file or directory in /hermes/bosweb/web140/b1400/d5.dagostinohair/public_html/blackrhino/diction/test2.php on line 20

Warning: filesize() [function.filesize]: stat failed for file.ext in /hermes/bosweb/web140/b1400/d5.dagostinohair/public_html/blackrhino/diction/test2.php on line 21

Warning: fread(): supplied argument is not a valid stream resource in /hermes/bosweb/web140/b1400/d5.dagostinohair/public_html/blackrhino/diction/test2.php on line 21

Warning: fclose(): supplied argument is not a valid stream resource in /hermes/bosweb/web140/b1400/d5.dagostinohair/public_html/blackrhino/diction/test2.php on line 22

these were the error messages I received and below is the code I used.. This is extremely important to get done. Sorry!

<?php
// array with filenames to be sent as attachment
$files = array("file.ext");
// email fields: to, from, subject, and so on
$to = "email@example.com";
$from = "name";
$subject ="TEST";
$message = "My message";
$headers = "From: $from";
// boundary
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
// headers for attachment
$headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";
// multipart boundary
$message = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n";
$message .= "--{$mime_boundary}\n";
// preparing attachments
for($x=0;$x<count($files);$x++){
$file = fopen($files[$x],"rb");
$data = fread($file,filesize($files[$x]));
fclose($file);
$data = chunk_split(base64_encode($data));
$message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$files[$x]\"\n" .
"Content-Disposition: attachment;\n" . " filename=\"$files[$x]\"\n" .
"Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
$message .= "--{$mime_boundary}\n";
}
// send
$ok = @mail($to, $subject, $message, $headers);
if ($ok) {
echo "<p>mail sent to $to!</p>";
} else {
echo "<p>mail could not be sent!</p>";
}
?>

[edited by: eelixduppy at 9:22 pm (utc) on Feb. 22, 2009]
[edit reason] specifics [/edit]

VarX

6:23 am on Feb 25, 2009 (gmt 0)

10+ Year Member



in my first example when you call SendMail function, remember the data i have mentioned just dummy's please use correct ones for the file name you can use something like image.jpg but the same file must exist in the same directory or give the full path to the other directory. also put the correct MIME type if you have attached a PNG file use image/png and your email address.

check the bellow

SendMail('email@example.com','My Test Mail','Test Message Body','MyTest',null,'image.png','image/png');

[edited by: eelixduppy at 6:58 am (utc) on Feb. 25, 2009]
[edit reason] specifics [/edit]

franco190453

3:07 pm on Feb 25, 2009 (gmt 0)

10+ Year Member



in order to handle other file types you need to add something like this ->

$type = stristr($file,'.');

if($type == ".jpg") {
$message .= "Content-Type: image/jpg; name=\"$name\"\n";
}
ETC...

Regards
Franco