Created
September 19, 2012 20:15
-
-
Save jrobinsonc/3751966 to your computer and use it in GitHub Desktop.
Enviar email usando la libreria PHPMailer
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 | |
/** | |
* Envio de emails. | |
* | |
* @param string $to Destinatario del email. | |
* @param string $subject Asunto. | |
* @param string $body Mensaje. | |
* @param string $alt_body (Opcional) Mensaje en texto plano. | |
* @return mixed Devuelve TRUE si todo paso bien o un String con el error si algo fallo. | |
*/ | |
function send_mail($to, $subject, $body, $alt_body = '', $attachments = array()) | |
{ | |
require_once APP_PATH . '/lib/PHPMailer_v5.1/class.phpmailer.php'; | |
require_once APP_PATH . '/lib/PHPMailer_v5.1/class.smtp.php'; | |
$mail = new PHPMailer(TRUE); | |
//$mail->SMTPDebug = 2; | |
$mail->IsSMTP(); | |
$mail->SMTPAuth = SMTP_AUTH; | |
$mail->SMTPSecure = SMTP_SECURE; | |
$mail->Port = SMTP_PORT; | |
$mail->Host = SMTP_HOST; | |
$mail->Username = SMTP_USER; | |
$mail->Password = SMTP_PASS; | |
$mail->SetFrom(SMTP_FROM, SMTP_FROM_NAME); | |
$mail->AddReplyTo(SMTP_FROM, SMTP_FROM_NAME); | |
// Se setea el asunto y el mensaje. | |
$mail->Subject = $subject; | |
if (strlen($alt_body) > 0) | |
$mail->AltBody = $alt_body; | |
$mail->MsgHTML($body); | |
// Archivos adjuntos. | |
if (count($attachments) > 0) | |
{ | |
foreach ($attachments as $attachment) | |
{ | |
if (!is_file($attachment)) continue; | |
$mail->AddAttachment($attachment, basename($attachment), 'base64', mime_content_type($attachment)); | |
} | |
} | |
if (!$mail->ValidateAddress($to)) return 'Invalid email address.'; | |
// Se setea el destinatario. | |
if (is_array($to) && count($to) === 2) | |
$mail->AddAddress($to[0], $to[1]); | |
else if (is_array($to) && count($to) === 1) | |
$mail->AddAddress($to[0]); | |
else if (is_string($to)) | |
$mail->AddAddress($to); | |
// Se intenta enviar. | |
try { | |
$result = $mail->Send(); | |
} catch (Exception $exc) { | |
$result = $exc->getMessage(); | |
} | |
// Se resetean los destinatarios. | |
$mail->ClearAddresses(); | |
//--------------------------------------------------- | |
return $result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment