【问题标题】:How to call php file to send mail ?? , Android如何调用php文件发送邮件? , 安卓
【发布时间】:2025-12-23 22:30:07
【问题描述】:

我有以下 mail.php 文件:

<?php
$name = $_POST['name'];
$to = $_POST['to'];
$from = $_POST['from'];
$subject = $_POST['subject'];
$message = "From: ".$name."\r\n";
$message .= $_POST['message'];
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
?> 

我有以下布局从我的应用程序发送邮件:

我想在点击发送按钮后将邮件发送到静态邮件,例如:mymail@mail.com。
我如何通过调用上面的 php 文件来发送邮件。
我听说过 post 方法来调用上面的 php 文件 但对此一无所知。
请帮忙!

【问题讨论】:

标签: android email post


【解决方案1】:

这里是:

    public static void sendData(String name, String to, String from, String subject, String message)
    {
        String content = "";

        try
        {               
            /* Sends data through a HTTP POST request */
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost("http://your.website.com");
            List <NameValuePair> params = new ArrayList <NameValuePair>();
            params.add(new BasicNameValuePair("name", name));
            params.add(new BasicNameValuePair("to", to));
            params.add(new BasicNameValuePair("from", from));
            params.add(new BasicNameValuePair("subject", subject));
            params.add(new BasicNameValuePair("message", message));
            httpPost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));

            /* Reads the server response */
            HttpResponse response = httpClient.execute(httpPost);
            InputStream in = response.getEntity().getContent();

            StringBuffer sb = new StringBuffer();
            int chr;
            while ((chr = in.read()) != -1)
            {
                sb.append((char) chr);
            }
            content = sb.toString();
            in.close();

            /* If there is a response, display it */
            if (!content.equals(""))
            {
                Log.i("HTTP Response", content);
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

【讨论】:

  • str = str.replaceAll("(\r\n|\n\r|\r|\n)", "
    ");
【解决方案2】:

您必须加载 Web 视图或编写 Web 服务并将参数作为 JSON 传递。

【讨论】:

  • 不要忘记将 添加到清单中