Sending an EMail using Wordpress - php

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 = 'skchourasia#asselslutions.com';
$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('support#wordpressapi.com/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"

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.

Related

PHPmailer not sending mail when message field is empty but is not required?

http://nodeiterator.pl/
Why my php mailer script is not sending mail when message field is empty but is not required ? I get the message "please try again later". What am I missing ?
This is my script:
$msg = "";
use PHPMailer\PHPMailer\PHPMailer;
include_once "phpmailer/src/PHPMailer.php";
include_once "phpmailer/src/Exception.php";
if (isset($_POST['submit'])) {
$subject = $_POST['subject'];
$email = $_POST['email'];
$message = $_POST['message'];
if (isset($_FILES['attachment']['name']) && $_FILES['attachment']['name'] != "") {
$file = "attachment/" . basename($_FILES['attachment']['name']);
move_uploaded_file($_FILES['attachment']['tmp_name'], $file);
} else
$file = "";
$mail = new PHPMailer();
$mail->addAddress('piterdeja#gmail.com');
$mail->setFrom($email);
$mail->Subject = $subject;
$mail->isHTML(true);
$mail->Body = $message;
$mail->addAttachment($file);
if ($mail->send())
$msg = "Your email has been sent, thank you!";
else
$msg = "Please try again!";
}
I don't think PHPMail by default will let you send an email with an empty body, but you can just go:
$mail->AllowEmpty = true;
Check the error returned:
if(!$mail->Send()) {
echo 'Mailer Error: ' . $mail->ErrorInfo;
}
Normally phpmailer does not accept an empty body, you must force it using the attribute $mailer->AllowEmpty = true;

Sending and retriving an email using a php form

I am attempting to retrieve a sender's email, sender's name, mail subject, and mail message sent.
You will also see in the code i want to check if the name, email address, and message of a sender are not empty using empty()function. If one of them are empty, i want to display an error message in the document. Otherwise, call send_email() function.
Right now my output is displaying parts of my code. I need help with the formatting or maybe I am missing a step.
<?php
require_once ('c:/wamp64/www/Lab6/PHPMailerAutoload.php');
function sendEmail($fromEmail,$fromName,$subject, $message){
$mailer = new PHPMailer;
$mailer->isSMTP();
$mailer->Host = 'smtp.gmail.com';
$mailer->Port = 587;
$mailer->SMTPSecure = 'tls';
$mailer->SMTPAuth = true;
$mailer->Username= "geogumd#gmail.com";
$mailer->Password= "gis#umd2016";
$mailer->From= $fromEmail;
$mailer->FromName=$fromName . "(" . $fromEmail . ")";
$mail->IsHTML(true);
$mailer->Subject=$subject;
$mailer->Body=$message;
$mailer->addAddress('worthem.aleah#terpmail.umd.edu', 'Aleah');
$mailer->Sendmail($fromEmail,$fromName,$subject,$message);
if(!$mailer->send()) {
$h = "Mailer Error " . $mailer->ErrorInfo;
$m = "Mailer Error was not sent";
print "<h1>$h</h1>";
print "<pre>$m</pre>";
}else{
print "<h1>Mail Sent</h1>";
print "<p>Thank you. Your message has been sent successfully.</p>";
}
}
?>
<div id="content">
<?php
$user_input1 = $_POST['From'];
$user_input2 = $_POST['FromName'];
$user_input3 = $_POST['Subject'];
$user_input4 = $_POST['Body'];
if(!empty($user_input1) && is_string($user_input1)&&
(!empty($user_input2) && is_string($user_input2))&&
(!empty($user_input3) && is_string($user_input3))&&
(!empty($user_input4) && is_string($user_input4))
)
{
sendEmail($user_input1, $user_input2, $user_input3, $user_input4);
}
else{
print "<h4>Sorry</h4>";
print "<p>You didnt fill out the form completley. <a href='index.php?action=sendmail'>Try Again";
}
?>

PHP Mailer languages encoding issues: It accepts only English

I am using php mailer at my website contact form. When i receive a message in greek language, i don't receive the text as typed in the contact form. In class.phpmailer.php file line 59 the encoding is public $CharSet = 'iso-8859-1'; Is there a way to make my text appear correctly as typed in the contact form?
Languages comonly supported by ISO/IEC 8859-1 can be found here
I have also tried german and albanian languages but i also have the same problem. I can only receive english, if the user types another language on some words i receive "chinese".
I get this message:
The code:
<?php
require_once('phpmailer/class.phpmailer.php');
// if(isset($_POST['g-recaptcha-response'])){
if (empty($_POST['Email'])) {
$_POST['Email'] = "-";
}
if (empty($_POST['Name'])) {
$_POST['Name'] = "-";
}
if (empty($_POST['Subject'])) {
$_POST['Subject'] = " ";
}
if (empty($_POST['message'])) {
$_POST['message'] = "-";
}
$mymail = smtpmailer("example#gmail.com", $_POST['Email'], $_POST['Name'],
$_POST['Subject'], $_POST['message']);
function smtpmailer($to, $from, $from_name, $subject, $body)
{
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Debugoutput = 'html';
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->Username = 'example#gmail.com';
$mail->Password = 'pass';
$mail->SetFrom($from, $from_name);
$mail->Subject = " Contact form ~Name: $from_name ~ subject: $subject ";
$mail->Body = " You have received a new message
from $from_name, here are the details:\n\n_____
___________________\n" . "\nDear $from_name,\n
Your enquiry had been received on " . date("D j F ") . "
\nINFORMATION SUBMITTED: " . "\n\nName: $from_name\n\nEmail: $from
\nSubject: $subject\n\nMessage: $body \n\nTo:
$to\nDate: " . date("d/m/y") . "\nWebsite: " . "\n____________
__________________"; //end body
$mail->AddAddress($to);
//send the message, check for errors
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Well done $from_name, your message has been sent!\n
We will reply to the following email: $from" . "\nYour Message: $body";
}
} //end function smtpmailer
//}
?>
In your example output, the char count amplification suggests that you're receiving data from your form in UTF-8, but are then telling PHPMailer (by default) that it's ISO-8859-1, which results in the kind of corruption you're seeing.
You should be using UTF-8 everywhere. In PHPMailer you do it like this:
$mail->CharSet = 'UTF-8';
Then you need to be sure that every step of your processing supports UTF-8 as well.

Two versions of same email to two different recipients using class.phpmailer.php

I'm trying to send two versions of the same email to two recipients using phpMailer
Depending on the email I need to send one version or another.
At the moment I'm able to send only the same version to both email address
<?php
require_once('class.phpmailer.php');
if(isset($_POST['Submit'])) {
$fname = $_POST['fname'];
$maileremail = $_POST['maileremail'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$mail = new PHPMailer();
$text = 'The person that contacted you is '.$fname.' E-mail: '.$maileremail.' Subject: '.$subject.' Message: '.$message.' :';
$body = eregi_replace("[\]",'',$text);
$text2 = 'The person that contacted you is '.$fname.' E-mail: REMOVED - To get email address you must login Subject: '.$subject.' Message: '.$message.' :';
$body2 = eregi_replace("[\]",'',$text2);
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "xxxxx.xxxxx.xxx"; // SMTP server
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->SMTPKeepAlive = true; // SMTP connection will not close after each email sent
$mail->Host = "xxxxx.xxxxx.xxx"; // sets the SMTP server
$mail->Port = XXXXXXXX; // set the SMTP port for the GMAIL server
$mail->Username = "xxxxx#xxxxx.xxx"; // SMTP account username
$mail->Password = "XXXXXXXX"; // SMTP account password
$mail->SetFrom('xxxxx#xxxxx.xxx', 'XXXXXXXX');
$mail->Subject = $subject;
$add = array("first#email.xxx", "second#email.xxx");
foreach($add as $address) {
if($address == "first#email.xxx") {
$mail->AddAddress($address, "xxxxxxxxx");
$mail->Body = $body;
}
elseif($address == "second#email.xxx") {
$mail->AddAddress($address, "xxxxxxxxx");
$mail->Body = $body2;
}
}
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "<h3><b>Message sent!</b></h3>";
}}?>
If you're looking to do all the setup once for minimal repetition, simply clone your $mail object in your foreach($add) loop:
foreach ( $add as $address ) {
$current = clone $mail;
if ( $address == 'first#mail.com' ) {
$current->AddAddress($address, "xxxxxxxxx");
$current->Body = $body;
$current->send();
} elseif....
This creates a separate clone of your $mail object for every loop, allowing you to add different email bodies, subjects, etc. without having to rewrite all connection settings.
I think you want to seach the $maileremail inside $add and send the desired email.
$add = array("first#email.xxx", "second#email.xxx");
$mail->AddAddress($maileremail, "xxxxxxxxx");
if($maileremail === $add[0])
$mail->Body = $body;
if($maileremail === $add[1])
$mail->Body = $body2;
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "<h3><b>Message sent!</b></h3>";
}
EDITED:
I misunderstood the question. Brian is right.

PHPMailer emails not sending but there are no errors

I am using PHPMailer and using this PHP Code to send emails:
$email = new PHPMailer();
while($result2=mysql_fetch_array($rs2))
{
$email->AddAttachment( $result2["attachment"] , basename($result2["attachment"]) );
}
$email->From = 'Integra Digital';
$email->FromName = $result["emailfrom"];
$email->Subject = $result["subject"];
$email->Body = $result["message"];
$email->AddAddress($result["emailto"]);
$email->IsHTML(true);
if(!$email->Send())
{
echo "<strong>Mailer Error: </strong>" . $email->ErrorInfo;
}
else
{
echo '<strong>Email sent to: </strong>' .implode(',',$emails_list). '<br/ >';
}
$result["emailto"] is equal to a valid email address but i am not receiving the emails
any ideas?
looks like it wanted the AddAttachment part after everything else and just before the Send function

Categories