Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending an EMail using Wordpress

I have tried so many different approaches, but cannot successfully send an EMail through SMTP in PHP using the mail() function.

 <?php
require_once ABSPATH . WPINC . '/class-phpmailer.php';
require_once ABSPATH . WPINC . '/class-smtp.php';
$phpmailer = new PHPMailer();
$phpmailer->SMTPAuth = true;
$phpmailer->Username = '[email protected]';
$phpmailer->Password = 'password01';
 
$phpmailer->IsSMTP(); // telling the class to use SMTP
$phpmailer->Host       = "mail.asselsolutions.com"; // SMTP server
$phpmailer->FromName   = $_POST[your_email];
$phpmailer->Subject    = $_POST[your_subject];
$phpmailer->Body       = $_POST[your_message];                      //HTML Body
$phpmailer->AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$phpmailer->WordWrap   = 50; // set word wrap
$phpmailer->MsgHTML($_POST[your_message]);
$phpmailer->AddAddress('[email protected]/files/', 'Wordpress support');
//$phpmailer->AddAttachment("images/phpmailer.gif");             // attachment
if(!$phpmailer->Send()) {
 echo "Mailer Error: " . $phpmailer->ErrorInfo;
} else {
 echo "Message sent!"; 
}
$to = $_REQUEST['to'];
$subject = $_REQUEST['subject'];
$message =  $_REQUEST['message'];
$from = $_REQUEST['from'];
$headers = "From:" . $from;

$mail = mail($to,$subject,$message,$headers);

echo "Mail Sent.";
 ?>

What am I doing wrong? I am getting the following error:

Parse error: syntax error, unexpected T_VARIABLE in C:\xampp\htdocs\wp-vtr\wp-content\themes\twentyeleven\content.php on line 8

 $phpmailer->IsSMTP(); // telling the class to use SMTP"
like image 207
user1811549 Avatar asked Nov 12 '22 18:11

user1811549


1 Answers

This:

$phpmailer->FromName   = $_POST[your_email];
$phpmailer->Subject    = $_POST[your_subject];
$phpmailer->Body       = $_POST[your_message]; 

$phpmailer->MsgHTML($_POST[your_message]);

should be this:

$phpmailer->FromName   = $_POST['your_email'];
$phpmailer->Subject    = $_POST['your_subject'];
$phpmailer->Body       = $_POST['your_message']; 

$phpmailer->MsgHTML($_POST['your_message']);

Anyway, it seems you are trying to send an e-mail both via PHPMailer class and mail() native PHP function. You may be just testing but I am not really sure what are you trying to do.

like image 107
jmic Avatar answered Nov 15 '22 09:11

jmic