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