oriolrius.cat

Des del 2000 compartiendo sobre…

Tag: phpmailer

phpMailer – una classe de PHP per enviar emails

Reading time: 5 – 8 minutes

En PHP quan s’ha d’enviar un email sovint s’usa la funció mail() tot i que aquesta és una mica limitada en quan a paràmetres que se li poden passar al servidor SMTP. Per exemple, temes d’autenticació i xifrat. Via pear hi ha diverses solucions però a vegades cal que la solució que incorpori el nostre aplicatiu sigui independent d’aquest tipus de requisits de sistema. Així doncs phpMailer ens pot servir per sortir de més d’un embolic.

Suporta:

  • Can send emails with multiple TOs, CCs, BCCs and REPLY-TOs
  • Redundant SMTP servers
  • Multipart/alternative emails for mail clients that do not read HTML email
  • Support for 8bit, base64, binary, and quoted-printable encoding
  • Uses the same methods as the very popular AspEmail active server (COM) component
  • SMTP authentication
  • Word wrap
  • Address reset functions
  • HTML email
  • Tested on multiple SMTP servers: Sendmail, qmail, Postfix, Imail, Exchange, etc
  • Works on any platform
  • Flexible debugging
  • Custom mail headers
  • Multiple fs, string, and binary attachments (those from database, string, etc)
  • Embedded image support

Un petit exemple d’ús perquè vegeu el senzill que és usar-lo:

require("class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP();                                   // send via SMTP
$mail->Host     = "smtp1.site.com;smtp2.site.com"; // SMTP servers
$mail->SMTPAuth = true;     // turn on SMTP authentication
$mail->Username = "jswan";  // SMTP username
$mail->Password = "secret"; // SMTP password
$mail->From     = "from@email.com";
$mail->FromName = "Mailer";
$mail->AddAddress("josh@site.com","Josh Adams");
$mail->AddAddress("ellen@site.com");               // optional name
$mail->AddReplyTo("info@site.com","Information");
$mail->WordWrap = 50;                              // set word wrap
$mail->AddAttachment("/var/tmp/file.tar.gz");      // attachment
$mail->AddAttachment("/tmp/image.jpg", "new.jpg");
$mail->IsHTML(true);                               // send as HTML
$mail->Subject  =  "Here is the subject";
$mail->Body     =  "This is the <b>HTML body</b>";
$mail->AltBody  =  "This is the text-only body";
if(!$mail->Send())
{
   echo "Message was not sent <p>";
   echo "Mailer Error: " . $mail->ErrorInfo;
   exit;
}
echo "Message has been sent";

Aquesta troballa l’he fet a través de l’article phpMailer , a great Email transfer class del blog de Moody Bahrain.