【问题标题】:send e-mail via dialog box通过对话框发送电子邮件
【发布时间】:2014-01-26 16:24:41
【问题描述】:

我正在使用对话框发送电子邮件。当用户单击“分配”按钮时,将打开一个对话框。这是我的 HTML 输出:http://jsfiddle.net/DBWc2/1/

当用户单击对话框的“发送电子邮件”按钮时,我如何合并以下 PHP 代码来发送电子邮件。

这是我从 W3schools.com 获得的 PHP 代码

<?php
 $to = "someone@example.com";
 $subject = "Test mail";
 $message = "Hello! This is a simple email message.";
 $from = "someonelse@example.com";
 $headers = "From:" . $from;
 mail($to,$subject,$message,$headers);
 echo "Mail Sent.";
?>

【问题讨论】:

  • 您需要为此使用 AJAX。另外,请注意直接在其中包含$from——有人可能会注入恶意标头甚至插入恶意附件。请在CRLF Injection 上查看此页面。

标签: javascript php jquery email


【解决方案1】:

只需将action 添加到您的form

<form action="sendEmail.php" id="dialog" method="post" novalidate="novalidate"> 

然后将你的php代码放入sendEmail.php:

<?php
 $to = "someone@example.com";
 $subject = "Test mail";
 $message = "Hello! This is a simple email message.";
 $from = "someonelse@example.com";
 $headers = "From:" . $from;
 mail($to,$subject,$message,$headers);
 echo "Mail Sent.";
?>

在这种情况下,sendEmail.php 是与您的 HTML 文件相同的目录。根据您当前的项目结构,它可以是不同的位置。

如果您不想刷新页面或重定向到另一个页面来发送电子邮件。你可以看看Ajax

【讨论】:

  • 无法连接到“localhost”的邮件服务器端口 25,验证您的“SMTP”;和“smtp_port”在 php.ini 中设置或在 C:\wamp\www\10CE101\rfp_final\send_email.php 中使用 ini_set()。出现此错误。
  • @user3212202 不要使用普通的mail 函数。使用诸如SwiftMailer 之类的库来执行此操作。您可以通过这个问题了解如何使用swiftmailer 发送 gmail:stackoverflow.com/questions/3478906/…
猜你喜欢
  • 2013-12-24
  • 1970-01-01
  • 2012-02-15
  • 1970-01-01
  • 1970-01-01
  • 2012-01-07
  • 2012-04-27
  • 2016-07-04
相关资源
最近更新 更多