Jump to content

Mail() With Pdf Attachment


djp1988

Recommended Posts

How can I send an attached file with the mail() function ?I've been looking round google, but all the scripts I've tried have not sent email at all...

Link to comment
Share on other sites

Hi, thanks, this was actually one of the first scripts i tested, and it doesn't seem to send anything, the message i get is "Mail Sent" but i don't ever receive anything in my emails....

Link to comment
Share on other sites

If none of the scripts work then the problem isn't that all of the code is wrong, there's probably something wrong with your server. Just use this to test, once you get this working then you can figure out the attachment issue:

<?phperror_reporting(E_ALL);ini_set('html_errors', 1);ini_set('display_errors', 1);mail('you@domain.com', 'test subject', 'test message', 'From: test@test.com');?>

Link to comment
Share on other sites

On the server now it does send an email, and it's size is correct, but instead of an attachment, my pdf file, I am getting hundreds of lines of the raw data, this is using the original script suggested...What could be going wrong ? There are no PHP warnings or errors...

--PHP-mixed-cb705b843a12a73c7da2d0e1ab179663  Content-Type: multipart/alternative; boundary="PHP-alt-cb705b843a12a73c7da2d0e1ab179663" --PHP-alt-cb705b843a12a73c7da2d0e1ab179663  Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bitHello World!!! This is simple text email message. --PHP-alt-cb705b843a12a73c7da2d0e1ab179663  Content-Type: text/html; charset="iso-8859-1" Content-Transfer-Encoding: 7bit<h2>Hello World!</h2> <p>This is something with <b>HTML</b> formatting.</p> --PHP-alt-cb705b843a12a73c7da2d0e1ab179663-- --PHP-mixed-cb705b843a12a73c7da2d0e1ab179663  Content-Type: application/pdf; name="mydocument.pdf"  Content-Transfer-Encoding: base64  Content-Disposition: attachment  JVBERi0xLjUNCiW1tbW1DQoxIDAgb2JqDQo8PC9UeXBlL0NhdGFsb2cvUGFnZXMgMiAwIFIvTGFuZyhlbi1HQikgL1N0cnVjdFRyZWVSb290IDIwIDAgUi9NYXJrSW5mbzw8L01hcmtlZCB0cnVlPj4+Pg0KZW5kb2JqDQoyIDAgb2JqDQo8PC9UeXBlL1BhZ2VzL0NvdW50IDEvS2lkc1sgMyAwIFJdID4+DQplbmRvYmoNCjMgMCBvYmoNCjw8L1R5cGUvUGFnZS9QYXJlbnQgMiAwIFIvUmVzb3VyY2VzPDwvRm9udDw8L0YxIDUgMCBSL0YyIDggMCBSL0YzIDEwIDAgUi9GNCAxMiAwIFIvRjUgMTQgMCBSPj4vRXh0R1N0YXRlPDwvR1M3IDcgMCBSPj4vUHJvY1NldFsvUERGL1RleHQvSW1hZ2VCL0ltYWdlQy9JbWFnZUldID4+L01lZGlhQm94WyAwIDAgNTk1LjMyIDg0MS45Ml0gL0NvbnRlbnRzIDQgMCBSL0dyb3VwPDwvVHlwZS9Hcm91cC9TL1RyYW5zcGFyZW5jeS9DUy9EZXZpY2VSR0I+Pi9UYWJzL1MvU3RydWN0UGFyZW50cyAwPj4NCmVuZG9iag0KNCAwIG9iag0KPDwvRmlsdGVyL0ZsYXRlRGVjb2RlL0xlbmd0aCAyNDE5Pj4NCnN0cmVhbQ0KeJylWVtv28oRfjfg/7B9E4GjDe+Xg8CA4ySnKXraoDHQh4MioCVaYk1dQspO9e+7M7Oz3OVFdlEEkUVyOTv7ze2bkXj3Vbx//+73uy8fhX9zIz58vBMf7q+v3n0O[...]--PHP-mixed-cb705b843a12a73c7da2d0e1ab179663--

Link to comment
Share on other sites

following these errors, I decided to try out PHPMailer again, I'd had problems getting it up and running, down to the GoDaddy shared hosting server I was using, but when i try to send email with it I'm getting:Could not instantiate mail function. Mailer Error: Could not instantiate mail function.I've been researching why this is the case and the same thing keeps on popping up, I need to install an SMTP server to use mail() function directly or via phpmailer, but... that can't be the problem, because I can send email with mail(), just seems a problem if i use phpmailer...any thoughts ?

Link to comment
Share on other sites

Okay I'm up and running !but.... there always a but, some email addresses are not getting a pdf attachment, they're getting as I described before, thousands of lines of raw data, on yahoo etc it's fine but on others its not, maybe the headers are not being prepared correctly? :

<?phpfunction mail_attachment($filename, $path, $mailto, $from_mail, $from_name, $replyto, $subject, $message) {	$file = $path.$filename;	$file_size = filesize($file);	$handle = fopen($file, "r");	$content = fread($handle, $file_size);	fclose($handle);	$content = chunk_split(base64_encode($content));	$uid = md5(uniqid(time()));	$name = basename($file);	$header = "From: ".$from_name." <".$from_mail.">\r\n";	$header .= "Reply-To: ".$replyto."\r\n";	$header .= "MIME-Version: 1.0\r\n";	$header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";	$header .= "This is a multi-part message in MIME format.\r\n";	$header .= "--".$uid."\r\n";	$header .= "Content-type:text/plain; charset=iso-8859-1\r\n";	$header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";	$header .= $message."\r\n\r\n";	$header .= "--".$uid."\r\n";	$header .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n"; // use different content types here	$header .= "Content-Transfer-Encoding: base64\r\n";	$header .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";	$header .= $content."\r\n\r\n";	$header .= "--".$uid."--";	if (mail($mailto, $subject, "", $header)) {		echo "mail send ... OK"; // or use booleans here	} else {		echo "mail send ... ERROR!";	}}$my_file = "document.pdf";$my_path = "";$my_name = "my name";$my_mail = "a@b.com";$my_replyto = "a@b.com";$my_subject = "Your email !";$my_message = "Something here";mail_attachment($my_file, $my_path, $_POST['email'], $my_mail, $my_name, $my_replyto, $my_subject, $my_message);?>

Link to comment
Share on other sites

no I can't :sIt works for yahoo emails, and orange emails, but not on hotmail and I'm about to test on gmail...so the php script seems to not be the way forward, I'd use PHPMailer but I can't get it to run on my server, something to do with smtp ? I'm not sure... Where can I go from here... ?

Link to comment
Share on other sites

I would send that email text to tech support at hotmail and ask them why it doesn't go through. They might also have public documentation you can look over. You could also create an email with an attachment in hotmail and email it to a system where you can use a script to grab the raw email and compare it for differences with yours.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...