Created
March 16, 2011 21:18
-
-
Save luizfonseca/873331 to your computer and use it in GitHub Desktop.
Sample class that parse a CSV file, a HTML file and send it.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @author Luiz Fonseca | |
* Sample Class | |
* | |
*/ | |
class Marketing { | |
public $destination = null; | |
public $type = 'html'; | |
public $file = null; | |
public $csv = null; | |
public $subject = 'Assunto exemplo'; | |
public $body = 'Sample Body'; | |
public $wordwrap = null; | |
public function send_marketing() | |
{ | |
if($this->destination == null) | |
{ | |
trigger_error('You MUST set the Destination directory', E_USER_WARNING); | |
} | |
else | |
{ | |
if (is_array($this->file) && is_array($this->csv)) | |
{ | |
$this->parse_file(); | |
$this->open_file(); | |
} | |
} | |
} | |
public function check_file() | |
{ | |
if ($this->type == 'html') | |
{ | |
return true; | |
} | |
else | |
{ | |
return false; | |
} | |
} | |
protected function parse_file() | |
{ | |
if($this->check_file() === true) | |
{ | |
$this->type = '.' . $this->type; | |
$file_extension = strrchr($this->file['file']['name'], '.'); | |
$csv_extension = strrchr($this->csv['csv']['name'], '.'); | |
if ($file_extension == $this->type) | |
{ | |
if ($this->file['file']['error'] == UPLOAD_ERR_OK) | |
{ | |
if($csv_extension == '.csv' AND $this->csv['csv']['error'] == UPLOAD_ERR_OK) | |
{ | |
move_uploaded_file($this->csv['csv']['tmp_name'], $this->destination . $this->csv['csv']['name']); | |
} | |
else | |
{ | |
trigger_error('It\'s not a CSV file', E_USER_ERROR ); | |
} | |
move_uploaded_file($this->file['file']['tmp_name'], $this->destination . $this->file['file']['name']); | |
} | |
else | |
{ | |
trigger_error('File coundn\'t be uploaded', E_USER_ERROR); | |
} | |
} | |
} | |
} | |
protected function open_file() | |
{ | |
if ($this->file) | |
{ | |
if (is_readable($this->destination . $this->file['file']['name'])) | |
{ | |
$file = $this->destination . $this->file['file']['name']; | |
$csv = $this->destination . $this->file['csv']['name']; | |
$file_content = file_get_contents($file); | |
$csv_content = fopen($csv, 'r'); | |
while (!feof($csv_content)) | |
{ | |
$csv_data[] = fgetcsv($csv_content, 8000, ';'); | |
} | |
$csv_data = array_filter($csv_data); | |
fclose($csv_content); | |
foreach ($csv_data as $data) | |
{ | |
foreach ($data as $key => $value) | |
{ | |
$this->send_mailing($data[0], $data[1], $file_content); | |
echo '<em>Marketing enviado para ',$data[1],' com sucesso</em>.<br/>'; | |
} | |
} | |
} | |
} | |
} | |
protected function send_mailing($to_name, $to_email, $body) | |
{ | |
$mailing = new PHPMailer; | |
//$mailing->IsSMTP(); // set mailer to use SMTP | |
$mailing->From = "[email protected]"; | |
$mailing->FromName = "Sample"; | |
$mailing->AddAddress($to_email, $to_name); | |
$mailing->IsHTML(true); // set email format to HTML | |
$mailing->Subject = $this->subject; | |
$mailing->Body = $body; | |
$mailing->Send(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment