Enviar mail des de PHP: phpMailer

De Wikijoan
Dreceres ràpides: navegació, cerca

És ben simple configurar el pgpMailer per enviar mails des de PHP.

Descarreguer pgpMailer des de Sourceforge: phpMailer_v2.3.tar.gz,

i el descomprimim a la carpeta phpMailer que penja del nostre projecte.

Configuració del SMTP de gmail

Disposo de un compte de gmail:

Amb aquestes dades n'hi ha prou.

dins de la carpeta examples hi ha el fitxer test_gmail.php, que el configuro amb les meves dades (compte perquè hi ha un error en la ruta relativa del include):

fitxer test_gmail.php:

?php

//error_reporting(E_ALL);
error_reporting(E_STRICT);

date_default_timezone_set('America/Toronto');

include("../class.phpmailer.php");
//include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded

$mail             = new PHPMailer();

$body             = $mail->getFile('contents.html');
$body             = eregi_replace("[\]",'',$body);

$mail->IsSMTP();
$mail->SMTPAuth   = true;                  // enable SMTP authentication
$mail->SMTPSecure = "ssl";                 // sets the prefix to the servier
$mail->Host       = "smtp.gmail.com";      // sets GMAIL as the SMTP server
$mail->Port       = 465;                   // set the SMTP port for the GMAIL server

$mail->Username   = "joanqc@gmail.com";  // GMAIL username
$mail->Password   = "jq****";            // GMAIL password

$mail->AddReplyTo("joanqc@gmail.com","Joan Q");

$mail->From       = "Joanqc@gmail.com";
$mail->FromName   = "Joan Q";

$mail->Subject    = "PHPMailer Test Subject via gmail";

//$mail->Body       = "Hi,<br>This is the HTML BODY<br>";                      //HTML Body
$mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->WordWrap   = 50; // set word wrap

$mail->MsgHTML($body);

$mail->AddAddress("joan_quintana@yahoo.com", "Joan Quinti");

$mail->AddAttachment("images/phpmailer.gif");             // attachment

$mail->IsHTML(true); // send as HTML

if(!$mail->Send()) {
  echo "Mailer Error: " . $mail->ErrorInfo;
} else {
  echo "Message sent!";
}

?>


Configuració del SMTP de indicerural.com

test_smtp_indicerural.php

<?php

//error_reporting(E_ALL);
error_reporting(E_STRICT);

date_default_timezone_set('America/Toronto');
//date_default_timezone_set(date_default_timezone_get());

include_once('../class.phpmailer.php');
//include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded

$mail             = new PHPMailer();

$body             = $mail->getFile('contents.html');
$body             = eregi_replace("[\]",'',$body);

$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host       = "smtp.indicerural.com"; // SMTP server

$mail->SMTPAuth = true; //or True if your smtp server requires authentication.
$mail->Username = "sn7333";
$mail->Password = "****";

$mail->From       = "administrador@indicerural.com";
$mail->FromName   = "Joan Q";

$mail->Subject    = "PHPMailer Test Subject via smtp";

$mail->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test

$mail->MsgHTML($body);

$mail->AddAddress("joan_quintana@yahoo.com", "Joni Q");

$mail->AddAttachment("images/phpmailer.gif");             // attachment

if(!$mail->Send()) {
  echo "Mailer Error: " . $mail->ErrorInfo;
} else {
  echo "Message sent!";
}

?>

creat per Joan Quintana Compte, març 2009

Eines de l'usuari
Espais de noms
Variants
Accions
Navegació
IES Jaume Balmes
Màquines recreatives
CNC
Informàtica musical
joanillo.org Planet
Eines