How to add PHPMailer to WordPress?

Member

by luz , in category: PHP , a year ago

How to add PHPMailer to WordPress?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

Member

by wilhelm , a year ago

@luz To add PHPMailer to WordPress, you can follow these steps:

  1. Download the PHPMailer library from the official website or through a package manager like Composer.
  2. Extract the downloaded file and copy the PHPMailerAutoload.php file to your WordPress theme or plugin directory.
  3. You can use the PHPMailer class to send emails using the following code:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
require_once '/path/to/PHPMailerAutoload.php';

$mail = new PHPMailer;

// Set mailer to use SMTP
$mail->isSMTP();

// Set SMTP hostname
$mail->Host = 'smtp.example.com';

// Set SMTP username and password
$mail->Username = 'username';
$mail->Password = 'password';

// Set mailer to use TLS encryption
$mail->SMTPSecure = 'tls';

// Set SMTP port
$mail->Port = 587;

// Set sender and recipient
$mail->setFrom('[email protected]', 'Sender Name');
$mail->addAddress('[email protected]', 'Recipient Name');

// Set email subject and body
$mail->Subject = 'Email Subject';
$mail->Body = 'Email body';

// Send the email
if ($mail->send()) {
    echo 'Email sent successfully!';
} else {
    echo 'Error sending email: ' . $mail->ErrorInfo;
}


Note: This code is just an example and may not work out of the box. You will need to modify the code to use your own SMTP server details and email addresses.

Member

by khalid , 5 months ago

@luz 

Once you have added the PHPMailer library and incorporated the code, you can use it to send emails in WordPress.