PHPMailer default configuration SMTP

16,538

Solution 1

Create a function, and include / use it.

function create_phpmailer() {
  $mail             = new PHPMailer();
  $mail->IsSMTP(); // telling the class to use SMTP
  $mail->SMTPAuth   = true;                  // enable SMTP authentication
  $mail->Host       = "mail.yourdomain.com"; // sets the SMTP server
  $mail->Username   = "yourname@yourdomain"; // SMTP account username
  $mail->Password   = "yourpassword";        // SMTP account password
  return $mail;
}

And call create_phpmailer() to create a new PHPMailer object.

Or you can derive your own subclass, which sets the parameters:

class MyMailer extends PHPMailer {
  public function __construct() {
    parent::__construct();
    $this->IsSMTP(); // telling the class to use SMTP
    $this->SMTPAuth   = true;                  // enable SMTP authentication
    $this->Host       = "mail.yourdomain.com"; // sets the SMTP server
    $this->Username   = "yourname@yourdomain"; // SMTP account username
    $this->Password   = "yourpassword";        // SMTP account password
  }
}

and use new MyMailer().

Solution 2

Can I not just edit the class.phpmailer.php file?

It's best not to edit class files themselves because it makes the code harder to maintain.

Share:
16,538
Gilly
Author by

Gilly

Software engineer

Updated on July 18, 2022

Comments

  • Gilly
    Gilly almost 2 years

    I know how to use SMTP with PHPMailer:

    $mail             = new PHPMailer();
    $mail->IsSMTP(); // telling the class to use SMTP
    $mail->SMTPAuth   = true;                  // enable SMTP authentication
    $mail->Host       = "mail.yourdomain.com"; // sets the SMTP server
    $mail->Username   = "yourname@yourdomain"; // SMTP account username
    $mail->Password   = "yourpassword";        // SMTP account password
    

    And it works fine. But my question is:

    How can I configure PHPMailer to use these settings on default, so that I do not have to specify them each time I want to send mail?

  • Kolja
    Kolja about 9 years
    Can I not just edit the class.phpmailer.php file? By default it (at least the current version) starts with: class PHPMailer { public $Version = '5.2.9'; public $Priority = 3; public $CharSet = 'iso-8859-1'; public $ContentType = 'text/plain'; public $Encoding = '8bit'; public $ErrorInfo = ''; public $From = 'root@localhost'; public $FromName = 'Root User'; ... so what if I change the value of $From to, let's say, [email protected]
  • Steven
    Steven about 7 years