Below is the code which shows the following functionalities
1) Read a file from FTP using PHP
2) Send mail using php
3) Send mail with a dynamic html template
4) Send a mail with attachment from FTP using PHP
<?php
function flush_disp(){
echo (str_repeat(' ',256));
// check that buffer is actually set before flushing
if (ob_get_length()){
@ob_flush();
@flush();
@ob_end_flush();
}
@ob_start();
}
?>
<?php
// Class : begin
class sendMailwithAttachment
{
var $MailHost;
var $SMTPAuth;
var $sub;
var $bdy;
var $adres;
var $bccadres;
var $adnm;
var $ftpHost;
var $ftpuserName;
var $ftpPwd;
function sendMailwithAttachment()
{
// Method : begin
$this->MailHost = "mail.domain.com";
$this->bccadres = "bccaddress@domain.com";
$this->SMTPAuth = true;
$ftpHost="ftpserver.domain.com";
$ftpuserName="user";
$ftpPwd="pwd";
}
// Method : end
function getFile($fn)
{
// Method : begin
$return = '';
if ($fp = fopen($fn, 'rb')) {
while (!feof($fp))
{
$return .= fread($fp, 1024);
}
fclose($fp);
return $return;
} else {
return false;
}
} // Method : end
function sendMail($emailto, $Name)
{
// Method : begin
include_once('class.phpmailer.php');
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->IsHTML(true);
$mail->Host = $this->Host;
$mail->SMTPAuth = $this->SMTPAuth;
$mail->FromName = 'FROMNAME';
$mail->Username = 'test@domain.com';
$mail->Password = '123456789';
$mail->From = $mail->Username;
$body = $this->getFile('test.html');
$body = str_replace("{Name}", $Name, $body );
$this->bdy = $body;
$this->adres = $emailto;
$this->adnm = $Name;
$this->bdy = eregi_replace("[\]",'',$this->bdy);
$this->sub = eregi_replace("[\]",'',$this->sub);
$mail->Subject = 'MAIL SUBJECT';
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!";
// optional, comment out and test
$mail->MsgHTML($this->bdy);
$mail->AddAddress($this->adres, $this->adnm);
$mail->AddBCC($this->bccadres, $this->adnm);
//FTP CODE
$conn = ftp_connect(ftpHost) or die("Could not connect");
ftp_login($conn,$ftpuserName,$ftpPwd);
//THE PATH IN THE LOCAL SYSTEM WHERE WE NEED TO COPY THE FILE FROM FTP SERVER
$localpath="TEMPFOLDER\\".$filename".pdf";
//USING ftp_get() function to copy file from FTP SERVER
ftp_get($conn,"TEMPFOLDER\\".$filename".pdf","FTPFOLDER\\SUBFOLDER\\".$filename.".pdf",FTP_ASCII);
// CLOSING THE FTP CONNECTION
ftp_close($conn);
//END
$mail->AddAttachment($localpath);
if(!$mail->Send()) {
//UNLINK IS USED TO REMOVE THE FILE FROM LOCAL SYSTEM IF NEEDED
unlink($localpath);
return 'Failed to send mail to '.$to_email;
} else {
//UNLINK IS USED TO REMOVE THE FILE FROM LOCAL SYSTEM IF NEEDED
unlink($localpath);
return 'Mail sent to '.$to_email;
}
} // Method : end
}
// Class : end
function flush_disp(){
echo (str_repeat(' ',256));
// check that buffer is actually set before flushing
if (ob_get_length()){
@ob_flush();
@flush();
@ob_end_flush();
}
@ob_start();
}
?>
<?php
// Class : begin
class sendMailwithAttachment
{
var $MailHost;
var $SMTPAuth;
var $sub;
var $bdy;
var $adres;
var $bccadres;
var $adnm;
var $ftpHost;
var $ftpuserName;
var $ftpPwd;
function sendMailwithAttachment()
{
// Method : begin
$this->MailHost = "mail.domain.com";
$this->bccadres = "bccaddress@domain.com";
$this->SMTPAuth = true;
$ftpHost="ftpserver.domain.com";
$ftpuserName="user";
$ftpPwd="pwd";
}
// Method : end
function getFile($fn)
{
// Method : begin
$return = '';
if ($fp = fopen($fn, 'rb')) {
while (!feof($fp))
{
$return .= fread($fp, 1024);
}
fclose($fp);
return $return;
} else {
return false;
}
} // Method : end
function sendMail($emailto, $Name)
{
// Method : begin
include_once('class.phpmailer.php');
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->IsHTML(true);
$mail->Host = $this->Host;
$mail->SMTPAuth = $this->SMTPAuth;
$mail->FromName = 'FROMNAME';
$mail->Username = 'test@domain.com';
$mail->Password = '123456789';
$mail->From = $mail->Username;
$body = $this->getFile('test.html');
$body = str_replace("{Name}", $Name, $body );
$this->bdy = $body;
$this->adres = $emailto;
$this->adnm = $Name;
$this->bdy = eregi_replace("[\]",'',$this->bdy);
$this->sub = eregi_replace("[\]",'',$this->sub);
$mail->Subject = 'MAIL SUBJECT';
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!";
// optional, comment out and test
$mail->MsgHTML($this->bdy);
$mail->AddAddress($this->adres, $this->adnm);
$mail->AddBCC($this->bccadres, $this->adnm);
//FTP CODE
$conn = ftp_connect(ftpHost) or die("Could not connect");
ftp_login($conn,$ftpuserName,$ftpPwd);
//THE PATH IN THE LOCAL SYSTEM WHERE WE NEED TO COPY THE FILE FROM FTP SERVER
$localpath="TEMPFOLDER\\".$filename".pdf";
//USING ftp_get() function to copy file from FTP SERVER
ftp_get($conn,"TEMPFOLDER\\".$filename".pdf","FTPFOLDER\\SUBFOLDER\\".$filename.".pdf",FTP_ASCII);
// CLOSING THE FTP CONNECTION
ftp_close($conn);
//END
$mail->AddAttachment($localpath);
if(!$mail->Send()) {
//UNLINK IS USED TO REMOVE THE FILE FROM LOCAL SYSTEM IF NEEDED
unlink($localpath);
return 'Failed to send mail to '.$to_email;
} else {
//UNLINK IS USED TO REMOVE THE FILE FROM LOCAL SYSTEM IF NEEDED
unlink($localpath);
return 'Mail sent to '.$to_email;
}
} // Method : end
}
// Class : end
No comments:
Post a Comment