Skip to content

Instantly share code, notes, and snippets.

@tcelestino
Last active October 11, 2015 07:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tcelestino/3825119 to your computer and use it in GitHub Desktop.
Save tcelestino/3825119 to your computer and use it in GitHub Desktop.
Using PHPMailer to attachment WordPress files
<?php
define('WP_USE_THEMES', false);
require('../../../wp-load.php');
require_once("class.phpmailer.php");
if(isset($_POST)) {
$nome = $_POST["name"];
$email = $_POST["email"];
$tel = $_POST["phone"];
$photos = (!isset($_POST["photos"])) ? NULL : $_POST["photos"];
$msg = nl2br($_POST["mensagem"]);
$status = array();
if(empty($nome)) {
$status['status'] = '0';
$status["message"] = 'Digite o nome';
} elseif(empty($email)) {
$status['status'] = '0';
$status["message"] = 'Digite o email';
} else {
//formato a mensagem
$message .= "<b>Nome:</b> \t$nome<BR>";
$message .= "<b>E-mail:</b> \t$email<BR>";
$message .= "<b>Telefone:</b> \t$tel<BR>";
$message .= "<b>Mensagem:</b> \t$msg<BR>";
//crio um objeto do PHPMailer
$mail = new PHPMailer();
//configuração do servidor para rodar o PHPMailer
$mail->IsSMTP();
$mail->Host = "localhost";
//configuro os remetentes
$mail->From = ""; // email configurado no servidor
$mail->Sender = ""; // email configurado no servidor
$mail->FromName = ""; // nome configurado no servidor
//inclue a mensagem a ser enviada
$mail->MsgHTML($message);
// verifico se existe a foto
// para poder anexar ela ao email
if($photos) {
foreach($photos as $photo) {
$baseurl = basename($photo);
//print_r($path);
$url = parse_url($photo);
$url = substr($url['path'], -28);
$mail->AddAttachment(WP_CONTENT_DIR . '/uploads/'.$url, $baseurl);
}
}
$mail->CharSet = 'utf-8';
$mail->SetFrom($email, $nome);
$mail->AddAddress("email_que_vai_receber", "nome_do_responsavel_do_email");
$mail->Subject = ("Titulo do email");
$mail->AddReplyTo("$email", "$nome");
if($mail->Send()){
$status['status'] = '1';
$status["message"] = 'Enviado com sucessso';
} else {
$status['status'] = '0';
$status["message"] = $mail->ErrorInfo;
}
}
echo json_encode($status);
}
var TEMPLATE_URL = "<?php bloginfo('template_url'); ?>";
(function(){
var $images = $("li.images"); // pai de todos os elementos
var $links_imagens = $images.find('a'); // links que são clicaveis
var itens_anexos = []; // array que guarda as url das imagens
$links_imagens.on('click', function(evt){
var self = $(this); // o proprio link
// adiciona para click a url da imagem no array
self.each(function(){
itens_anexos.push($(this).attr('href'));
});
self.remove(); // remove o link clicado
// verifica a quantidade de imagens inseridas no array
// caso as 5 forem selecionadas, some toda a linha
if(itens_anexos.length == 5) {
$images.fadeOut(1000, function(){
$(this).remove();
});
}
evt.preventDefault();
evt.stopPropagation();
});
// envia os dados para o email
// por uma requisicao ajax
var form = $("form");
form.on('submit', function(evt){
var name, email, phone, msg;
name = $("input#name").val();
email = $("input#email").val();
phone = $("input#phone").val();
msg = $("textarea#mensagem").val();
console.log(name + " <br />" + email + " <br />" + phone + " <br />" + msg );
$.ajax({
url: TEMPLATE_URL+"/mailer.php",
data: {
name: name,
email: email,
phone: phone,
mensagem: msg,
photos:itens_anexos
},
type: "POST",
beforeSend: function(){},
success: function(data){
console.log(data);
},
error: function(statusError) {
console.log(statusError)
}
})
evt.preventDefault();
})
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment