发送电子邮件表单

时间:2015-08-16 20:37:04

标签: php html

我创建了一个HTML表单,以及PHP脚本的某些部分。看下面。

我需要以某种方式使用mail函数发送表单。我该怎么做?

HTML:

<form id="kontaktForm" action="Scripts-ContactForm/recieving.php" method="POST">
    <div id="kontaktFormVenstre">
        <div id="inputFields_container">
            <div id="inputField_container">
                <input class="inputField" type="text" name="name" placeholder="Dit navn" />
            </div>
            <div id="inputField_container">
                <input class="inputField" type="text" name="email" placeholder="Din E-Mail" />
            </div>
        </div>
        <div id="inputFieldMessage_container">
            <textarea class="inputFieldMessage" name="message" placeholder="Din besked her" rows="8" cols="20"></textarea>
        </div>
    </div>
    <div id="kontaktFormHøjre">
        <div id="button">
            <input type="submit" name="submit" value="Send mail" />
        </div>
    </div>
</form>

PHP:

<?php
    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];
    $from = 'Fra:'$email; 
    $to = 'MyEmail'; 
    $subject = 'Hello';

    $body = "From: $name\n E-Mail: $email\n Message:\n $message";
?>

3 个答案:

答案 0 :(得分:0)

您可以像这样发送邮件。使用PHP mail()函数。

<?php
if(isset($_POST['submit'])){
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'Fra:'$email; 
$to = 'MyEmail'; 
$subject = 'Hello';

$body = "From: $name\n E-Mail: $email\n Message:\n $message";

mail($to,$subject,$body,"From:$from");

}

?>

答案 1 :(得分:0)

http://php.net/manual/en/function.mail.php

试试这个:

<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$to = 'MyEmail'; 
$subject = 'Hello';
$headers = 'From: '.$name.' <' . $email . ">\r\n" .
'Reply-To: '.$name.' <' . $email . ">\r\n";

$body = "From: $name\n E-Mail: $email\n Message:\n $message";

mail($to, $subject, $body, $headers);

答案 2 :(得分:0)

您最好使用PHPMailer之类的东西,可以从以下网址下载:https://github.com/PHPMailer/PHPMailer

有许多示例可以帮助您入门,但使用此示例可以生成HTML电子邮件和HTML替代方案(对于无法读取HTML的客户端)

来自github的例子:

<?php
require 'PHPMailerAutoload.php';

$mail = new PHPMailer;

//$mail->SMTPDebug = 3;                               // Enable verbose     debug output

$mail->isSMTP();                                      // Set mailer to use SMTP
$mail->Host = 'smtp1.example.com;smtp2.example.com';  // Specify main and backup SMTP servers
$mail->SMTPAuth = true;                               // Enable SMTP authentication
$mail->Username = 'user@example.com';                 // SMTP username
$mail->Password = 'secret';                           // SMTP password
$mail->SMTPSecure = 'tls';                            // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587;                                    // TCP port to connect to

$mail->From = 'from@example.com';
$mail->FromName = 'Mailer';
$mail->addAddress('joe@example.net', 'Joe User');     // Add a recipient
$mail->addAddress('ellen@example.com');               // Name is optional
$mail->addReplyTo('info@example.com', 'Information');
$mail->addCC('cc@example.com');
$mail->addBCC('bcc@example.com');

$mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name
$mail->isHTML(true);                                  // Set email format to HTML

$mail->Subject = 'Here is the subject';
$mail->Body    = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

if(!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent';
}

作为替代方案,你可以使用PHP的mail()函数,如上所述,它就像使用一样简单:

if(mail('toemail', 'subject', 'message', 'headers')) {
    echo 'Mail has sent';
} else {
    echo 'Mail not sent';
}