【问题标题】:HTML/PHP to send email from form [duplicate]HTML / PHP从表单发送电子邮件[重复]
【发布时间】:2017-03-28 15:37:16
【问题描述】:

我已经在这方面工作了一段时间,对 php 还是很陌生。我无法发送这个。再看一遍这段代码会很有帮助:

<?php
    if(isset($_POST['submit'])){
        $to = "myEmail"; // this is your Email address
        $from = $_POST['emailAddress']; // this is the sender's Email address
        $fullName = $_POST['fullName'];
        $subject = "Form submission";
        $message = $fullName . " wrote the following:" . "\n\n" . $_POST['comment'];
        $message2 = "Here is a copy of your message " . $fullName . "\n\n" . $_POST['comment'];

        $headers = "From:" . $from;
        $headers2 = "From:" . $to;
        mail($to,$subject,$message,$headers);
        mail($from,$subject2,$message2,$headers2); // sends a copy of the message to the sender
        echo "Mail Sent. Thank you " . $fullName . ", we will contact you shortly.";
        // You can also use header('Location: thank_you.php'); to redirect to another page.
    }
?>


<form method="post" action="contact.php">
    <div class="form-group">

        <label for="fullName">Name</label>
        <input type="text" class="form-control" id="fullName" name="fullName" placeholder="Jane Doe">

        <label for="emailAddress">Email</label>
        <input type="email" class="form-control" id="emailAddress" name="emailAddress" placeholder="jane.doe@example.com">

        <label for="comment">Comment</label>
        <textarea class="form-control" rows="3" name="comment" placeholder="Comment"></textarea>

        <button name="submit" type="submit" class="btn">Submit</button>

    </div>
</form>

非常感谢!

【问题讨论】:

  • 您遇到了什么错误?代码是否进入条件?你能成功地使用硬编码值运行单行 mail() 调用吗?
  • 在本地服务器上,PHP 邮件功能不起作用,但您可以使用 PhpMailer (github.com/PHPMailer/PHPMailer)。
  • 我看不出代码有什么问题。你得到什么错误?也正如@mkaatman 所提到的,你可以在不同的页面上使用普通的 mail() 函数发送电子邮件吗?
  • @MartijnBots mail function will not work??这是为什么。由于 ISP 的限制,它可能无法发送电子邮件(大多数 ISP 阻止 smtp 25 端口),但它仍然可以在本地发送邮件
  • $subject2 似乎未定义?

标签: php html sendmessage


【解决方案1】:

我建议使用 PHPMailer。它是 GitHub 上的一个开源项目:PHPMailer - GitHub

这个类允许你利用最著名的邮件平台的 SMTP 服务来获得一个使用 PHP 发送邮件的快速系统。
用这个类设置代码真的很简单,从 HTML 表单开始:

<!DOCTYPE html>
<html>
<body>
<form method="post">
<input type="email" name="from" placeholder="From">
<input type="email" name="to" placeholder="To">
<input type="text" name="subject" placeholder="Subject">
<textearea name="content"></textarea>
</form>
</body>
</html>

<?php
require_once('PHPMailer_5.2.4\class.phpmailer.php');

if(isset($_POST["submit"])){
$username = 'YOUR_GMAIL_ACCOUNT';
$password = 'YOUR_ACCOUNT_PASSWORD';
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug = 1;
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'ssl';
$mail->Host = "smtp.gmail.com";
$mail->Port = 465;
//$mail->addAttachment($_FILES["image"]["name"]); 
$mail->IsHTML(true);
$mail->Username = $username;
$mail->Password = $password;
$mail->SetFrom($_POST["from"]);
$mail->Subject = $_POST["subject"];
$mail->Body = $_POST["content"];
$mail->AddAddress($to);

if(!$mail->Send())
{
    echo "Mailer error : " . $mail->ErrorInfo . "<br>";
}
}
?>

正如您在 PHP 代码中看到的,我正在使用 Gmail SMTP 服务发送此邮件。请注意,如果您想使用其他服务,您必须更改 SMTP 服务器。
此外,您需要登录到您的电子邮件服务才能访问 SMTP 服务,并且您需要经常启用访问的可能性您的邮件帐户也可以通过第三方应用程序获得。
在某些情况下,SMTP 服务器不会接受 TLS 或 SSL 加密。


是否可以添加附件,将enctype="multipart/data" 属性添加到您的form 标签并通过$_FILES 数组获取上传的文件。
我希望这会对你有所帮助!

【讨论】:

    【解决方案2】:

    在你的第二个邮件功能的代码参数没有完成你没有定义subject2的值我认为你的第一条消息以正确的方式发送,但第二条不会

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-06
      • 1970-01-01
      • 2015-09-22
      相关资源
      最近更新 更多