Học lập trình web PHP
- Mr.Tom
- June 01, 2012
- Học lập trình web PHP
- 3,765 views
Gửi mail SMTP - PHP đơn giản nhất - Send Mail using SMTP and PHP
Trong nội dung phần này gồm có 3 file:
- Index.php
- SMTPconfig.php // SMTP Server Cofiguration
- SMTPClass.php // SMTP Mail Sending Class
File đầu tiên, đó là file cấu hình các thông tin liên quan tới việc đăng nhập.
SMTPconfig.php You have to change SMTP server details.
<?php
//Server Address
$SmtpServer="127.0.0.1"; // server gửi email của bạn
$SmtpPort="25"; //default // cổng
$SmtpUser="username";
$SmtpPass="password";
?>
Ví dụ:
<?php
//Server Address
$SmtpServer="112.78.8.20"; // địa chỉ ip server share hosting
$SmtpPort="2095"; // cổng đăng nhập vào hệ thống mail
$SmtpUser="[email protected]";
$SmtpPass="password";
?>
Tuy nhiên, mình không dám chắc là sẽ thực hiện thành công với dịch vụ share hosting. Tốt nhất là đầu tư luôn 1 con server chuyên gửi email.
File thứ 2: chưa dòng lệnh để gửi email
SMTPclass.php SMTP mail sending class.
<?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;
}
}
?>
Và file cuối cùng là file ví dụ để kiểm tra lại kết quả.
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>
Download toàn bộ ví dụ về gửi mail bằng smtp và php tại đây
Công ty thiết kế website, lập trình phần mềm NEVICOM
Bài viết khác:
- Email Class PHP - Gửi Email bằng SMTP server. Full class_smtp source php
07/06/2012
- OpenID là gì | hướng dẫn cách sử dụng OpenID của yahoo, gmail bằng PHP. Đăng nhập gmail, yahoo bằng PHP
26/05/2012
- Ajax file upload - upload file tự động bằng PHP sử dụng ajax
26/05/2012
- Xử lý XML trong PHP5 một cách nhanh chóng và hiệu quả
26/05/2012
- 17 tùy biến .htaccess để cấu hình lại website, cấu hình lại appache để website bạn chạy theo ý muốn mình
19/05/2012
- Sao file .htaccess không chạy được trên windows? không chạy trên localhost
19/05/2012
- Help me! Làm sao để tạo được file .htaccess trên windows?
19/05/2012
- Đăng nhập vào quản trị không cần biết mật khẩu, kỹ thuật gọi là bypass login.
28/03/2012
- Phân tích lổ hỏng upload file và cách phòng chống (P1)
16/03/2012
- PHP code tạo captcha chống auto spam đơn giản, học PHP ai cũng có thể làm được
02/03/2012
- PHP căn bản: Bài 1 - Hướng dẫn cài đặt webserver trên windows - để chạy được php trên máy tính của mình
17/02/2012
Bình luận