-
-
Save aaoliveira/3826533 to your computer and use it in GitHub Desktop.
Using PHPMailer to attachment WordPress files
This file contains 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 | |
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); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment