Php 发送电子邮件表单
我创建了一个HTML表单和PHP脚本的某些部分。看看下面 不知何故,我需要使用邮件功能发送表单。我该怎么做 HTML: PHP:Php 发送电子邮件表单,php,html,Php,Html,我创建了一个HTML表单和PHP脚本的某些部分。看看下面 不知何故,我需要使用邮件功能发送表单。我该怎么做 HTML: PHP: 你可以像这样发送邮件。使用PHP邮件功能 <?php if(isset($_POST['submit'])){ $name = $_POST['name']; $email = $_POST['email']; $message = $_POST['message']; $from = 'Fra:'$email; $to = 'MyEmail'; $subj
你可以像这样发送邮件。使用PHP邮件功能
<?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");
}
?>
试试这个:
<?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);
您最好使用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 send email之类的东西会发现很多例子。你不会不经意间知道,当消息被发送时,如何在屏幕顶部显示一个固定元素?来点绿酒吧什么的?现在,当我点击send时,它只会给我一个空白页面,脚本本身的页面或其他什么。消息发送正确,我已经做了一些修改tests@MagnusPilegaardifmail$to、$subject、$body、From:$From{echo'Mail Sent';}ese{echo'failed';};重定向呢?它也能解决这个问题吗?因为它显示白色页面。。。
<?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);
<?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';
}
if(mail('toemail', 'subject', 'message', 'headers')) {
echo 'Mail has sent';
} else {
echo 'Mail not sent';
}