Skip to content

Instantly share code, notes, and snippets.

@iegik
Created June 26, 2012 07:43
Show Gist options
  • Select an option

  • Save iegik/2994169 to your computer and use it in GitHub Desktop.

Select an option

Save iegik/2994169 to your computer and use it in GitHub Desktop.
Mail object, can convert simple html with inline styles to html mail
#!/usr/bin/php
<?php
require_once('ChromePhp.php');
class HTMLMailPart{
public $content;
public $type;
public $name;
public $path;
public function __construct($src)
{
//--
if(file_exists($src)){
$this->name = htmlentities($src);
}else{
console::log("File '".$src."' does not exist.\n");
}
$this->type = pathinfo($this->name, PATHINFO_EXTENSION);
$this->path = pathinfo($this->name, PATHINFO_DIRNAME);
$bin = fread(fopen($this->name, "r"), filesize($this->name));
if($this->type == 'html'||$this->type == 'css'){
$this->mime = 'text';
$this->content = $bin;
}else{
$this->mime = 'image';
$this->content = base64_encode($bin); // TODO: too long operation, optimize all images, before run this operation
}
}
}
class HTMLMail{
public $from;
public $to;
public $parts = array();
public $multipart
= 'mixed' // The primary subtype for multipart, "mixed", is intended for use when the body parts are independent and intended to be displayed serially. Any multipart subtypes that an implementation does not recognize should be treated as being of subtype "mixed".
// = 'alternative' // The multipart/alternative type is syntactically identical to multipart/mixed, but the semantics are different. In particular, each of the parts is an "alternative" version of the same information. User agents should recognize that the content of the various parts are interchangeable. The user agent should either choose the "best" type based on the user's environment and preferences, or offer the user the available alternatives. In general, choosing the best type means displaying only the LAST part that can be displayed. This may be used, for example, to send mail in a fancy text format in such a way that it can easily be displayed anywher
// = 'digest' // This document defines a "digest" subtype of the multipart Content-Type. This type is syntactically identical to multipart/mixed, but the semantics are different. In particular, in a digest, the default Content-Type value for a body part is changed from "text/plain" to "message/rfc822". This is done to allow a more readable digest format that is largely compatible (except for the quoting convention) with RFC 934.
// = 'parallel' // This document defines a "parallel" subtype of the multipart Content-Type. This type is syntactically identical to multipart/mixed, but the semantics are different. In particular, in a parallel entity, all of the parts are intended to be presented in parallel, i.e., simultaneously, on hardware and software that are capable of doing so. Composing agents should be aware that many mail readers will lack this capability and will show the parts serially in any event.
;
public function addPart($file){
$part = new HTMLMailPart($file);
if($part->type == ('html' || 'css')){
$re = '/[\'"](\S*\.(?:jpe?g|gif|png|css|html))[\'"]/im';
if (preg_match_all($re, $part->content, $matches)) {
$items = array_unique($matches[1]);
$part->inline = $part->content;
foreach($items as $subpart){
$s = $this->addPart($part->path.'/'.$subpart);
$s->cid = 'p'.(count($this->parts)-1);
$part->content = str_replace($subpart,'cid:'.$s->cid,$part->content);
$part->inline = str_replace($subpart,"data:".$part->mime."/".$part->type.";base64,".$s->content,$part->inline);
}
}
}
return $this->parts[] = $part;
}
public function __construct($html)
{
$this->addPart($html);
}
public function __toString()
{
return (@$this->headers||@$this->body)?@$this->headers.@$this->body:$this->generate();
}
public function generate()
{
$this->headers = '';
if(@$this->to && count($this->to)){
$this->headers = 'To: <';
var_dump($this->to);die;
if(@$this->to)foreach($this->to as $address){
$this->headers.=(@$i++>0?', <':'').$address.">";
}
}
if(@$this->from && count($this->from)){
$this->headers.= "\nFrom: <";
foreach($this->from as $address){
$this->headers.=(@$j++>0?', <':'').$address.">";
}
}
if(count($this->parts)){
$this->headers .= "\nContent-Type: multipart/" . $this->multipart . "; boundary=" . $this->multipart;
$this->body = '';
foreach($this->parts as $part){
$this->body .= "\n--".$this->multipart;
$this->body .= "\nContent-Type: " . $part->mime ."/" . $part->type;
if($part->mime != 'text')
{
$this->body .= "\nContent-type:text/html;charset = utf-8";
$this->body .= "\nContent-Id: <" . $part->cid . ">";
$this->body .= "\nContent-Location: " . $part->cid . "";
$this->body .= "\nContent-Transfer-Encoding: base64";
}
$this->body.= "\n\n".$part->content."\n";
}
$this->body .= "\n--".$this->multipart.'--';
}
return $this->__toString();
}
public function send(){
if(!@$this->headers||!@$this->body)
$this->generate();
return mail($this->to[0]
, @$this->subject
, $this->body
, $this->headers);
}
}
$args = @$_SERVER['argv']?:(@$_GET?:@$_POST);
$args[3] = !@$args[3]?!1:array_filter(array_unique(explode(',',implode(',',$args[3]))));
$m = new HTMLMail($args[1]);
$m->from[] = '[email protected]';
$m->subject = @$args[2]?:'no-replay';
$m->to = $args[3];
if(@$args[4]){
$m->send();
}
if(!@$args[3]){
$args[3] = array(
'[email protected]', // Hotmail
'[email protected]', // Google
'[email protected]', // Horde
);
}
if(!@$_SERVER['argv']){
?>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<form>
<p><input name="1" value="<?=@$args[1]?>" placeholder="html file"/></p>
<p><input name="2" value="<?=@$args[2]?>" placeholder="subject"/></p>
<p>
<input name="3[]" value="" placeholder="<?=count(@$args[3])==1?@$args[3][0]:implode(',',@$args[3])?>"/>
<?php if(@$args[3])foreach(@$args[3] as $mail):?>
<input name="3[]" value="<?=$mail?>" type="checkbox"> <?=$mail?>
<?php endforeach;?>
</p>
<p><input name="4" <?=!@$args[4]?:'selected'?> type="checkbox"> Send</p>
<p><input type="submit"/></p>
</form>
<textarea cols="90" rows="38" style="float:left"><?=$m->__toString();?></textarea>
<div style="overflow:auto;width:800px;height:600px;background-image:url('data:image/gif;base64,R0lGODdhEAAQAIACAGZmZpmZmSwAAAAAEAAQAAACH4RvoauIzNyBSyYaLMDZcv15HAaSIlWiJ5Sy a/RWVgEAOw==')">
<?php
foreach($m->parts as $part){
/*
if($part->mime == 'image'):?>
<img src="data:image/<?=$part->type?>;base64,<?=$part->content?>"/>
<?php endif;
*/
if($part->mime == 'text'){
echo $part->inline;
}
}
?>
</div>
<?php
}else{
echo $m->__toString();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment