【问题标题】:Phonegap send email (PHP and jQuery) : no variable POSTPhonegap 发送电子邮件(PHP 和 jQuery):无变量 POST
【发布时间】:2017-05-17 11:09:18
【问题描述】:

我在其他帖子或教程中搜索过,但不幸的是,没有什么对我有用...

我将 PhoneGap 用于带有表单的小应用程序。我的简化想法是:当访问者在输入中写下他的电子邮件并单击“发送”时,它会向我发送一封包含他的电子邮件的电子邮件。但是,目前,当我发送表单时,我的变量 $_POST['email'] 为空...

这是我的表格:

<form action="http://my-domain-name/app_mail.php"  method="POST">
    <div id="coord">
        <h3>Vos coordonnées</h3>
        <div class="group">
            <input id="email" type="email" placeholder="Email" required">
        </div>
        <div class="group">
            <input id="envoyer" value="Envoyer" type="submit">
        </div>                
    </div>
</form>

我使用这个 jQuery 代码:

$(document).bind( "mobileinit", function() {
    $.post({
        // My variables
        url: 'http://my-domain-name/app_mail.php',
        type: 'POST',
        data: {
            email: $('#email').val()
        }

    // Fonction HTML
    }, function(html) {
        var response = html;
        if(response == "success") {
            alert("Email envoyé avec succès !");
        } else {
            alert("Votre email n'a pas pu être envoyé...");
            return false;
        }
    });

$.mobile.allowCrossDomainPages = true;
});

我在互联网上找到了这段代码,我已经根据我的需要对其进行了调整......所以如果我没记错的话,这段代码允许这个函数跨域,使用变量 url,类型和数据,最后写一条消息PHP 是否有效。

最后,我的 PHP:

$email = $_POST['email'];
$header = "From: ".$email;
$message = $email;

// Validation
if($email == "") {
    echo "Erreur";
    echo "<br>Email :".$email;
} else {
    mail("my-email@my-domain.fr", "Message reçu depuis l'application", $message, $header);
    echo "Succès";
}

当我尝试发送表单时,我得到了这个答案:

“错误
电子邮件:"

在新页面上...如果我在我的 PHP 文件中添加 ini_set('display_errors', 1);,我在控制台或 PHP 中没有任何错误...

所以我认为我的变量没有被传输,但我不知道为什么......

【问题讨论】:

  • 如果你在 chrome 上,打开网络检查器,然后选择保留日志选项,然后单击 btn,然后首先检查 ajax 请求是否进行,然后检查它是否进行,单击请求并转到参数选项卡以查看电子邮件是否正在发送。
  • 感谢您的回答!它与 esskay 的解决方案一起使用,是的,我可以在发送表单时看到我的文件!感谢这个技巧:)

标签: php jquery cordova email


【解决方案1】:

将html表单中的语句更改为以下语法

<input id="email" name="email" type="email" placeholder="Email" required">

我已经添加了使用“S_POST”工作的名称标签。

您可以将以下内容编辑到您自己的页面:

 <?php
 $email=$_POST['mail'];
 $header = "From: ".$email;
 $message = $email;

include '__lib__/class.phpmailer.php';
$mail = new PHPMailer();
$mail->IsSMTP();                                     
$mail->SMTPSecure = 'ssl';

$mail->Host = "smtp.mail.yahoo.com";  
     $mail->Port = 465;

$mail->SMTPAuth = true;     // turn on SMTP authentication
//you must use an existing email address 
$mail->Username = 'email address-anything yahoo or google';  // SMTP username

$mail->Password = 'password for the email '; // SMTP password

$mail->From = 'xyz@yahoo.co.in';


$mail->FromName ='HelpQueires';

$mail->AddAddress($email);               



$mail->WordWrap = 50;                              

$mail->IsHTML(true);                              
$mail->Subject = 'Student Help Queries';

$mail->Body  = "hello";
$mail->SMTPDebug = 1;

if(!$mail->Send())
{
    echo "<h1>Mailer Error: " . $mail->ErrorInfo.'</h1>';

}
else
    echo "<h1>Mail Send Successfully</h1>";

?>

【讨论】:

  • 非常感谢您的回答!这是“工作”。我的意思是,我收到了成功消息,但我没有收到任何电子邮件……而且这个 PHP 代码可以在我的其他网站上运行……所以你知道我是否需要添加其他内容……?我有点失落……
  • 尝试使用 include 语句包含 phpmailer.php 类
  • 再次感谢您的回答。我在我的 PHP 文件中添加了require_once('phpmailer.php');,但它仍然不起作用(我有相同的结果)......(当然我也在我的 FTP 服务器上添加了文件)。
  • 你下载了phpmailer文件吗?
  • 当然!我已经下载了这个文件:link
【解决方案2】:

有点晚了,但这是我的最终代码。希望它会帮助别人!

所以,HTML(感谢 esskay,没有name 标签就无法工作):

<form action="http://your-domain/app_mail.php" method="POST">
    <div class="group">
        <input id="email" name="email" type="email" placeholder="Email" required>
        <span class="bar"></span>
    </div>
    <div class="group">
        <input id="envoyer" value="Send" type="submit">
    </div>
</form>

那么,这是我的 jQuery:

$(document).bind( "mobileinit", function() {
    $.ajax({
        // Variables
        url: 'http://your-domain/app_mail.php',
        type: 'POST',
        data: {
            email: $('#email').val()
        }
    // Fonction HTML
    }, function(html) {
        var response = html;
        // PHP done and email sent
        if(response == "success") {
            alert("Email sent !");
        } else {
            alert("Email can't been sent...");
            return false;
        }
    });
    $.mobile.allowCrossDomainPages = true;
});

所以在这里,您必须编写 PHP 文件的 URL(PHP 代码如下)、类型(POST 或 GET)和您的数据。 function(html) 不是必需的。最后,下面的函数允许 ajax 的跨域页面:

$(document).bind( "mobileinit", function() {
    // Ajax function
    $.mobile.allowCrossDomainPages = true;
});

PHP 代码:

require_once('phpmailer/PHPMailerAutoload.php');

// Variables
$email = $_POST['email'];
$to = "your@email.fr";
$sujet = "Your subject";
$message = "Message from".$email."\r\n\r\n";

$mail = new PHPMailer(); // create a new object
$mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // authentication enabled
$mail->SMTPSecure = 'tls'; // or 'ssl' depending of your configuration
$mail->Host = "smtp.gmail.com"; // host for gmail
$mail->Port = 465; // or 587
$mail->IsHTML(true);
$mail->CharSet = 'UTF-8';
$mail->Username = $to;
$mail->Password = "your-password";
$mail->SetFrom($email);
$mail->Subject = $sujet;
$mail->Body = $message;
$mail->AddAddress($to);

if (!$mail->send()) {
    echo "Mailer Error: " . $mail->ErrorInfo;
} else {
    echo "Sent !";
}

You have to download PHPMailer

注意:PHPMailer 中的所有文件都必须在同一个文件夹中。此外,这段代码对我有用,但如果你添加 $mail-&gt;IsSMTP(); // enable SMTP ,它就不再工作了......仍然不知道为什么......但无论如何,希望这段代码对你有用!再次感谢 esskay 的宝贵帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-02-08
    • 1970-01-01
    • 1970-01-01
    • 2015-02-12
    • 1970-01-01
    • 1970-01-01
    • 2021-03-22
    • 2010-11-22
    相关资源
    最近更新 更多