/    Sign up×
Community /Pin to ProfileBookmark

Sending Email Attachments Via PHP

I dont know what I should name the PHP’s. Also I have the one that is an application and it looks great, whats the html to add a file upload box? Once that is done I need to create another PHP like a miler.php, but im type confused. So all I have to do is edit the mailer php? So then what is the actual code for it then?

This is it, but its really confusing.

[QUOTE]

$mime_boundary = “<<<–==+X[“.md5(time()).”]”;

Sort out your basic headers as usual, by piling them into a $headers string.

$headers .= “From: Automatic <[email protected]>rn”;
$headers .= “To: SomeName <[email protected]>rn”;
Then you’ve got to specify the type of content you’re dealing with. This is also put into the $headers string. In this case, it will be mixed between text, and some kind of attachment, hence the Content-Type bit. The boundary bit uses the boundary variable you made earlier:

$headers .= “MIME-Version: 1.0rn”;
$headers .= “Content-Type: multipart/mixed;rn”;
$headers .= ” boundary=””.$mime_boundary.”””;
Next, get on with piling the message into a $message string. You need quite a bit of header looking stuff in here. The first bit is what is displayed if the client receiveing the email doesn’t support MIME, followed by the boundary string (preceded by “–“, not sure why).

$message .= “This is a multi-part message in MIME format.rn”;
$message .= “rn”;
$message .= “–“.$mime_boundary.”rn”;
Next comes the actual content of the email, each part needs it’s own mini header describing what it is.

$message .= “Content-Type: text/plain; charset=”iso-8859-1″rn”;
$message .= “Content-Transfer-Encoding: 7bitrn”;
$message .= “rn”;
$message .= “Email content and what not: rn”;
$message .= “This is the file you asked for! rn”;
$message .= “–“.$mime_boundary.”rn”;
Finally, a plaintext attachment. In this case I already put your contents into $fileContent.

$message .= “Content-Type: application/octet-stream;rn”;
$message .= ” name=”filename.extn”rn”;
$message .= “Content-Transfer-Encoding: quoted-printablern”;
$message .= “Content-Disposition: attachment;rn”;
$message .= ” filename=”filename.extn”rn”;
$message .= “rn”;
$message .= $fileContent;
$message .= “rn”;
$message .= “–“.$mime_boundary.”rn”;
Then send the thing, using the mail function as described in the manual. You will probably have to have a few goes at this before you get it right. The linbreaks need to be exactly right for this to work, and if it doesn’t, that’s probably why.

$ok = mail(“[email protected]“, “file by email”, $message, $headers);

[/QUOTE]

to post a comment
PHP

16 Comments(s)

Copy linkTweet thisAlerts:
@Baby_JaiauthorAug 18.2003 — can someone please help me with this
Copy linkTweet thisAlerts:
@brendandonhueAug 18.2003 — HTML for an upload box is

<input type="file">

I need some more detail before I can help ya....
I dont know what I should name the PHP's.[/QUOTE]
What do you mean? Are you talking about the filenames of the PHP scripts?
Also I have the one that is an application[/QUOTE]
Have one what thats an application?


What is miler.php? What do you want miler.php to do?

Whats the actual code for what?
Copy linkTweet thisAlerts:
@Baby_JaiauthorAug 18.2003 — Ok here is my issue, if you go [URL=http://www.babyjai.com/application.php]here[/URL] , i need to send a email attachment with this. Its going to be a picture. I just need to know what I need to add to it. Any help on this would be great
Copy linkTweet thisAlerts:
@brendandonhueAug 18.2003 — Why don't you just use the code in your first post?

Or are you having problems with it?
Copy linkTweet thisAlerts:
@Baby_JaiauthorAug 18.2003 — all i want to do is add another box in my form with a browse button, and it will automatically send the file attachment once they hit submit. I just dont know hwo about fdoing it. Also that one above has got me lost, there is stuff all over the place.
Copy linkTweet thisAlerts:
@brendandonhueAug 18.2003 — To add the upload box to your form use

<input type="file">

Where did you get that script from? It probably has instructions on how to use it.
Copy linkTweet thisAlerts:
@Baby_JaiauthorAug 18.2003 — That is not going to send it though, although i see where your att but it doesnt send it
Copy linkTweet thisAlerts:
@Baby_JaiauthorAug 18.2003 — see I added it but it doesnt work
Copy linkTweet thisAlerts:
@brendandonhueAug 19.2003 — You still have to submit it to the script you posted in your first post.

You might want to check for instructions that came with the script though.
Copy linkTweet thisAlerts:
@Baby_JaiauthorAug 19.2003 — ahh forget it, its useless
Copy linkTweet thisAlerts:
@pyroAug 19.2003 — Sending attachments in PHP is a complicated task, as you need to use MIME headers, and they are complicated, in themselves. I've never had to send an attachment with PHP, so I can give you much direct help. Just to answer brendandonhue's question, the script can be found at http://www.hollowearth.co.uk/tech/php/email_attachments.php and is used to send a plaintext attachment.
Copy linkTweet thisAlerts:
@Baby_JaiauthorAug 19.2003 — yeah thats the one i was trying to play with and got absolutely no where. I was hoping someone could help me out and help me fix that PHP. No other thread onthe web knows, and I figured you would know pyro ? .
Copy linkTweet thisAlerts:
@pyroAug 19.2003 — When it comes to MIME headers, it seems to just take a lot of playing around. One other possiblity would be to sent the mail using the IMAP mail protocol, perhaps? Though even with that, you may need to play around with the MIME headers....
Copy linkTweet thisAlerts:
@Baby_JaiauthorAug 19.2003 — any help bro is good help, can you do just this last piece of coding for me, anyway that I cant send a jpeg no bigger than 1 Meg would be great?
Copy linkTweet thisAlerts:
@cleromancerAug 20.2003 — This is a working example for you.

[URL]http://www.sitepoint.com/article/679/5[/URL]
Copy linkTweet thisAlerts:
@Baby_JaiauthorAug 20.2003 — anymore confusing than that and i would get lost
×

Success!

Help @Baby_Jai spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.4,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...