Advertisement
tcelestino

Usando PHPMailer para anexos WordPress

Sep 29th, 2012
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.88 KB | None | 0 0
  1. <?php
  2.  
  3. define('WP_USE_THEMES', false);
  4.  
  5. require('../../../wp-load.php');
  6.  
  7. require_once("class.phpmailer.php");
  8.  
  9. if(isset($_POST)) {
  10.     $nome   = $_POST["name"];
  11.     $email  = $_POST["email"];
  12.     $tel    = $_POST["phone"];
  13.     $photos = (!isset($_POST["photos"])) ? NULL : $_POST["photos"];
  14.     $msg    = nl2br($_POST["mensagem"]);
  15.  
  16.     $status = array();
  17.  
  18.     if(empty($nome)) {
  19.         $status['status'] = '0';
  20.         $status["message"] = 'Digite o nome';
  21.  
  22.     } elseif(empty($email)) {
  23.         $status['status'] = '0';
  24.         $status["message"] = 'Digite o email';
  25.  
  26.     } else {
  27.  
  28.         //formato a mensagem
  29.         $message .= "<b>Nome:</b> \t$nome<BR>";
  30.         $message .= "<b>E-mail:</b> \t$email<BR>";
  31.         $message .= "<b>Telefone:</b> \t$tel<BR>";
  32.         $message .= "<b>Mensagem:</b> \t$msg<BR>";
  33.  
  34.  
  35.         //crio um objeto do PHPMailer
  36.         $mail = new PHPMailer();
  37.  
  38.         //configuração do servidor para rodar o PHPMailer
  39.         $mail->IsSMTP();
  40.         $mail->Host = "localhost";
  41.  
  42.         //configuro os remetentes
  43.         $mail->From = ""; // email configurado no servidor
  44.         $mail->Sender = ""; // email configurado no servidor
  45.         $mail->FromName = ""; // nome configurado no servidor
  46.  
  47.         //inclue a mensagem a ser enviada
  48.         $mail->MsgHTML($message);
  49.  
  50.         if($photos) {
  51.             foreach($photos as $photo) {
  52.                 $baseurl = basename($photo);
  53.  
  54.                 //print_r($path);
  55.  
  56.                 $url = parse_url($photo);
  57.                 $url = substr($url['path'], -28);
  58.  
  59.                 $mail->AddAttachment(WP_CONTENT_DIR . '/uploads/'.$url, $baseurl);
  60.             }
  61.         }
  62.  
  63.         $mail->CharSet = 'utf-8';
  64.         $mail->SetFrom($email, $nome);
  65.         $mail->AddAddress("email_que_vai_receber", "nome_do_responsavel_do_email");
  66.         $mail->Subject  = ("Titulo do email");
  67.         $mail->AddReplyTo("$email", "$nome");
  68.  
  69.         if($mail->Send()){
  70.             $status['status'] = '1';
  71.             $status["message"] = 'Enviado com sucessso';
  72.         } else {
  73.             $status['status'] = '0';
  74.             $status["message"] = $mail->ErrorInfo;
  75.         }      
  76.     }
  77. echo json_encode($status);
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement