August 24, 2015

Cấu hình gởi mail trên web bằng giao thức SMTP

Như các bạn đã biết, hiện nay để hạn chế tình trạng Spam mail, các nhà cung cấp hosting mặc định đã vô hiệu hóa chức năng gửi mail với hàm mail(), bài này sẽ hướng dẫn các bạn cách gửi mail trên web với giao thức SMTP.
Trước hết, tải PHPMailer về, giải nén và up lên host.
thực hiện cấu hình SMTP đầy đủ trong file test.php (file này chính là form gởi mail trên website) để thực hiện gởi mail hợp lệ.
Tất nhiên, trước khi khai báo cấu hình SMTP trong file PHP gởi mail (file test.php), bạn cần phải chắc chắn tồn tại 1 account mail SMTP trên host để thực hiện cấu hình thành công (có thể vào quản trị host để tạo mail này).
  • PHP code sử dụng mail hosting : 
<?php
require("class.phpmailer.php");
$mail = new PHPMailer();
$SMTP_Host = "mail.tenmiencuaban.com";
$SMTP_Port = 25;
$SMTP_UserName = "email@tenmiencuaban.com";
$SMTP_Password = "matkhau";
$from = $SMTP_UserName;
$fromName = "Ten Nguoi Gui";
$to = "diachiemailguiden@tenmien.com";
// Luu y: $SMTP_UserName = $from
//******************************************************************************/
/********* Hay bo comment di - Neu truong hop ban can gui toi nhieu mail khac **************/
//******************************************************************************/
// $addressCC = "email01@yahoo.com; email02@yahoo.com;";
// $mail->AddCC($addressCC);
//******************************************************************************/
$mail->IsSMTP();
$mail->Host     = $SMTP_Host;
$mail->SMTPAuth = true;

$mail->Username = $SMTP_UserName;
$mail->Password = $SMTP_Password;

$mail->From     = $from;
$mail->FromName = $fromName;
$mail->AddAddress($to);
$mail->AddReplyTo($from, $fromName);

$mail->WordWrap = 50;
//$mail->AddAttachment("Duong Dan File Dinh Kem");
$mail->IsHTML(true);

$mail->Subject  =  "Tieu de Mail";
$mail->Body     =  "Day la <b>noi dung mail</b>";
$mail->AltBody  =  "This is the text-only body";

if(!$mail->Send())
{
   echo "Mail gui that bai! <p>";
   echo "Thong bao loi: " . $mail->ErrorInfo;
   exit;
}
echo "Mail gui thanh cong!";
?>
<?
include "class.phpmailer.php";
include "class.smtp.php";

$mail = new PHPMailer();
//******************************************************************************/
// Trong truong hop gui mail GMAIL SMTP can duoc ho tro 2 yeu to sau:
// 1. php_openssl
// 2. Account Gmail enable POP trong Setting
//******************************************************************************/

$mail->IsSMTP(); // set mailer to use SMTP
$mail->Host = "smtp.gmail.com"; // specify main and backup server
$mail->Port = 465; // set the port to use
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->SMTPSecure = "ssl";
$mail->Username = "username@gmail.com"; // your SMTP username or your gmail username
$mail->Password = "passwordhere"; // your SMTP password or your gmail password
$from = "fromemail@somemail.com"; // Reply to this email
$to="emailnguoinhan@domain.com"; // Recipients email ID
$name="MatBao Technical Support"; // Recipient"s name
$mail->From = $from;
$mail->FromName = "Your From Name"; // Name to indicate where the email came from when the recepient received
$mail->AddAddress($to,$name);
$mail->AddReplyTo($from,"MatBao Technical Support");
$mail->WordWrap = 50; // set word wrap
$mail->IsHTML(true); // send as HTML
$mail->Subject = "Test mail script from matbao.net";
$mail->Body = "<b>Mail nay duoc goi bang phpmailer class. - <a href="http://matbao.net">matbao.net</a></b>"; //HTML Body
$mail->AltBody = "Mail nay duoc goi bang phpmailer class. - matbao.net"; //Text Body
//$mail->SMTPDebug = 2;
if(!$mail->Send())
{
        echo "<h1>Loi khi goi mail: " . $mail->ErrorInfo . "</h1>";
}
else
{
        echo "<h1>Send mail thanh cong</h1>";
}
?>


Related Posts:

  • Zimbra Hot Backup Restore Tool Tool to create and restore backups of Zimbra open source. Zimbra Backup and Restore "hot". By Richardson Lima Developed in Bash Script, but currently under development in the Python programming language. BACKUP Script 1&… Read More
  • Script backup zimbra mail##!/bin/bash clear echo Start time of the backup = $(date +%T) before="$(date +%s)" ## Backup Format FORMAT=tgz ## Backup location ZBACKUP=/srv/backup/ ## Folder name for backup and using date DATE=`date +"%d%m%y"` ## Bac… Read More
  • xử lý lỗi exim dead but subsys locked và quản lý mail queue exim 1. Cách khắc phục lỗi exim dead but subsys locked Khi sử dụng Direct Admin quản lý mail nghĩa là bạn đang dựa trên 2 dịch vụ mail có sẵn của Centost đó là dovecot cổng 110 để nhận mail và exim cổng 25 SMTP để gửi ma… Read More
  • Các lệnh thao tác với Exim - Linux Mail Server ( P2 )Hôm nay chúng tôi giới thiệu thêm cho bạn 1 vài command thường được sử dụng để kiểm tra spam emails & bulk email senders. Hy vọng sẽ ít nhiều giúp ích bạn trong các trường hợp cụ thể để xử lý vấn đề đang xảy ra tại máy ch… Read More
  • Các lệnh thao tác với Exim - Linux Mail ServerExim là một SMTP Server tốt, tuy nhiên đôi lúc bạn cũng phải thao tác để quản lý hàng đợi (Queue) trong trường hợp các user gởi quá nhiều email, hoặc bị Spam làm tắc nghẽn. Hiển thị danh sách các email đang nằm trong Mail Q… Read More

0 comments:

Post a Comment