SitePoint Sponsor |
|
User Tag List
Results 1 to 18 of 18
-
Dec 24, 2004, 08:54 #1
Why is the email with attachment not working?
Hi,
I have a sample for sending email with attachment (shown below):
mail.php:
<html>
<head>
<title> Sending Email </title>
</head>
<body>
<?php
// Read POST request params into global vars
$to = $_POST['to'];
$from = $_POST['from'];
$subject = $_POST['subject'];
$message = $_POST['message'];
// Obtain file upload vars
$fileatt = $_FILES['fileatt']['tmp_name'];
$fileatt_type = $_FILES['fileatt']['type'];
$fileatt_name = $_FILES['fileatt']['name'];
$headers = "From: $from";
if (is_uploaded_file($fileatt)) {
// Read the file to be attached ('rb' = read binary)
$file = fopen($fileatt,'rb');
$data = fread($file,filesize($fileatt));
fclose($file);
// Generate a boundary string
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
// Add the headers for a file attachment
$headers .= "\nMIME-Version: 1.0\n" .
"Content-Type: multipart/mixed;\n" .
" boundary=\"{$mime_boundary}\"";
// Add a multipart boundary above the plain message
$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";
// Base64 encode the file data
$data = chunk_split(base64_encode($data));
// Add file attachment to the message
$message .= "--{$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" .
$data . "\n\n" .
"--{$mime_boundary}--\n";
}
// Send the message
$ok = @mail($to, $subject, $message, $headers);
if ($ok) {
echo "<p>Mail sent! Yay PHP!</p>";
} else {
echo "<p>Mail could not be sent. Sorry!</p>";
}
?>
</body>
</html>
index.html:
<html>
<head>
<title>Send an Email</title>
</head>
<body>
<h1>Send an Email</h1>
<form action="mail.php" method="POST" enctype="multipart/form-data">
<p>To: <input type="text" name="to" value="" /><br />
From: <input type="text" name="from" value="" /><br />
Subject: <input type="text" name="subject" value="" /></p>
<p>Messagebr />
<textarea cols="70" rows="20" name="message"></textarea></p>
<p>File Attachment: <input type="file" name="fileatt" /></p>
<p><input type="submit" value="Send" /></p>
</form>
</body>
</html>
Don't know why is it not working....I didn't receive any mails in my Yahoo! mailbox...and how can I have mutliple attachments.
Waiting for your kind advise. Thanks!
FYI this code is from http://www.sitepoint.com/article/advanced-email-php/5
Regards,
Junk
-
Dec 24, 2004, 09:43 #2
- Join Date
- Dec 2003
- Location
- Federal Way, Washington (USA)
- Posts
- 1,524
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Change this:
Code:$ok = @mail($to, $subject, $message, $headers);
Code:$ok = @mail("$to", $subject, $message, $headers);
Music Around The World - Collecting tips, trade
and want lists, album reviews, & more
Showcase your music collection on the Web
-
Dec 24, 2004, 10:08 #3
Hi vinyl-junkie, thanks for your fast reply.
I tried it out. No luck. Still didn't receive any mail.
Where has it gone wrong?
mmm.....Regards,
Junk
I am never more keen to learn...
-
Dec 24, 2004, 10:19 #4
- Join Date
- Dec 2003
- Location
- Federal Way, Washington (USA)
- Posts
- 1,524
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
That should work with the change that I gave you. I've used the same script myself (from Kevin Yank's article, right?).
Try sending the mail to something besides your Yahoo account. It's possible that because of size restrictions on attachments, the mail is getting bounced by Yahoo.
p.s. - I'm working on some sample code right now for multiple attachments. Wish me luck!Music Around The World - Collecting tips, trade
and want lists, album reviews, & more
Showcase your music collection on the Web
-
Dec 24, 2004, 10:27 #5
Yo, it said mail is sent alright, but I received nothing.
May I know the file type you attached? I attached a word doc...shouldn't be a problem though.
Well, best of luck for your multiple attachments. I will be proceeding in the same direction after I figure out what is wrong with this...
Any advise?
Thanks and merry christmas!Regards,
Junk
I am never more keen to learn...
-
Dec 24, 2004, 10:59 #6
Hi,
I tried sending just an email without any attachment. It wote email sent, but I didn't get anything in the inbox. Perhaps it is not the attachment that is causing the problem.
Really confused.
Please advise.
Thanks...Regards,
Junk
I am never more keen to learn...
-
Dec 24, 2004, 11:26 #7
Hi vinyl-junkie, could you please show me your codes? Well, if that is ok. Thanks.
Regards,
Junk
I am never more keen to learn...
-
Dec 24, 2004, 11:40 #8
- Join Date
- Dec 2003
- Location
- Federal Way, Washington (USA)
- Posts
- 1,524
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally Posted by Junk
Code:<?php // Read POST request params into global vars $name = $_POST['name']; $from = $_POST['from']; $subject = $_POST['subject']; $message = $_POST['message']; $noreply = $_POST['noreply']; $ThankyouURL = "http://www.napathon.net/ThankYou.php"; $subject = "Website Feedback - ".stripslashes(trim($subject)); $YourWebsiteURL = "http://www.napathon.net"; $to = "my-addy@domain.com"; // If they call this page direct from the browser, send them away because they havent filled in // the form! if($from == "") { header("location: $YourWebsiteURL"); ## Redirect them to your websites front page exit(); } $from = '"'. $name. '" <'.$from.'>'; if ($noreply == "") { $request_reply = "Reply Requested"; } else { $request_reply = "No Reply Requested"; } // Format the message body $message = " ----------------------------------------------------------------------------- $subject $request_reply ----------------------------------------------------------------------------- $name has visited your website and left the following message: Name: ".$name." Email: ".$from." Message: ".stripslashes($message)." All the best, The Pat Wong Email Robot ".$YourWebsiteURL." "; // Obtain file upload vars $fileatt = $_FILES['fileatt']['tmp_name']; $fileatt_type = $_FILES['fileatt']['type']; $fileatt_name = $_FILES['fileatt']['name']; $headers = "From: $from"; if (is_uploaded_file($fileatt)) { // Read the file to be attached ('rb' = read binary) $file = fopen($fileatt,'rb'); $data = fread($file,filesize($fileatt)); fclose($file); // Generate a boundary string $semi_rand = md5(time()); $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; // Add the headers for a file attachment $headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\""; // Add a multipart boundary above the plain message $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"; // Base64 encode the file data $data = chunk_split(base64_encode($data)); // Add file attachment to the message $message .= "--{$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" . $data . "\n\n" . "--{$mime_boundary}--\n"; } // Send the message $ok = @mail("$to", $subject, $message, $headers); if ($ok) { header("Location: $ThankyouURL"); } else { echo "<p>Mail could not be sent. Sorry!</p>"; } ?>
Music Around The World - Collecting tips, trade
and want lists, album reviews, & more
Showcase your music collection on the Web
-
Dec 24, 2004, 12:04 #9
Thanks vinyl-junkie.
I've tried yours too. Nope, no luck. Still the same situation.
Email sent, but nothing in the inbox.
Sigh...what's wrong?Regards,
Junk
I am never more keen to learn...
-
Dec 24, 2004, 12:11 #10
- Join Date
- Nov 2004
- Location
- Parry Sound, ON
- Posts
- 725
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Give us some OS and version details. Something's wrong server-side and you probably need to speak to your host.
-
Dec 24, 2004, 12:21 #11
Linux, ftp.
thanksRegards,
Junk
I am never more keen to learn...
-
Dec 24, 2004, 12:45 #12
FYI,
I managed to send email using this sample code(below). However, due to my limited knowledge for PHP, I know not how to include multiple attachments feature into this code.
I have seen examples of sending email with attachments using mail ( ); I am playing around with mail( ); function, but just couldn't let it deliver.
<?php
function send_email($to_addr,$cc_addr,$bcc_addr,$subject,$m
essage)
{
//input variables
$from_addr = "blah@yahoo.com";
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "Cc: $cc_addr\r\n";
$headers .= "Bcc: $bcc_addr\r\n";
$path_to_sendmail = "/usr/sbin/sendmail";
$fp = popen("$path_to_sendmail -Am -t", "w");
$num = fputs($fp, "From: $from_addr\n");
$num = fputs($fp, "To: $to_addr\n");
$num += fputs($fp, "Subject: $subject\n");
$num += fputs($fp, "$headers\n\n");
$num += fputs($fp, "$message\n\n");
pclose($fp);
//return infomation
if ($num > 0) { echo "Your mail has been sent out!"; }
else {Sorry there was a problem to send email!"; }?>
Any advise is most appreciated.
Thanks!Regards,
Junk
I am never more keen to learn...
-
Dec 24, 2004, 22:01 #13
Please, can someone help? Thanks a million!
Regards,
Junk
I am never more keen to learn...
-
Dec 24, 2004, 23:43 #14
- Join Date
- Dec 2003
- Location
- Federal Way, Washington (USA)
- Posts
- 1,524
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
See this thread. I have posted a solution there which shows you how to send mail with up to 3 attachments. Enjoy!
Music Around The World - Collecting tips, trade
and want lists, album reviews, & more
Showcase your music collection on the Web
-
Dec 26, 2004, 05:15 #15
Thanks vinyl-junkie!
I am still struggling with how Kelvin's code is not working in my case...
Hope to get some help soon...
Thanks pal and have a great day!Regards,
Junk
I am never more keen to learn...
-
Mar 13, 2005, 08:19 #16
help??
Regards,
Junk
I am never more keen to learn...
-
Mar 13, 2005, 11:17 #17
- Join Date
- Dec 2003
- Location
- Federal Way, Washington (USA)
- Posts
- 1,524
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I understand that you'd like to learn from your mistakes. I'm exactly the same way myself, but the problem with your code is that it doesn't even closely resemble what it should be for sending an email with an attachment.
For example, the mail headers don't have the right stuff in them. At a minimum, they should have:
Code:$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; $headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";
Did you try the script I posted a link to? Does it work for you? Start with that script, make sure it works on your server, then go from there.
Hope this helps.Music Around The World - Collecting tips, trade
and want lists, album reviews, & more
Showcase your music collection on the Web
-
Sep 18, 2005, 14:13 #18
Thanks for your advise! Have a great day!
Regards,
Junk
I am never more keen to learn...
Bookmarks