我需要有人指导我如何在php中正确使用Mail()函数。
我想制作一个简单的表单,用户可以将其姓名和电子邮件发送给我,以便我可以向他们发送邀请。
我想在php中使用mail()函数对其进行编码。
注意:我不打算创建一个联系表单,而只是一个只有两个字段的表单;只是姓名和电子邮件。
希望有人可以提供帮助。
答案 0 :(得分:0)
以下是根据您的要求提供的示例代码
<?php
$action=$_REQUEST['action'];
if ($action=="") /* display the contact form */
{
?>
<form action="" method="POST" enctype="multipart/form-data">
<input type="hidden" name="action" value="submit">
Your name:<br>
<input name="name" type="text" value="" size="30"/><br>
Your email:<br>
<input name="email" type="text" value="" size="30"/><br>
Your message:<br>
<textarea name="message" rows="7" cols="30"></textarea><br>
<input type="submit" value="Send email"/>
</form>
<?php
}
else /* send the submitted data */
{
$name=$_REQUEST['name'];
$email=$_REQUEST['email'];
$message=$_REQUEST['message'];
if (($name=="")||($email=="")||($message==""))
{
echo "All fields are required, please fill <a href=\"\">the form</a> again.";
}
else{
$from="From: $name<$email>\r\nReturn-path: $email";
$subject="Message sent using your contact form";
mail("youremail@yoursite.com", $subject, $message, $from);
echo "Email sent!";
}
}
?>
希望这有帮助
答案 1 :(得分:0)
你可以尝试这种方式。
$to_mail="ex@domain.com";
$subject="subject";
$message = "you can put in it your Form Data";// like name and Email that you want to send
// a random hash will be necessary to send mixed content
$separator = md5(time());
// carriage return type (we use a PHP end of line constant)
$eol = PHP_EOL;
// main header (multipart mandatory)
$headers = "MIME-Version: 1.0" . $eol;
$headers .= "Content-Type: multipart/mixed; boundary=\"" . $separator . "\"" . $eol . $eol;
$headers .= "Content-Transfer-Encoding: 7bit" . $eol;
$headers .= "This is a MIME encoded message." . $eol . $eol;
$headers .= "--" . $separator . $eol;
$headers .= "Content-Type: text/html; charset=\"iso-8859-1\"" . $eol;
$headers .= $message . $eol . $eol;
@mail("$to_mail","$subject","",$headers);