How to attach a written file to an email

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
Tokunbo
Forum Commoner
Posts: 46
Joined: Thu Sep 29, 2011 8:53 am

How to attach a written file to an email

Post by Tokunbo »

hello sirs,

I have made the following observations:

1) If I have a simple form of about 5 or 10 fields, which a user is supposed to fill - to be sent to an email address: this is a sample code I sourced from the internet.

Code: Select all

<?php
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$call = $_POST['call'];
$website = $_POST['website'];
$priority = $_POST['priority'];
$type = $_POST['type'];
$message = $_POST['message'];
[b]$formcontent[/b]=" From: $name \n Phone: $phone \n Call Back: $call \n Website: $website \n Priority: $priority \n Type: $type \n Message: $message";
$recipient = "YOUREMAIL@HERE.COM";
$subject = "Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You!" . " -" . "<a href='form.html' style='text-decoration:none;color:#ff0099;'> Return Home</a>";
?>
The above works fine - from my observation with just a couple of fields (inputs, etc).

I have a very very long code with several variables - a table actually, which I want to send by email. I have tested my code using echo statements to display / make sure my outputs are correct and displayed as I want them to be.

So I have them outputted through a single $formcontent - using the above as an example, as in the above with several line breaks in several rows.

My challenge is that if I use the code above, with just a handful of variables, the email goes successfully, but once I paste my code, it doesnt. No errors are displayed anywhere, I can see the Thank you echo - its as if the email went successfully but I dont see the email in my mail box.

So I thought that perhaps because of the size of the file, etc.,

So I set about writing the output into a text file. I have done this and the output is written to a text file in the same DIR as my script is. Now, I want to attach it(written text file) to an email address - hoping that perhaps the above code will work.

Informations gathered on the net include re-reading the text file into a single variable ex: $data and sending the contents to an email address, which is not what I want to do.

What I want to do is the send the text file as an attachment, so that the mail reads something like "please find attached" - then the text file is attached to the email.

questions:
1) is there a limit to the size of content that can be used as a variable, example: $formcontent in code:
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");

2) Now that Ive written the output to a text file, how can I send the written file as an attachment to my mail box? not as an inline pasted text information.

regards
Tokunbo
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: How to attach a written file to an email

Post by social_experiment »

Tokunbo wrote:1) is there a limit to the size of content that can be used as a variable, example: $formcontent in code:
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
Not that i'm aware of. The problem is likely to be in the "code you paste", as you refer to it. Is it an html table that you want to send?
Tokunbo wrote:2) Now that Ive written the output to a text file, how can I send the written file as an attachment to my mail box? not as an inline pasted text information.
I can only point you into a better direction because i don't have any experience with this but a search with the following keywords Sending attachments with php mail() returns quite a few results which should be more helpful than my attempts :)
“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
Tokunbo
Forum Commoner
Posts: 46
Joined: Thu Sep 29, 2011 8:53 am

Re: How to attach a written file to an email

Post by Tokunbo »

hi there,

thanks for the reply.

I stumbled on this post: http://www.daniweb.com/web-development/ ... eads/60795

I created a form with a single input and a submit button, just as the poster described. What I get is: Sorry but the email could not be sent. Please go back and try again!

Suppose my mypdffile.pdf is in the same DIR as my script, is the line below ok as is? or do I need to specify the path from /home/.....

Code: Select all

$fileatt = "mypdffile.pdf"; // Path to the file 
thanks
Tokunbo
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: How to attach a written file to an email

Post by social_experiment »

Yeah the path to the file is ok, paste the code that you are using to send the email
“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
Post Reply