Inviare email tramite php

From RVM Wiki
Jump to navigation Jump to search
Attenzione questo articolo è ancora incompleto.
Sentiti libero di contribuire cliccando sul tasto edit.

Installare il pacchetto

apt-get install libphp-phpmailer

PHP doesn't allow you to send emails with SMTP authentication through the common command mail().

When you write scripts to send mails your server won't recognize you and sometimes will block your email except for the local network.

You also need authentication to send HTML Mails

To overcome this you should write a script to directly connect to SMTP socket passing parameters to the mail server through it.
As it is an hard work and require a good knowledge on sockets and PHP programming, every "normal" PHP user can download and install PHPmailer (http://phpmailer.sourceforge.net/).

This PHP application includes many options, but we are focusing on simple use of authentication protocol. The installation need a folder (Ex. /PHPmailer) and 2 classes:

  • class.phpmailer.php
  • class.smtp.php
    These are the 2 files you must have in your new folder with the /language folder.

    The other files you will find in the .zip are /docs (examples described), /test (testing installation), /phpdocs (documentation) and don't need to be uploaded onto your site.

    After you copy the files on your site you will be able to use PHPmailer in this way:

    1. insert

    CODE
    require("phpmailer/class.phpmailer.php");


    in your .php file to load the functions.


    2. use this code to send a mail:

    CODE
    $mail = new PHPMailer(); //Create PHPmailer class
    $mail->From = "youremail@yourdomain.com"; //Sender address
    $mail->FromName = "Name to Display "; //The name that you'll see as Sender
    $mail->Host = "mail.youdomain.com"; //Your SMTP mail server
    $mail->Mailer = "smtp"; //Protocol to use
    $mail->AddAddress("SendToAddress@anydomain.com"); //The address you are sending mail to
    $mail->Subject = "Mail Subject"; //Subject of the mail
    $mail->Body = "Write your mail here"; //Body of the message


    You can also use account data if your server needs them for authentication:

    CODE
    $mail->SMTPAuth = "true";
    $mail->Username = "User";
    $mail->Password = "Pwd";

    if(!$mail->Send()){ //Check for result of sending mail
    echo "There was an error sending the message"; //Write an error message if mail isn't sent
    exit; //Exit the script without executing the rest of the code
    }



    To send HTML mails you just have to set:

    CODE
    $mail->IsHTML(true); //You can set this parameter with a variable to let the user choose his preferred method (TEXT or HTML)


    Then you have to choose if you want to link images through absolute path or embedding them into the mail.
    Using absolute path (Ex. http://www.yourdomain.com/images/header.gif) will let you easily modify each mail you sent until read cause each mail refers to your images. This result also in a smaller file to download and won't display images if the mail is read when not connected to the Internet. You can use the AltBody variable to set the message displayed if the user don't display the HTML body in the message:

    CODE
    $mail->AltBody = "You alternative text message"; //You have to use plain text, use \n to break line


    To embed images you must include some more commands in your script, using the AddEmbeddedImage function:

    CODE
    $path = "C:/myImages/myimage.gif"; //the address of the file. This could be any kind of file (sound, image, binary)
    $cid = "MyIdentifier"; //This is the id you must use in your HTML to include embedded files. You will not use id instead of src
    $name = "MyImage.gif"; //This will override your image name (OPTIONAL PARAMETER)
    $encoding = "base64"; //This encodes the message. Options for this are "8bit", "7bit", "binary", "base64", and "quoted-printable" (OPTIONAL PARAMETER)
    $type = "image/gif"; //This is MIME type of the embedded object. This parameter isn't optional if you are embedding an image. You should specify his MIME type as "image/gif" or "image/jpeg"
    $mail->AddEmbeddedImage($path,$cid,$name,$encoding,$type); //Add the specified file to the email body


    Repeat the code for each file you need to embed in the mail.
    PHPMailer has many other options you can set, like the confirm given back to a certain address:

    CODE
    $mail->ConfirmReadingTo = "myconfirmaddress@yourdomain.com"; //Enter the address where you want confirms go to.


    or set priority of a message:

    CODE
    $mail->Priority = 3; //Values are 1 = High , 3 = Normal, 5 = Low


    Further explaination of the commands are inside /phpdoc folder.

    Any comment or further examples can be addressed to info@hellord.com


    Hope to help
    Matteo Cappelloni

    --------------------


Riferimenti