2012-11-27 109 views
0

我想知道他們提交一個表單到我的網站後,我將如何發送電子郵件給用戶..發送表單提交後的電子郵件

背景:我重新推出了一個網站,我的父親關在2002年下降,我們目前有一個歡迎/啓動頁面,說該網站即將回來。該頁面要求用戶輸入他們的電子郵件以添加到啓動列表中。這種形式使用「Web表單郵件」,如下:

<form action="/webformmailer.php" method="post"> 

他們的電子郵件被髮送到我們選擇的收件箱。我想要做的是發送此用戶的電子郵件,說謝謝,期待更新,再次感謝,等等。

我會怎麼去呢?我不知道,任何幫助將不勝感激!

回答

0

試試這個

SMTPconfig.php

<?php 
//Server Address 
$SmtpServer="127.0.0.1"; 
$SmtpPort="25"; //default 
$SmtpUser="username"; 
$SmtpPass="password"; 
?> 

SMTPclass.php

<?php 
class SMTPClient 
{ 

function SMTPClient ($SmtpServer, $SmtpPort, $SmtpUser, $SmtpPass, $from, $to, $subject, $body) 
{ 

$this->SmtpServer = $SmtpServer; 
$this->SmtpUser = base64_encode ($SmtpUser); 
$this->SmtpPass = base64_encode ($SmtpPass); 
$this->from = $from; 
$this->to = $to; 
$this->subject = $subject; 
$this->body = $body; 

if ($SmtpPort == "") 
{ 
$this->PortSMTP = 25; 
} 
else 
{ 
$this->PortSMTP = $SmtpPort; 
} 
} 

function SendMail() 
{ 
if ($SMTPIN = fsockopen ($this->SmtpServer, $this->PortSMTP)) 
{ 
fputs ($SMTPIN, "EHLO ".$HTTP_HOST."\r\n"); 
$talk["hello"] = fgets ($SMTPIN, 1024); 
fputs($SMTPIN, "auth login\r\n"); 
$talk["res"]=fgets($SMTPIN,1024); 
fputs($SMTPIN, $this->SmtpUser."\r\n"); 
$talk["user"]=fgets($SMTPIN,1024); 
fputs($SMTPIN, $this->SmtpPass."\r\n"); 
$talk["pass"]=fgets($SMTPIN,256); 
fputs ($SMTPIN, "MAIL FROM: <".$this->from.">\r\n"); 
$talk["From"] = fgets ($SMTPIN, 1024); 
fputs ($SMTPIN, "RCPT TO: <".$this->to.">\r\n"); 
$talk["To"] = fgets ($SMTPIN, 1024); 
fputs($SMTPIN, "DATA\r\n"); 
$talk["data"]=fgets($SMTPIN,1024); 
fputs($SMTPIN, "To: <".$this->to.">\r\nFrom: <".$this->from.">\r\nSubject:".$this->subject."\r\n\r\n\r\n".$this->body."\r\n.\r\n"); 
$talk["send"]=fgets($SMTPIN,256); 
//CLOSE CONNECTION AND EXIT ... 
fputs ($SMTPIN, "QUIT\r\n"); 
fclose($SMTPIN); 
// 
} 
return $talk; 
} 
} 
?> 

的index.php

<?php 
include('SMTPconfig.php'); 
include('SMTPClass.php'); 
if($_SERVER["REQUEST_METHOD"] == "POST") 
{ 
$to = $_POST['to']; 
$from = $_POST['from']; 
$subject = $_POST['sub']; 
$body = $_POST['message']; 
$SMTPMail = new SMTPClient ($SmtpServer, $SmtpPort, $SmtpUser, $SmtpPass, $from, $to, $subject, $body); 
$SMTPChat = $SMTPMail->SendMail(); 
} 
?> 
<form method="post" action=""> 
To:<input type="text" name="to" /> 
From :<input type='text' name="from" /> 
Subject :<input type='text' name="sub" /> 
Message :<textarea name="message"></textarea> 
<input type="submit" value=" Send " /> 
</form> 
+1

不要發佈鏈接的答案。 –

+0

好吧,我會記住這一點。謝謝 – fledgling

+0

@Nishant:現在..從帖子中提取重要內容並將其插入到您的答案中......這就是Jack暗示的內容...... – akira

0

您可以使用此代碼以PHP發送電子郵件。 :

<?php 

    require_once "Mail.php"; 

    $from = "<from.gmail.com>"; 
    $to = "<to.yahoo.com>"; 
    $subject = "Hi!"; 
    $body = "Hi,\n\nHow are you?"; 

    $host = "ssl://smtp.gmail.com"; 
    $port = "465"; 
    $username = "[email protected]"; //<> give errors 
    $password = "password"; 

    $headers = array ('From' => $from, 
     'To' => $to, 
     'Subject' => $subject); 
    $smtp = Mail::factory('smtp', 
     array ('host' => $host, 
     'port' => $port, 
     'auth' => true, 
     'username' => $username, 
     'password' => $password)); 

    $mail = $smtp->send($to, $headers, $body); 

    if (PEAR::isError($mail)) { 
     echo("<p>" . $mail->getMessage() . "</p>"); 
    } else { 
     echo("<p>Message successfully sent!</p>"); 
    } 

?> <!-- end of php tag--> 

如果你想使用PHP,那麼你可以找到在HERE.

+0

「Mail.php」來自? – Robbie

+0

我想你必須查看[here](http://stackoverflow.com/a/2748837/1740379) –

1

解決方案發送羣發電子郵件如果您想發送一個非常簡單的純文本電子郵件,那麼你可以使用PHP mail()功能它很快就能做到。 (這是假設您的Web主機已設置了sendmail)。否則,SMTP功能是一個選項(上面的答案)。 但要確保你驗證所有用戶提交的輸入,以確保您的郵件不能被垃圾郵件發送者使用 - 最重要的是,剔除線從電子郵件地址和用戶名等

飼料但是,什麼是更好的選項是使用預先編寫的庫,因爲這會消除很多問題/問題。 SwiftMailer目前可能是最好的之一,因爲它可以處理各種發送方法(SMTP,sendmail)和格式(文本,HTML等),並有助於防止使用中繼形式傳遞垃圾郵件。它也很好記錄(如果你不使用IE瀏覽器 - 文檔頁面在IE中不起作用!)

如果你想發送附件或HTML郵件,那麼它也可以通過mail(),但有一點爭取讓它在所有平臺上運行。再次,SwiftMailer(和其他庫)使這一點變得簡單。所以不妨從圖書館開始。

0

您可以使用php的mail()函數或使用類似PHPmailer的類(download from here)。後者給了我更好的結果。

<?php 

require_once('../class.phpmailer.php'); 

$mail    = new PHPMailer(); // defaults to using php "mail()" 

$body    = file_get_contents('contents.html'); 
$body    = preg_replace('/[\]/','',$body); 

$mail->SetFrom('[email protected]', 'First Last'); 

$mail->AddReplyTo("[email protected]","First Last"); 

$address = "[email protected]"; 
$mail->AddAddress($address, "John Doe"); 

$mail->Subject = "PHPMailer Test Subject via mail(), basic"; 

$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test 

$mail->MsgHTML($body); 

$mail->AddAttachment("images/phpmailer.gif");  // attachment 
$mail->AddAttachment("images/phpmailer_mini.gif"); // attachment 

if(!$mail->Send()) { 
    echo "Mailer Error: " . $mail->ErrorInfo; 
} else { 
    echo "Message sent!"; 
} 

?> 

這是非常容易使用,並要求最低限度的設置。您可以使用$_POST來設置接收器地址等。您可能需要先驗證電子郵件地址。使用javascript的PHP validation

相關問題