Php 使用SMTP服务器将我的表单数据HTML发送到电子邮件

Php 使用SMTP服务器将我的表单数据HTML发送到电子邮件,php,html,forms,email,phpmailer,Php,Html,Forms,Email,Phpmailer,我正试图让我的for数据通过我的php代码,并通过电子邮件从我的gmail帐户发送到另一个电子邮件地址。但这样做有困难 以下是我的HTML代码: <form name="contactform" method="post" action="sendMail.php"> <table width="400px"> <tr> <td valign="top"> <label for="first_name">First Nam

我正试图让我的for数据通过我的php代码,并通过电子邮件从我的gmail帐户发送到另一个电子邮件地址。但这样做有困难

以下是我的HTML代码:

<form name="contactform" method="post" action="sendMail.php">

<table width="400px">

<tr>

 <td valign="top">

  <label for="first_name">First Name *</label>

 </td>

 <td valign="top">

  <input  type="text" name="first_name" maxlength="50" size="30">

 </td>

</tr>

<tr>

 <td valign="top">

  <label for="last_name">Last Name </label>

 </td>

 <td valign="top">

  <input  type="text" name="last_name" maxlength="50" size="30">

 </td>

</tr>

<tr>

 <td valign="top">

  <label for="email">Email *</label>

 </td>

 <td valign="top">

  <input  type="text" name="email" maxlength="80" size="30">

 </td>

</tr>

<tr>

 <td valign="top">

  <label for="telephone">Phone</label>

 </td>

 <td valign="top">

  <input  type="text" name="telephone" maxlength="30" size="30">

 </td>

</tr>

<tr>

 <td valign="top">

  <label for="comments">Comments *</label>

 </td>

 <td valign="top">

  <textarea  name="comments" maxlength="1000" cols="25" rows="6"></textarea>

 </td>

</tr>

<tr>

 <td colspan="2" style="text-align:center">

  <input type="submit" value="SUBMIT">

 </td>

</tr>

</table>

</form>

名字*
姓
电子邮件*
电话
评论*
PHP代码sendMail.PHP

<?php 

require 'PHPMailer-master/PHPMailerAutoload.php';

$mail = new PHPMailer;

$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'myname@gmail.com';
$mail->Password = 'H********';
$mail->SMTPSecure = 'tls';
$mail->Port = 587; 
$mail->SMTPDebug = 1;

$mail->From = 'myname@gmail.com';
$mail->FromName = 'TEST';
$mail->addAddress('myname@domain.com');

$errors = '';

if(empty($_POST['name'])  ||
   empty($_POST['email']) ||
   empty($_POST['phone']) ||
   empty($_POST['message']))
{
    $errors .= "\n Error: all fields are required";
}
$name = $_POST['name'];
$email_address = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
if (!preg_match(
"/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i",
$email_address))
{
    $errors .= "\n Error: Invalid email address";
}

if( empty($errors))
{
$to = $myemail;



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

$email_subject = "Request Additional Information: $name";
$email_body = "You have received a new request for additional information. ".
" Here are the details:\n Name: $name \n ".
"Email: $email_address\n  Phone: $phone\n  Message: \n $message";
$headers = "From: $myemail\n";
$headers .= "Reply-To: $email_address";
mail($to,$email_subject,$email_body,$headers);
//redirect to the 'thank you' page
header('Location: contact-form-thank-you.html');
}


?>
    <?php
$first_name="First Name : ".$_POST['first_name']."<br>";
$last_name="Last Name :". $_POST['last_name']. "<br>";
$your_email="Your Email :".$_POST['email']. "<br>";
$telephone="Your Telephone :".$_POST['telephone']. "<br>";
$your_message="Your Comments :".$_POST['comments']. "<br>";
$message = "
 \n $first_name \n 
 \n $last_name \n  
 \n $your_email \n 
 \n $telephone \n 
 \n $your_message \n
";
echo $message;
include "PHPMailer_5.2.4/class.phpmailer.php"; 


$mail = new PHPMailer;
 $mail->isSMTP();                                      
 $mail->Host = 'smtp.gmail.com';

 $mail->SMTPAuth = true;          
 $mail->Username = '***@gmail.com';
 $mail->Password = 'gmailpassword';           
 $mail->SMTPSecure = 'tls';              
 $mail->Port = 587;                      
 $mail->setFrom('someaddress@example.com', 'Mailer');
 $mail->addAddress($your_email, 'Name');
 $mail->addAttachment('fileaddress');       
 $mail->isHTML(true);                              
 $mail->Subject = 'Here is the subject';
 $mail->Body    = $message;
 $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';
 }
?>

有人能帮我弄清楚为什么当我发送它时,它来自用户从我的表单输入的电子邮件,而不是我的Gmail电子邮件地址。

这里是你的Html代码

<form name="contactform" method="post" action="sendMail.php">

<table width="400px">

<tr>

 <td valign="top">

  <label for="first_name">First Name *</label>

 </td>

 <td valign="top">

  <input  type="text" name="first_name" maxlength="50" size="30">

 </td>

</tr>

<tr>

 <td valign="top">

  <label for="last_name">Last Name </label>

 </td>

 <td valign="top">

  <input  type="text" name="last_name" maxlength="50" size="30">

 </td>

</tr>

<tr>

 <td valign="top">

  <label for="email">Email *</label>

 </td>

 <td valign="top">

  <input  type="email" name="email" maxlength="80" size="30">

 </td>

</tr>

<tr>

 <td valign="top">

  <label for="telephone">Phone</label>

 </td>

 <td valign="top">

  <input  type="text" name="telephone" maxlength="30" size="30">

 </td>

</tr>

<tr>

 <td valign="top">

  <label for="comments">Comments *</label>

 </td>

 <td valign="top">

  <textarea  name="comments" maxlength="1000" cols="25" rows="6"></textarea>

 </td>

</tr>

<tr>

 <td colspan="2" style="text-align:center">

  <input type="submit" value="SUBMIT">

 </td>

</tr>

</table>

</form>

名字*
姓
电子邮件*
电话
评论*
这是您的sendMail.php

<?php 

require 'PHPMailer-master/PHPMailerAutoload.php';

$mail = new PHPMailer;

$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'myname@gmail.com';
$mail->Password = 'H********';
$mail->SMTPSecure = 'tls';
$mail->Port = 587; 
$mail->SMTPDebug = 1;

$mail->From = 'myname@gmail.com';
$mail->FromName = 'TEST';
$mail->addAddress('myname@domain.com');

$errors = '';

if(empty($_POST['name'])  ||
   empty($_POST['email']) ||
   empty($_POST['phone']) ||
   empty($_POST['message']))
{
    $errors .= "\n Error: all fields are required";
}
$name = $_POST['name'];
$email_address = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
if (!preg_match(
"/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i",
$email_address))
{
    $errors .= "\n Error: Invalid email address";
}

if( empty($errors))
{
$to = $myemail;



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

$email_subject = "Request Additional Information: $name";
$email_body = "You have received a new request for additional information. ".
" Here are the details:\n Name: $name \n ".
"Email: $email_address\n  Phone: $phone\n  Message: \n $message";
$headers = "From: $myemail\n";
$headers .= "Reply-To: $email_address";
mail($to,$email_subject,$email_body,$headers);
//redirect to the 'thank you' page
header('Location: contact-form-thank-you.html');
}


?>
    <?php
$first_name="First Name : ".$_POST['first_name']."<br>";
$last_name="Last Name :". $_POST['last_name']. "<br>";
$your_email="Your Email :".$_POST['email']. "<br>";
$telephone="Your Telephone :".$_POST['telephone']. "<br>";
$your_message="Your Comments :".$_POST['comments']. "<br>";
$message = "
 \n $first_name \n 
 \n $last_name \n  
 \n $your_email \n 
 \n $telephone \n 
 \n $your_message \n
";
echo $message;
include "PHPMailer_5.2.4/class.phpmailer.php"; 


$mail = new PHPMailer;
 $mail->isSMTP();                                      
 $mail->Host = 'smtp.gmail.com';

 $mail->SMTPAuth = true;          
 $mail->Username = '***@gmail.com';
 $mail->Password = 'gmailpassword';           
 $mail->SMTPSecure = 'tls';              
 $mail->Port = 587;                      
 $mail->setFrom('someaddress@example.com', 'Mailer');
 $mail->addAddress($your_email, 'Name');
 $mail->addAttachment('fileaddress');       
 $mail->isHTML(true);                              
 $mail->Subject = 'Here is the subject';
 $mail->Body    = $message;
 $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';
 }
?>

试试这个,效果很好

这里是您的Html代码

<form name="contactform" method="post" action="sendMail.php">

<table width="400px">

<tr>

 <td valign="top">

  <label for="first_name">First Name *</label>

 </td>

 <td valign="top">

  <input  type="text" name="first_name" maxlength="50" size="30">

 </td>

</tr>

<tr>

 <td valign="top">

  <label for="last_name">Last Name </label>

 </td>

 <td valign="top">

  <input  type="text" name="last_name" maxlength="50" size="30">

 </td>

</tr>

<tr>

 <td valign="top">

  <label for="email">Email *</label>

 </td>

 <td valign="top">

  <input  type="email" name="email" maxlength="80" size="30">

 </td>

</tr>

<tr>

 <td valign="top">

  <label for="telephone">Phone</label>

 </td>

 <td valign="top">

  <input  type="text" name="telephone" maxlength="30" size="30">

 </td>

</tr>

<tr>

 <td valign="top">

  <label for="comments">Comments *</label>

 </td>

 <td valign="top">

  <textarea  name="comments" maxlength="1000" cols="25" rows="6"></textarea>

 </td>

</tr>

<tr>

 <td colspan="2" style="text-align:center">

  <input type="submit" value="SUBMIT">

 </td>

</tr>

</table>

</form>

名字*
姓
电子邮件*
电话
评论*
这是您的sendMail.php

<?php 

require 'PHPMailer-master/PHPMailerAutoload.php';

$mail = new PHPMailer;

$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'myname@gmail.com';
$mail->Password = 'H********';
$mail->SMTPSecure = 'tls';
$mail->Port = 587; 
$mail->SMTPDebug = 1;

$mail->From = 'myname@gmail.com';
$mail->FromName = 'TEST';
$mail->addAddress('myname@domain.com');

$errors = '';

if(empty($_POST['name'])  ||
   empty($_POST['email']) ||
   empty($_POST['phone']) ||
   empty($_POST['message']))
{
    $errors .= "\n Error: all fields are required";
}
$name = $_POST['name'];
$email_address = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
if (!preg_match(
"/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i",
$email_address))
{
    $errors .= "\n Error: Invalid email address";
}

if( empty($errors))
{
$to = $myemail;



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

$email_subject = "Request Additional Information: $name";
$email_body = "You have received a new request for additional information. ".
" Here are the details:\n Name: $name \n ".
"Email: $email_address\n  Phone: $phone\n  Message: \n $message";
$headers = "From: $myemail\n";
$headers .= "Reply-To: $email_address";
mail($to,$email_subject,$email_body,$headers);
//redirect to the 'thank you' page
header('Location: contact-form-thank-you.html');
}


?>
    <?php
$first_name="First Name : ".$_POST['first_name']."<br>";
$last_name="Last Name :". $_POST['last_name']. "<br>";
$your_email="Your Email :".$_POST['email']. "<br>";
$telephone="Your Telephone :".$_POST['telephone']. "<br>";
$your_message="Your Comments :".$_POST['comments']. "<br>";
$message = "
 \n $first_name \n 
 \n $last_name \n  
 \n $your_email \n 
 \n $telephone \n 
 \n $your_message \n
";
echo $message;
include "PHPMailer_5.2.4/class.phpmailer.php"; 


$mail = new PHPMailer;
 $mail->isSMTP();                                      
 $mail->Host = 'smtp.gmail.com';

 $mail->SMTPAuth = true;          
 $mail->Username = '***@gmail.com';
 $mail->Password = 'gmailpassword';           
 $mail->SMTPSecure = 'tls';              
 $mail->Port = 587;                      
 $mail->setFrom('someaddress@example.com', 'Mailer');
 $mail->addAddress($your_email, 'Name');
 $mail->addAttachment('fileaddress');       
 $mail->isHTML(true);                              
 $mail->Subject = 'Here is the subject';
 $mail->Body    = $message;
 $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';
 }
?>


试试这个,效果很好

您有两个代码块,第一个用于
phpMailer
第二个用于使用PHPs
mail
功能,第一个
phpMailer
从不发送任何内容,第二个
mail
发送任何内容。按照这里的示例,您有两个代码块:第一个用于
phpMailer
第二个用于使用PHPs
mail
功能,第一个
phpMailer
从不发送任何东西,第二个
mail
发送任何东西。请按照这里的示例进行操作