PHP 不从 html 发送电子邮件


PHP not sending email from html

我正在尝试制作一个表单,其中用户引入数据并将其提交到电子邮件中。

但是,它会让我进入空白页(文件.php页),并且电子邮件未发送。

网页代码:

<form action="./sendmail.php" method="post">
    <div class="log">
        <input type="text" id="name" value="name" name="studname" class="form">
        <input type="email" id="mail" value="mail@gmail.com" name="studmail" class="form">
        <input type="tel" id="age" value="age" class="form" name="studage">
        <input type="submit" value="submit!" id="submit">
    </div>
</form>

现在这是我的"sendmail.php"代码:

<?php
    /*  STUDENT PARAMETERS  */
    $studentName = $_POST['studname'];
    $studentMail = $_POST['studmail'];
    $studentAge = $_POST['studage'];
    mail('code@gmail.com', 'Message',
    'NAME: ' . $studentName . ''nMAIL: ' . $studentMail . ''nAge: ' . $studentAge);
?>

我该如何解决这个问题,为什么这不起作用?谢谢你的时间。

我认为你可以使用phpmailer类

@Telmo Vaz试试这个

<?php
    $studentName = $_POST['studname'];
        $studentMail = $_POST['studmail'];
        $studentAge = $_POST['studage'];
    $recipient='code@gmail.com'; 
    $subject="Message"; 
    $content = "New Message Form Contact us'n From: ".$studentName .", 'n Email: ".$studentMail .",'n Age: ".$studentAge;
        $headers = 'From: code@gmail.com' . "'r'n" .
        'Reply-To: code@gmail.com' . "'r'n" .
        'X-Mailer: PHP/' . phpversion();
        $retval=mail($recipient, $subject, $content, $headers);
        if( $retval == true ){do something;}
        else{echo "Error Sending Message";}
?>

您需要一个电子邮件地址才能发送电子邮件。电子邮件不会从此地址发送,但所选地址将显示在"发件人"部分中。试试这个:

/*  STUDENT PARAMETERS  */
$studentName = $_POST['studname'];
$studentMail = $_POST['studmail'];
$studentAge = $_POST['studage'];
$to = "code@gmail.com";
$subject = "Message";
$message = "NAME: " . $studentName . "'nMAIL: " . $studentMail . "'nAge: " . $studentAge;
$from = $to;
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
echo "Mail Sent.";