Constructor.
public __construct ( boolean $exceptions = null ) | ||
$exceptions | boolean | Should we throw external exceptions? |
// Include PHPMailer Autoload file require 'vendor/autoload.php'; // Create a new PHPMailer instance $mail = new PHPMailer\PHPMailer\PHPMailer(); // Set the basic parameters of the email message $mail->IsSMTP(); $mail->CharSet = 'UTF-8'; $mail->SMTPDebug = 0; $mail->SMTPAuth = true; $mail->SMTPSecure = 'ssl'; $mail->Host = 'smtp.gmail.com'; $mail->Port = 465; $mail->Username = 'your_email_address@gmail.com'; $mail->Password = 'your_email_password'; // Set the Recipient $mail->SetFrom('noreply@example.com', 'No-Reply'); $mail->AddAddress('recipient@example.com', 'John Doe'); $mail->AddCC('cc_recipient@example.com', 'CC Recipient'); $mail->AddBCC('bcc_recipient@example.com', 'BCC Recipient'); // Set message subject and body $mail->Subject = 'Test Email'; $mail->Body = 'This is a test email from PHPMailer.'; // Add attachment $mail->AddAttachment('/path/to/file.pdf'); // Send the email and check its status if(!$mail->send()) { echo 'Message could not be sent.'; echo 'Mailer Error: ' . $mail->ErrorInfo; } else { echo 'Message has been sent'; }In this example, we have used the PHPMailer __construct method to create a new PHPMailer instance. We then set the basic parameters of the email message, including the SMTP host, port, username, and password. We also set the recipient(s) and message subject and body. Finally, we added an attachment to the email and sent it. The PHPMailer package library used in this example is PHPMailer version 6, which can be installed via Composer.
public __construct ( boolean $exceptions = null ) | ||
$exceptions | boolean | Should we throw external exceptions? |