Task #946 : envoi de fichier par email
Task #946 - Envoi de fichier par email
This commit is contained in:
parent
476ffaea40
commit
9c9003644a
2 changed files with 197 additions and 0 deletions
56
include/class_filetosend.php
Normal file
56
include/class_filetosend.php
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
<?php
|
||||
/**
|
||||
* @brief file to add to a message
|
||||
*
|
||||
* @see Sendmail
|
||||
* @author dany
|
||||
*/
|
||||
class FileToSend
|
||||
{
|
||||
var $filename;
|
||||
var $type;
|
||||
var $path;
|
||||
var $full_name;
|
||||
function __construct($p_filename,$p_type="")
|
||||
{
|
||||
$this->full_name=$p_filename;
|
||||
if (strpos($p_filename,'/') != false)
|
||||
{
|
||||
$this->path=dirname($p_filename);
|
||||
}
|
||||
$this->filename=basename ($p_filename);
|
||||
if ( $p_type=="")
|
||||
{
|
||||
$this->guess_type();
|
||||
|
||||
}
|
||||
}
|
||||
function guess_type()
|
||||
{
|
||||
$ext_pos= strrpos($this->filename,'.');
|
||||
if ( $ext_pos == false ) {
|
||||
$this->type="application/octect";
|
||||
return;
|
||||
}
|
||||
$ext= substr($this->filename, $ext_pos+1, 3);
|
||||
switch ($ext)
|
||||
{
|
||||
case 'odt':
|
||||
$this->type='application/vnd.oasis.opendocument.text';
|
||||
break;
|
||||
case 'ods':
|
||||
$this->type='application/vnd.oasis.opendocument.spreadsheet';
|
||||
break;
|
||||
case 'pdf':
|
||||
$this->type="application/pdf";
|
||||
break;
|
||||
case 'zip':
|
||||
$this->type="application/zip";
|
||||
break;
|
||||
default:
|
||||
$this->type="application/octect";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
141
include/class_sendmail.php
Normal file
141
include/class_sendmail.php
Normal file
|
|
@ -0,0 +1,141 @@
|
|||
<?php
|
||||
|
||||
|
||||
/**
|
||||
* Description of class_sendmail
|
||||
*
|
||||
* @author dany
|
||||
*/
|
||||
require_once 'class_filetosend.php';
|
||||
|
||||
class Sendmail
|
||||
{
|
||||
|
||||
private $mailto;
|
||||
private $afile;
|
||||
private $subject;
|
||||
private $message;
|
||||
private $from;
|
||||
private $content;
|
||||
|
||||
/**
|
||||
* set the from
|
||||
* @parameter $p_from has the form name <info@phpcompta.eu>
|
||||
*/
|
||||
function set_from($p_from)
|
||||
{
|
||||
$this->from = $p_from;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param $p_subject set the subject
|
||||
*/
|
||||
function set_subject($p_subject)
|
||||
{
|
||||
$this->subject = $p_subject;
|
||||
}
|
||||
|
||||
/**
|
||||
* set the recipient
|
||||
* @param type $p_mailto has the form name <email@email.com>
|
||||
*/
|
||||
function mailto($p_mailto)
|
||||
{
|
||||
$this->mailto = $p_mailto;
|
||||
}
|
||||
|
||||
/**
|
||||
* body of the message (utf8)
|
||||
* @param type $p_message
|
||||
*/
|
||||
function set_message($p_message)
|
||||
{
|
||||
$this->message = $p_message;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add file to the message
|
||||
* @param FileToSend $file file to add to the message
|
||||
*/
|
||||
function add_file(FileToSend $file)
|
||||
{
|
||||
$this->afile[] = $file;
|
||||
}
|
||||
|
||||
/**
|
||||
* verify that the message is ready to go
|
||||
* @throws Exception
|
||||
*/
|
||||
function verify()
|
||||
{
|
||||
$array = explode(",", "from,subject,mailto,message");
|
||||
for ($i = 0; $i < count($array); $i++)
|
||||
{
|
||||
$name = $array[$i];
|
||||
if (trim($this->$name) == "")
|
||||
{
|
||||
throw new Exception("$name est vide");
|
||||
}
|
||||
}
|
||||
}
|
||||
/**
|
||||
* create the message before sending
|
||||
*/
|
||||
function compose()
|
||||
{
|
||||
$this->verify();
|
||||
$uid = md5(uniqid(time()));
|
||||
|
||||
// a random hash will be necessary to send mixed content
|
||||
$separator = md5(time());
|
||||
|
||||
// carriage return type (we use a PHP end of line constant)
|
||||
$eol = PHP_EOL;
|
||||
|
||||
// main header (multipart mandatory)
|
||||
$headers = "From: " . $this->from . $eol;
|
||||
$headers .= "MIME-Version: 1.0" . $eol;
|
||||
$headers .= "Content-Type: multipart/mixed; boundary=\"" . $separator . "\"" . $eol . $eol;
|
||||
$headers .= "Content-Transfer-Encoding: 7bit" . $eol;
|
||||
$headers .= "This is a MIME encoded message." . $eol . $eol;
|
||||
$headers .= $eol . $eol;
|
||||
|
||||
// message
|
||||
$headers .= "--" . $separator . $eol;
|
||||
$headers .= "Content-Type: text/plain; charset=\"utf-8\"" . $eol;
|
||||
$headers .= "Content-Transfer-Encoding: 7bit" . $eol . $eol;
|
||||
$headers .= $this->message . $eol . $eol;
|
||||
|
||||
// attachment
|
||||
for ($i = 0; $i < count($this->afile); $i++)
|
||||
{
|
||||
$file = $this->afile[$i];
|
||||
$file_size = filesize($file->full_name);
|
||||
$handle = fopen($file->full_name, "r");
|
||||
$content = fread($handle, $file_size);
|
||||
fclose($handle);
|
||||
$content = chunk_split(base64_encode($content));
|
||||
$headers .= "--" . $separator . $eol;
|
||||
$headers .= "Content-Type: " . $file->type . "; name=\"" . $file->filename . "\"" . $eol;
|
||||
$headers .= "Content-Disposition: attachment; filename=\"" . $file->filename . "\"" . $eol;
|
||||
$headers .= "Content-Transfer-Encoding: base64" . $eol;
|
||||
$headers .= $content . $eol . $eol;
|
||||
}
|
||||
$headers .= "--" . $separator . "--";
|
||||
$this->content = $headers;
|
||||
}
|
||||
/**
|
||||
* Send the message
|
||||
* @throws Exception
|
||||
*/
|
||||
function send()
|
||||
{
|
||||
//SEND Mail
|
||||
if (!mail($this->mailto, $this->subject, "", $this->content))
|
||||
{
|
||||
throw new Exception('send failed');
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue