|
-
Sep 15th, 2009, 03:23 AM
#1
Thread Starter
New Member
Troubble with sending email including attachement
Hi all,
I have saved a some data and a file in my Database. Now I want to send this data and file via Email.
I have successfully save data in database,
I have successfully retrieve that data from Database.
I have successfully sent an email BUT without attachement.
I am facing a problem when it comes to send an email with attachment. this attachement file can be of any type.
My Code is Below. (addsendcampaign.php)
=======================================================
Code:
$sql2 = "Select * from emailtemplate WHERE ID=$emailmessageID";
$rs2 = mysql_query($sql2);
if(mysql_num_rows($rs2)>0)
{
while($obj2=mysql_fetch_object($rs2)) // Getting the Email Message Information
{
$Subject = $obj2->Subject;
$Message = $obj2->Message;
$FromAddress = $obj2->FromAddress;
$AttFile = $obj2->filedata;// this field contains the Data of the File
$size = @MYSQL_RESULT($rs2,0,"filesize");
$data = @MYSQL_RESULT($rs2,0,"filedata");
$type = @MYSQL_RESULT($rs2,0,"filetype");
$filename = @MYSQL_RESULT($rs2,0,"filename");
$content = str_replace(chr(10), " ", $Message);
$content = str_replace(chr(13), " ", $Message);
// Replace single quotes.
$content = str_replace(chr(145), chr(39), $Message);
$content = str_replace(chr(146), chr(39), $Message);
$content = "Dear ".$fname." ".$lname."<br />".$Message;
//=============== Attach a File =======================
//create a boundary string. It must be unique
$headers .= 'From:'.$FromAddress."\r\n";
//$random_hash = md5(date('r', time()));
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$headers .= "\nMIME-Version: 1.0\n"."Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";
$message = "\n\n" ."--{$mime_boundary}\n" ."Content-Type: text/html; charset=\"iso-8859-1\"\n" ."Content-Transfer-Encoding: 7bit\n\n" .$content . "\n\n";
$attachment = chunk_split(base64_encode($data));
$message .= "--{$mime_boundary}\n" ."Content-Type: {$type};\n" ." name=\"{$filename}\"\n";
$message .= "Content-Disposition: attachment;\n" ." filename=\"{$filename}\"\n";
$message .= "Content-Transfer-Encoding: base64\n\n".$attachment."\n\n" ."--{$mime_boundary}--\n";
//======================================================
//=======================================================
}
}
echo "Header : ".$headers."<br />";
echo "Message : ".$message."<br />";
if(eregi("^[0-9a-z]([-_.]?[0-9a-z])*@[0-9a-z]([-.]?[0-9a-z])*\\.[a-z]{2,4}$",$to)) // Checking if the Email Address is Correct
{
if(mail($to, $Subject, $message ,$headers)) // Sending Email
{
echo "To : ".$to."<br />";
echo "Subject : ".$subject."<br />";
echo "Email Sent!!!!!!!! <br />";
}
else
{
echo "Email Not Sent !!!!!!!!!!<br />";
}
}
else
{
echo "Email Address Was Wrong!!!";
}
===================================================
The Error I received is
=====================================
Warning: mail() [function.mail]: Bad parameters to mail() function, mail not sent
============================================
I hope someone can help me soon. I will be very thankful to you all
Thanks
Last edited by mfaisalnice; Sep 15th, 2009 at 03:59 AM.
-
Sep 15th, 2009, 03:26 PM
#2
Re: Troubble with sending email including attachement
what do your headers look like when you have echoed them out? where did you get all of this code?
you may want to look into using the PHP Mailer class instead, or something.
-
Sep 16th, 2009, 06:38 AM
#3
Thread Starter
New Member
Re: Troubble with sending email including attachement
header out put is
======================================================
Header : From: [email protected] MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="==Multipart_Boundary_x6d877a2bc79ad72f9866783b1823ee9ax"
Message : --==Multipart_Boundary_x6d877a2bc79ad72f9866783b1823ee9ax Content-Type: text/html; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Dear Muhammad Faisal
Email Message here
I am going Home
--==Multipart_Boundary_x6d877a2bc79ad72f9866783b1823ee9ax Content-Type: image/pjpeg; name="Winter.jpg" Content-Disposition: attachment; filename="Winter.jpg" Content-Transfer-Encoding: base64 /9j/4AAQSkZJRgABAgEAYABgAAD/7RBKUGhvdG9zaG9wIDMuMAA4QklNA+0KUmVzb2x1dGlvbgAA AAAQAGAAAAABAAEAYAAAAAEAAThCSU0EDRhGWCBHbG9iYWwgTGlnaHRpbmcgQW5nbGUAAAAABAAA AHg4QklNBBkSRlggR2xvYmFsIEFsdGl0dWRlAAAAAAQAAAAeOEJJTQPzC1ByaW50IEZsYWdzAAAA CQAAAAAAAAAAAQA4QklNBAoOQ29weXJpZ2h0IEZsYWcAAAAAAQAAOEJJTScQFEphcGFuZXNlIFBy aW50IEZsYWdzAAAAAAoAAQAAAAAAAAACOEJJTQP1F0NvbG9yIEhhbGZ0b25lIFNldHRpbmdzAAAA SAAvZmYAAQBsZmYABgAAAAAAAQAvZmYAAQChmZoABgAAAAAAAQAyAAAAAQBaAAAABgAAAAAAAQA1 AviNV+j/AI/9VOlC/wCt/iutc/0rw9h9r/y/nNVe8tklv0XGR5Kikjmvhl24jw/92iFXHvX/AKK6 LCQxg78/L81TJ9pI+5ZaSMb4fog7/V0Ca2kEiYEQRMo7LtQdoaO0aCT+cshJKF2ar6KlVau06wvP ulkDUnuPpe2UnuJO5vJP0ifvWKkpNdb/APQlnZ2Ze7gDwkGApbGggtMEzrIKxEkdOqnaLgHe8F3Y jhJxGg4bGndYqSSnV2n1HExzEymfMQBrKy0lH0Pmu6h//9k= --==Multipart_Boundary_x6d877a2bc79ad72f9866783b1823ee9ax--
Warning: mail() [function.mail]: Bad parameters to mail() function, mail not sent. in /hsphere/local/home/voip321/voipwonders.com/lesco/admin/addsendcampaign.php on line 87
Email Not Sent !!!!!!!!!!
======================================================
I also tried PHPMailer class and it is not working for me as well. I will be very thankful to you for help. any simple tutorial with attachment will be very helpful.
I tried PHPmailer as welll
Code:
require("../include/class.phpmailer.php");
$mail = new PHPMailer();
$mail->From = "[email protected]";
$mail->FromName = "Mailer";
$mail->AddAddress("[email protected]", "M Faisal");
$mail->AddAddress("[email protected]", "M Faisal");
// name is optional
$mail->AddReplyTo("[email protected]", "Faisal");
$mail->WordWrap = 50; // set word wrap to 50 characters
$mail->AddAttachment("/images/about.gif"); // add attachments
$mail->AddAttachment("/imagesp/about.gif", "new.gif"); // optional name
$mail->IsHTML(true); // set email format to HTML
$mail->Subject = "Here is the subject";
$mail->Body = "This is the HTML message body <b>in bold!</b>";
$mail->AltBody = "This is the body in plain text for non-HTML mail clients";
if(!$mail->Send())
{
echo "Message could not be sent. <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}else
{
echo "Message has been sent";
}
In output it says " Message has been sent " while there is no mail in my mailbox.
Thanks
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|