1

Windows Phone ベースのクライアント アプリケーション用の PHP サービスを開発しています。クライアント (WP7) を介してサービスを呼び出し、電子メールの一部として送信される添付ファイルも送信するようにします。したがって、HTTP POST を使用して、PHP 内で $to、$message、$subject、および $from を設定し、添付ファイルに同じロジックを使用しています。サービスを呼び出した後、メールボックスにもメールを受け取りますが、破損した添付ファイル。添付ファイルには通常、ガベージ値が含まれています。ここに私のPHPコードがあります

 $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 is a 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";

クライアント側では、次のコードを使用しています->

            var to = "xyz@gmail.com";
            var from = "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 the request.
                        using (var requestStream = request.EndGetRequestStream(result))
                        {
                            using (StreamWriter writer = new StreamWriter(requestStream))
                            {
                                writer.Write(postData);
                                writer.Flush();
                            }
                        }
                    }
                    catch
                        (Exception e) {  }

                    // Getting the 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)
            {

            }
        }

メールボックスの私の添付ファイル (ガベージ値は) -> (引用符内) "

こんにちは! ������������������������������������������������ ������������������������������������������������ ������������������������������������������������ ������������������������������������������������ "

私はPHPMailerと他のいくつかの場所も見てきました。しかし、何も見つかりませんでした。これは私が PHPMailer で使用したコードですが、動作しませんでした :( 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();
}
4

0 に答える 0