c# - PHP Mail using HTTP POST Attachments -
i developing php service windows phone based client application. invoking service through client(wp7) , want service send attachment sent part of email. hence using http post, set $to, $message, $subject , $from inside php, , use same logic attachments, after invoking service email on mailbox, corrupt attachment. attachment contains garbage value. here php code
$filenames = $_post['attachment']; $to = $_post['to']; $subject = $_post['subject']; $messagetxt = $_post['message']; $headers = "from: " . $_post['from']; $attachmentname = $_post['attachmentname']; $rand_seed = md5(time()); $mime_boundary = "==multipart_boundary_x{$rand_seed}x"; $headers .= " \r\nmime-version: 1.0\r\n" ."content-type: multipart/mixed;\r\n" ." boundary=\"{$mime_boundary}\"\r\n"; $message .= "this multi-part message in mime format.\n\n" ."--{$mime_boundary}\n\n" . $messagetxt . "\n\n"; $message .= "--{$mime_boundary}\n"; $data = $filenames; $message .= "content-type: {\"application/octet-stream\"};\n" ." name=\"$attachmentname\"\n" ."content-disposition: attachment;\n" . " filename=\"$attachmentname\"\n" ."content-transfer-encoding: base64\n\n" . $data . "\n\n"; $message .= "--{$mime_boundary}\n"; $mail_sent = @mail( $to, $subject, $message, $headers ); echo $mail_sent ? "mail sent" : "mail failed";
on client side using following code ->
var = "xyz@gmail.com"; var = "xyz@gmail.com"; var subj = "hey"; var msg = "oh yeah!"; memorystream ms = new memorystream(); streamwriter sw = new streamwriter(ms); sw.write("hi there!"); sw.flush(); httpwebrequest request = (httpwebrequest)webrequest.create("http://rushabhgosar.com/api/email/v3/index.php"); request.method = "post"; request.contenttype = "application/x-www-form-urlencoded"; string postdata = string.format("to={0}&from={1}&subject={2}&message={3}&attachment={4}&attachmentname={5}", to, from, subj, msg, convert.tobase64string(ms.getbuffer()), "1.txt"); try { request.begingetrequeststream (result => { try { // sending request. using (var requeststream = request.endgetrequeststream(result)) { using (streamwriter writer = new streamwriter(requeststream)) { writer.write(postdata); writer.flush(); } } } catch (exception e) { } // getting response. request.begingetresponse(responseresult => { try { var webresponse = request.endgetresponse(responseresult); using (var responsestream = webresponse.getresponsestream()) { using (var streamreader = new streamreader(responsestream)) { string srresult = streamreader.readtoend(); } } } catch (exception e) { } }, null); }, null); } catch (exception e) { } }
my attachment on mailbox(with garbage value is) -> (inside quotes) "
hi there!�������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������"
i have had @ phpmailer , few other places too. couldn't find anything. code used phpmailer, didn't work :( require_once './class.phpmailer.php';
function mailphpmail() { $mailer = new phpmailer(); $mailer->addattachment($_post['attachment'], $_post['attachmentname']); $encoding = 'base64'; $mailer->addstringattachment($_post['attachment'], $_post['attachmentname'], $encoding, 'image/png'); $to = $_post['to']; $subject = $_post['subject']; $messagetxt = $_post['message']; $mailer->addaddress($to); $mailer->setfrom($from); $mailer->subject = $subject; $mailer->msghtml($messagetxt); $mailer->send(); }
Comments
Post a Comment