Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-07-2012, 11:54 PM   PM User | #1
chellert
New Coder

 
Join Date: Mar 2012
Location: Ontario, Canada
Posts: 47
Thanks: 8
Thanked 0 Times in 0 Posts
chellert is an unknown quantity at this point
Sending Attachments through PHP Script

I am trying to send an email using PHP script but it seems that the attachments wants to convert to text and embed into the email.

this is the code, not sure why its embedding.

$fileatt = $_FILES['attach']['tmp_name'];
$fileatt_name = basename($_FILES['attach']['name']);
$fileatt_type = substr($fileatt_name, strrpos($fileatt_name, '.') + 1);


$file = fopen($fileatt,'rb');
$data = fread($file,filesize($fileatt));
fclose($file);

$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .

$body .= "This is a multi-part message in MIME format.\n\n" .
"--{$mime_boundary}\n" .
"Content-Type:text/html; charset=\"iso-8859-1\"\n" .
"Content-Transfer-Encoding: 7bit\n\n" .

$body .= "\n\n";

$data = chunk_split(base64_encode($data));

$body .= "-{$mime_boundary}\n" .
"Content-Type: {$fileatt_type};\n" .
" name=\"{$fileatt_name}\"\n" .
"Content-Disposition: attachment;\n" .
" filename=\"{$fileatt_name}\"\n" .
"Content-Transfer-Encoding: base64\n\n" .
$body .= "\n\n" .
"-{$mime_boundary}-\n";

mail($email_to, $subject, $body, $headers);
chellert is offline   Reply With Quote
Old 11-08-2012, 12:00 AM   PM User | #2
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,521
Thanks: 45
Thanked 440 Times in 429 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
Use the phpmailer class instead (find it via google). It takes all the hassle out of doing stuff like this the hard way.
__________________
Please wrap your code in [php] tags. It is a sticky topic and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.
tangoforce is offline   Reply With Quote
Old 11-08-2012, 06:11 PM   PM User | #3
chellert
New Coder

 
Join Date: Mar 2012
Location: Ontario, Canada
Posts: 47
Thanks: 8
Thanked 0 Times in 0 Posts
chellert is an unknown quantity at this point
OK, I got the PHPMailer working. Since I pull all of the names from a Database I had to use the AddBCC line.

Is there a way to clear the AddAddress, every time I go through the script? So their names show up in the To line and ONLY their email address shows up there? This way there is no distribution of an entire list
chellert is offline   Reply With Quote
Old 11-08-2012, 07:10 PM   PM User | #4
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,521
Thanks: 45
Thanked 440 Times in 429 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
Yes - ClearAddresses()

If you use notepad++ it has an option in the 'view' menu called 'Fold all'.

Click that and then click the little + symbol by the line numbers. It will expand the class file so you can see the function names only without the function code. It makes it a lot easier to see what functions are available to you.
__________________
Please wrap your code in [php] tags. It is a sticky topic and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.
tangoforce is offline   Reply With Quote
Users who have thanked tangoforce for this post:
chellert (11-17-2012)
Old 11-09-2012, 02:18 AM   PM User | #5
minder
Banned

 
Join Date: Oct 2012
Posts: 81
Thanks: 0
Thanked 4 Times in 4 Posts
minder can only hope to improve
This is a pretty good and popular tutorial on how you can send plain text emails, html emails and emails with attachments from webcheatsheets.com.

http://webcheatsheet.com/php/send_em...attachment.php
minder is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:34 AM.


Advertisement
Log in to turn off these ads.