使用HTTP POST附件的PHP邮件

时间:2013-05-04 14:24:32

标签: c# php windows-phone-7 post httpwebrequest

我正在为基于Windows Phone的客户端应用程序开发PHP服务。我将通过我的客户端(WP7)调用该服务,我希望该服务还发送作为电子邮件的一部分发送的任何附件。我因此使用HTTP POST,在我的PHP中设置$ to,$ message,$ subject和$,并为我的附件使用相同的逻辑,在调用服务后我也在我的邮箱上收到电子邮件,但是腐败的依恋。附件通常包含垃圾值。 这是我的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)
            {

            }
        }

我在MailBox上的附件(垃圾值为) - > (内部报价) “

你好! “

我已经看过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();
}

0 个答案:

没有答案