邮件发送文件在PHP中

时间:2013-02-27 11:29:08

标签: php email

我想在php中创建邮件表单。我为ny form写了html代码。我在同一个文件中写了html表单和php代码。所以我不想用另一个文件发送电子邮件。

    <div class="agent-contact">
      <table width="400" border="0" align="center" cellpadding="3" cellspacing="1">
      <tr>
      <td><strong>Contact Form </strong></td>
      </tr>
      </table>

      <table width="400" border="0" align="center" cellpadding="0" cellspacing="1">
        <tr>
          <td>
            <form name="form1" method="post" action="">
              <table width="100%" border="0" cellspacing="1" cellpadding="3">
                <tr>
                  <td width="16%">Subject</td>
                  <td width="2%">:</td>
                  <td width="82%"><input name="subject" type="text" id="subject" size="50"></td>
                </tr>
                <tr>
                  <td>Detail</td>
                  <td>:</td>
                  <td><textarea name="detail" cols="50" rows="4" id="detail"></textarea></td>
                </tr>
                <tr>
                  <td>Name</td>
                  <td>:</td>
                  <td><input name="name" type="text" id="name" size="50"></td>
                </tr>
                <tr>
                  <td>Email</td>
                  <td>:</td>
                  <td><input name="customer_mail" type="text" id="customer_mail" size="50"></td>
                </tr>
                <tr>
                  <td>&nbsp;</td>
                  <td>&nbsp;</td>
                  <td><input type="submit" name="Submit" value="Send Message"></td>
                </tr>
              </table>
            </form>
          </td>
        </tr>
      </table>
  </div>

我发送邮件的PHP代码是

<?php

$subject ="$subject";

$message="$detail";

$mail_from="$customer_mail";

$header="from: $name <$mail_from>";


$to ='someone@somewhere.com';

$send_contact=mail($to,$subject,$message,$header);


if($send_contact){
echo "We've recived your contact information";
}
else {
echo "ERROffR";
}

&GT;

如何在提交发送消息按钮时发送邮件?我有相同的文件。

1 个答案:

答案 0 :(得分:0)

使用isset

if($_POST['submit']) {
  $subject =$_POST['subject'];

  $message=$_POST['detail'];

  $mail_from=$_POST['customer_mail'];

  $header="from: " . $_POST['name'] ." <$mail_from>";


  $to ='someone@somewhere.com';

  $send_contact=mail($to,$subject,$message,$header);


  if($send_contact){
    echo "We've recived your contact information";
  }
  else {
     echo "ERROffR";
  }
 }