3

PHPMailer — Installing on a Shared server

 2 years ago
source link: https://help.dreamhost.com/hc/en-us/articles/360031174411-PHPMailer-Installing-on-a-Shared-server
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Basic code example for email hosted at DreamHost

Insert the following code into a PHP file.

dh-kb-important-icon.svg

You only need to update the code in bold.

username — Make sure to change the 'username' field to your actual shell username. You can also view this by running the following command.

[server]$ echo $USER
username

Host — If you're sending from a DreamHost address, you should only use smtp.dreamhost.com. If you are using a Gmail address, use smtp.gmail.com.

Username and setFrom must both be an email on the domain you're sending from.

<?php
// Import PHPMailer classes into the global namespace
// These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require '/home/username/PHPMailer/src/Exception.php';
require '/home/username/PHPMailer/src/PHPMailer.php';
require '/home/username/PHPMailer/src/SMTP.php';

$mail = new PHPMailer(true);                              // Passing `true` enables exceptions
try {
    //Server settings
    $mail->SMTPDebug = 2;                                 // Enable verbose debug output
    $mail->isSMTP();                                      // Set mailer to use SMTP
    $mail->Host = 'smtp.dreamhost.com';                  // Specify main and backup SMTP servers
    $mail->SMTPAuth = true;                               // Enable SMTP authentication
    $mail->Username = '[email protected]';             // SMTP username
    $mail->Password = 'secret';                           // SMTP password
    $mail->SMTPSecure = 'ssl';                            // Enable SSL encryption, TLS also accepted with port 465
    $mail->Port = 465;                                    // TCP port to connect to

    //Recipients
    $mail->setFrom('[email protected]', 'Mailer');          //This is the email your form sends From
    $mail->addAddress('[email protected]', 'Joe User'); // Add a recipient address
    //$mail->addAddress('[email protected]');               // Name is optional
    //$mail->addReplyTo('[email protected]', 'Information');
    //$mail->addCC('[email protected]');
    //$mail->addBCC('[email protected]');

    //Attachments
    //$mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
    //$mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name

    //Content
    $mail->isHTML(true);                                  // Set email format to HTML
    $mail->Subject = 'Subject line goes here';
    $mail->Body    = 'Body text goes here';
    //$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

    $mail->send();
    echo 'Message has been sent';
} catch (Exception $e) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
}
?>

It is recommended to use port 465 with SSL as shown above.

You could optionally use port 587 with TLS.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK