MyPHPMailer multi Language Body Class

I developed some applications need an easy and quick support languages for PHPMailer class. If you have to manage many email bodies with more languages this class can help you!

This is the tree example of the class:

  • MyPHPMailer (directory)
    • MyPHPMailer.php
    • MyPHPMailer-Config.php
    • class.phpmailer.php
    • class.smtp.php
    • test.php
    • language (directory)
      • Email-en.php
      • Email-it.php

First step – Configuration file

Open the MyPHPMailer-Config.php, you will see:


/* MyPHPMailer Configuration file */
// domain
define("_MyMailer_Domain", "domain.com");
// Default Address
define("_MyMailer_Default_Address", "info");
// Default Subject
define("_MyMailer_Default_Subject", "Message from domain.com");
// Sender Mailer
define("_MyMailer_Mailer", "mail");

The parameters defined are:

  • _MyMailer_Domain
    the domain sender (set here the domain of the FROM address)
  • _MyMailer_Default_Address
    the default account address FROM; if you set info the email will be sent from info@_MyMailer_Domain
  • _MyMailer_Default_Subject
    default subject of your email (you can change it later)
  • _MyMailer_Mailer
    this is the class.phpmailer.php parameters (smtp, mail, sendmail, etc)

Second step – write your language email boby

In the language directory you will find two file: Email-en.php (English example) and Email-it.php (Italian example).
Create other files for every language you’ll manage in your application: Email-de.php (German), ecc. The “de” extension is free (see below).
Put in this files an email body like this:

## Welcome
define("_EMAIL_WELCOME_ACCOUNT", "
Welcome <b>%s</b>,
Thank you for registering in our site!
" );

Define all bodies that you want.
%s are a variables you will set after, in the code, with a direct method (see below).

Third step – send email 😉

In your application (see test.php) you may send email with this rows:

include("MyPHPMailer.php");
// init MyPHPMailer class with en (English) language
$mail = new MyPHPMailer("en");
// set TO
$mail->AddAddress("someuser@somedomain.com", "Name Surname");
// set BODY and message vars
$arVal = array('account_name', 'account_EMail', 'account_EMail', 'account_password');
$mail->init_EMAIL_SEND_PASSWORD("Subject email", $arVal);
if( $mail->Send() )
{
echo "OK - Email SENDED!!";
} else {
echo "KO - ERROR: " . $mail->ErrorInfo;
}

The method init_EMAIL_SEND_PASSWORD (called in the above example) is not exist in the MyPHPMailer class, there is an overloading __call method. So, you can prepares all bodies easily with dynamic method:

$mail->init_[DEFINED_BODY_IN_LANGUAGE_FILES]
The parameters are:

  1. Subject
  2. Array values to put in the body (through vsprintf php function)

See language file (ex: Email-en.php) for the _EMAIL_SEND_PASSWORD defined body. You will find 4 vars (%s), like the $arVal array above.

Then invoke $mail->Send for send the email.

Advanced configuration – more Address FROM

If you have more address than info (like this example) open MyPHPMailer-Config.php, search for the array $_MyMailer_Address and push other value:

// Address email
$_MyMailer_Address = array(
'info' => 'Info name',
'webmaster' => 'Webmaster name',
'sender' => 'Sender name',
);

Now you may send your emails with info, webmaster and sender account.
In your application (see test.php) you may send email with other address (not default) :

// set FROM (not use default): webmaster
$mail->Set_From("webmaster");

This email start from webmaster@yourdomain.com.

Download

Ok, now you are ready to start!

Download MyPHPMailer class Version 0.2 (include PHPMailer Version 2.2.1)

Sorry for my english 🙁

Lascia un commento

Il tuo indirizzo email non sarà pubblicato. I campi obbligatori sono contrassegnati *