2015-12-23 40 views
-1

在我的網站底部有一個「發送消息」按鈕。我希望它將信息和聯繫信息(電子郵件,姓名)發送到我的電子郵件地址。我怎麼可能做到這一點?順便說一句,我是這個網站的新東西。我不確定如何通過電子郵件發送表單

+0

重複的問題。 – Nikhil

回答

0

您可以使用PHP郵件功能。

<?php 
 

 
if (!empty($_POST)) { 
 
\t $name= $_POST['name']; 
 
\t $mail_id= $_POST['email_id']; 
 
\t $email= '';//email address on which you want to receive website details 
 
\t $message_field = $_POST['message']; 
 
\t 
 
\t \t $header = "MIME-Version: 1.0" . "\r\n"; 
 
\t \t $header .= "Return-Path: \r\n"; 
 
\t \t $header .= "Content-type:text/html;charset=UTF-8" . "\r\n"; 
 
\t \t $header= "From: TriaaHousing"; 
 
     $message="Name:".$name."\r\n"; 
 
\t \t $message .=" "; 
 
\t \t $message .= "EmailId:".$mail_id."\r\n"; 
 
\t \t $message .=" "; 
 
\t \t $message .= "Message:".$message_field; 
 
\t \t 
 
     
 
\t \t if(mail($email, "Subject", $message, $header)){ 
 
\t \t \t \t echo 1; 
 
\t \t }else{ 
 
\t \t \t echo 0; 
 
\t \t } 
 
      
 
       
 
} 
 
?>

0

使用可以使用默認PHP's mail()功能,或者您可以使用PHPMailer(郵件發送助手)。兩者都是安全和正確的。但如果你需要一些其他的東西,然後使用PHPMailer。

1.使用PHP的mail()函數是可能的。記住郵件功能在本地服務器中不起作用。

<?php 
$to  = '[email protected]'; 
$subject = 'the subject'; 
$message = 'hello'; 
$headers = 'From: [email protected]' . "\r\n" . 
    'Reply-To: [email protected]' . "\r\n" . 
    'X-Mailer: PHP/' . phpversion(); 

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

注:你需要,如果你正在使用SMTP在本地服務器上,以配置SMTP。看看這個類似的post

2.您也可以使用PHPMailer類https://github.com/PHPMailer/PHPMailer

它允許您使用郵件功能或透明地使用smtp服務器。它還處理基於HTML的電子郵件和附件,因此您不必編寫自己的實現。

下面是從網頁上面的例子:

<?php 
require 'PHPMailerAutoload.php'; 

$mail = new PHPMailer; 

$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 = '[email protected]';     // SMTP username 
$mail->Password = 'secret';       // SMTP password 
$mail->SMTPSecure = 'tls';       // Enable encryption, 'ssl' also accepted 

$mail->From = '[email protected]'; 
$mail->FromName = 'Mailer'; 
$mail->addAddress('[email protected]', 'Webmaster User');  // Add a recipient 
$mail->addAddress('[email protected]');    // Name is optional example 
$mail->addReplyTo('[email protected]', 'Information'); 
$mail->addCC('[email protected]'); 
$mail->addBCC('[email protected]'); 

$mail->WordWrap = 50;         // Set word wrap to 50 characters 
$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'; 
} 

使用addReplyToaddCCaddBCC如果需要。

希望這對你有幫助!