HTML/PHP从表单发送电子邮件

HTML/PHP从表单发送电子邮件,php,html,sendmessage,Php,Html,Sendmessage,我已经在这方面工作了一段时间,并且对php非常陌生。我很难把这个寄出去。对该代码进行第二次观察将非常有帮助: <?php if(isset($_POST['submit'])){ $to = "myEmail"; // this is your Email address $from = $_POST['emailAddress']; // this is the sender's Email address $fullName =

我已经在这方面工作了一段时间,并且对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.
    }
?>

在您的代码中,第二封邮件功能的参数未完成。您未定义主题2的值。我认为您的第一封邮件发送方式正确,但第二封邮件不会发送。我建议使用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”
属性添加到您的
表单
标记,并通过
$\u文件
数组获取上传的文件。

我希望这对你有帮助

你犯了什么错误?代码是否进入条件内部?您能否成功地使用硬编码的值运行一行mail()调用?在本地服务器上,PHP mail函数将无法工作,但您可以使用PhpMailer()。我看不到代码中有任何错误。你犯了什么错误?正如@mkaatman所提到的,您是否可以在另一个页面上使用plain mail()函数发送电子邮件?@MartijnBots
邮件函数将不起作用
??这是为什么。由于ISP限制(大多数ISP阻止smtp 25端口),它可能不会发送电子邮件,但它仍然可以在本地发送邮件
$subject2
似乎未定义?
<!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>";
}
}
?>