PHPMailer body message empty error - phpmailer

using tutorials I pasted and filled the following PHPMailer php file and made the following html form. However after send the error appears "Message could not be sent.Mailer Error: Message body empty". Please help.
<?php
require 'phpmailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
//$mail->SMTPDebug = 3;
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'xxx#gmail.com';
$mail->Password = 'xxx';
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
$mail->setFrom('from#example.com', 'Mailer');
$mail->addAddress('xxx#gmail.com', 'Joe User');
$mail->addReplyTo('info#example.com', 'Information');
$mail->addAttachment('/var/tmp/file.tar.gz');
$mail->addAttachment('/tmp/image.jpg', 'new.jpg');
$mail->isHTML(true);
$mail->Subject = 'Here is the subject';
$mail->From = $email;
$mail->FromName = $name;
$mail->Body = $message;
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
?>
and here is the form
<form name="contactform" method="post" action="email.php">
<div class="input-text-container clear">
<input type="text" name="name" id="name" placeholder="Your Name"
class="input-text input-text-left">
<input type="text" name="email" id="email" placeholder="Your E-mail"
class="input-text input-text-right">
</div>
<textarea type="text" name="message" id="message" placeholder="Your
Message" class="textarea"></textarea>
<button type="submit" value="submit Form" class="submit">SUBMIT</button>
</form>

Your script seems to rely on request parameters being available as global variables. But this is a very uncommon (and insecure) practice.
Use $_POST['message'] instead $message and the message body should contain data from the form. Do the same for the other parameters.

Related

Including multiple form fields via Nodemailer with Express and Node.js

I am trying to receive an e-mail everytime the user submits my contact form.
The form has the following fields:
- email
- subject
- name
- track
- number
- the message
However when the e-mail is sent I can only get whichever field I input into the "text" field under mail options. It won't work if I try to turn it into an array or object because it says the chunk needs to be a string.
How can I send more form fields than whatever I input into text? Here's my code:
Nodemailer POST Route :
app.post ("/contact" , urlencodedParser, function(req, res){
const transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: 'theemail',
pass: 'emailpassword' // naturally, replace both with your real credentials or an application-specific password
}
});
var subject = req.body.subject;
var email = req.body.email;
var data = [
req.body.name,
req.body.track,
req.body.number,
req.body.message,
]
const mailOptions = {
from: req.body.email,
subject: req.body.subject,
to: "theemail",
text: req.body.message,
};
transporter.sendMail(mailOptions, function(error, info){
if (error) {
console.log(error);
} else {
console.log('Email sent: ' + info.response);
}
});
res.render("../EN/views/contact-success");
});
Form HTML
<div class="fade-in serviceform text-center container form-group">
<form method ="POST" action = "/contact">
<p class=" text-center">Touring | Recording | Online sessions | Any other question</p>
<input type="email" name="email" class="contactform" placeholder=" E-mail" required>
<input type="text" name="subject" class="contactform" placeholder=" Subject" required>
<input type="text" name="name" class="contactform" placeholder=" First and last name" required>
<input type="text" name="track" class="contactform" placeholder=" Track Name" required>
<input type="number" name="number" class="contactform" placeholder=" +351919999999" required>
<textarea class="contactformtext" name="message" placeholder=" Write your message here." rows="3" required></textarea>
<div class="text-center">
<button type="submit" class="btn btn-outline-danger">Contact</button>
</div>
</form>
<div class ="text-center">
<a class="anchor" href="https://www.facebook.com/"><i class="fab fa-facebook-square"></i></a><a class="anchor" href="https://www.instagram.com//"><i class="fab fa-instagram"></i></a>
<p>Facebook | | Instagram </p>
</div>
```
just use the backticks it will considered itself as the string

How to install phpMailer in shared hosting environment?

How to install phpMailer in a shared hosting environment?
Seen this How to install phpMailer in a shared hosting environment? but I don't understand what does "include the main file with this line:" means and the next part too "After that, you will need an external SMTP account, like gmail.com for example. Here is a working example of PHPMailer with GMAIL:"
Thanks in advance
Athlios
This is the send_form.php
<?php
$formid = $_POST['contactform'];
$email_to = "info#a-wd.eu";
$fullname = $_POST['fullname']; // required
$email_from = $_POST['email']; // required
$subject = $_POST['subject']; // required
//$subjectselect = $_POST('subject').value();
$message = $_POST['message']; // required
echo($email_from);
$email_message = "Submission details below.\n\n";
$email_message .= "Fullname: ".clean_string($fullname)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Whats this about: ".clean_string($subject)."\n";
$email_message .= "Message: ".clean_string($message)."\n";
$email_message = wordwrap($email_message, 70, "\r\n");
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($email_to, $email_subject, $email_message,$headers) or die("Error!");
echo "Thank you for contacting us. We will be in touch with you very soon. \r\n";
?>
This is the page
<div class="container-fluid">
<div class="row">
<div class="col-12">
<div class="container contact-form-container">
<div class="row">
<div class="col-md-12">
<form action="send_form.php" id="contactform" name="contactform" method="post" data-parsley-validate>
<div class="row">
<!-- <span class="required-key">Fields marked with a <span class="label-required">*</span> are required.</span> -->
<div class="form-group col-lg-12 col-12">
<label for="fullname">Name <span>(Required)</span>:</label>
<input type="text" name="fullname" data-parsley-trigger="focusin focusout" required data-parsley-required="true" data-parsley-errors-messages-disabled />
</div>
<div class="form-group col-lg-12 col-12">
<label for="email">Email <span>(Required)</span>:</label>
<input type="email" data-parsley-type="email" name="email" data-parsley-trigger="focusin focusout" required data-parsley-required="true" data-parsley-errors-messages-disabled />
</div>
<div class="form-group col-lg-12 col-12">
<label for="subject">Subject <span>(Required)</span>:</label>
<select name="subject" required data-parsley-required="true" data-parsley-errors-messages-disabled>
<option value="question">General Question</option>
<option value="quote">Request a Quote</option>
<option value="sponsorship">Sponsorship</option>
<option value="other">Other</option>
</select>
</div>
<div class="form-group col-lg-12 col-12">
<label for="message">Message <span>(Required)</span>:</label>
<textarea name="message" data-parsley-trigger="focusin focusout" data-parsley-minlength="20" data-parsley-maxlength="1000" data-parsley-validation-threshold="10" data-parsley-minlength-message="Minimum message length is 20 characters" data-parsley-maxlength-message="Maximum message length is 1000 characters" data-parsley-required="true"></textarea>
</div>
<div class="form-group col-lg-12 col-12">
<label for="message">Captcha <span>(Required)</span>:</label>
<script src='https://www.google.com/recaptcha/api.js'></script>
<div class="g-recaptcha" data-sitekey="6LeAAUkUAAAAAJeW7fjroLKFkYtETHvXGgflK49u"></div>
</div>
<div class="form-group col-lg-12 col-12">
<button class="btn-contact" name="send" type="submit">Send Message <i class="fa fa-paper-plane" aria-hidden="true"></i></button>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
Go to the PHPMailer github, click the green "Clone or Download" button, and click "Download ZIP". On your local computer, unarchive the ZIP file and upload the PHPMailer-master folder to your shared server's public_html directory.
Now, wherever you want to use PHPMailer, include the parts you need:
<?php
require '/path/to/PHPMailer-master/src/PHPMailer.php'; // Only file you REALLY need
require '/path/to/PHPMailer-master/src/Exception.php'; // If you want to debug
?>
You don't need an external SMTP account for this to work, as noted in the PHPMailer documentation. So in a script where you're sending out an email, your code should look something like this:
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'PHPMailer-master/src/PHPMailer.php'; // Only file you REALLY need
require 'PHPMailer-master/src/Exception.php'; // If you want to debug
$mail = new PHPMailer(true); // Passing `true` enables exceptions
try {
//Recipients
$mail->setFrom('from#example.com', 'Mailer');
$mail->addAddress('joe#example.net', 'Joe User'); // Add a recipient
$mail->addAddress('ellen#example.com'); // Name is optional
$mail->addReplyTo('info#example.com', 'Information');
$mail->addCC('cc#example.com');
$mail->addBCC('bcc#example.com');
//Attachments
$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
//Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}
?>
This should be all you need to get started using PHPMailer on a shared server. Check out the project's README and look at some examples for a better understanding of all the awesomeness you get with this library.
Update for OP's code
Put the include code at the top of send_post.php:
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'PHPMailer-master/src/PHPMailer.php'; // Only file you REALLY need
require 'PHPMailer-master/src/Exception.php'; // If you want to debug
// Form details
$formid = $_POST['contactform'];
$email_to = "info#a-wd.eu";
$fullname = $_POST['fullname']; // required
$email_from = $_POST['email']; // required
$subject = $_POST['subject']; // required
$message = $_POST['message']; // required
$email_message = "Submission details below.\n\n";
$email_message .= "Fullname: ".clean_string($fullname)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Whats this about: ".clean_string($subject)."\n";
$email_message .= "Message: ".clean_string($message)."\n";
$email_message = wordwrap($email_message, 70, "\r\n");
// No need to set headers here
// Replace the mail() function with PHPMailer
$mail = new PHPMailer(true); // Passing `true` enables exceptions
try {
//Recipients
$mail->setFrom($email_from, 'From Name');
$mail->addAddress($email_to, $fullname); // Add the recipient
//Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $subject;
$mail->Body = $email_message;
$mail->send();
echo "Thank you for contacting us. We will be in touch with you very soon. \r\n";
} catch (Exception $e) {
echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo;
}

PHPmailer Contact form

I need a little help here. I'm using PHPmailer for my contact form.
When i test PHPmailer, it works perfect, i receive the email send from the script, But when I try to connect it to the contact form.. I don't receive any emails (not even in junk folder) or errors.
What am I doing wrong?
<?php
ini_set('display_errors', true);
error_reporting(1);
if(isset($_POST['submit1']))
{
$message=
'Full Name: '.$_POST['name'].'<br />
Subject: '.$_POST['subject'].'<br />
from: '.$_POST['email'].'<br />
Comments: '.$_POST['message'].'
';
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
// Set up SMTP
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->SMTPSecure = "ssl";
$mail->Host = "mail.mys4e.com";
$mail->Port = 587;
$mail->Encoding = '7bit';
// Authentication
$mail->Username = "neal#mys4e.com";
$mail->Password = "********";
// Compose
$mail->SetFrom($_POST['email'], $_POST['name']);
$mail->AddReplyTo($_POST['email'], $_POST['name']);
$mail->Subject = "New Contact Form Enquiry";
$mail->MsgHTML($message);
// Send To
$mail->AddAddress("neal#mys4e.com", "Neal Fourie");
$result = $mail->Send();
$message = $result ? 'Successfully Sent!' : 'Sending Failed!';
unset($mail);
}
?>
HTML
<form id="main-contact-form" name="contact-form" method="post" action="sendemail.php">
<div class="form-group">
<input type="text" name="name" class="form-control" placeholder="Name" required>
</div>
<div class="form-group">
<input type="email" name="email" class="form-control" placeholder="Email" required>
</div>
<div class="form-group">
<input type="text" name="subject" class="form-control" placeholder="Subject" required>
</div>
<div class="form-group">
<textarea name="message" class="form-control" rows="8" placeholder="Message" required></textarea>
</div>
<button type="submit" name="submit1" class="btn btn-primary">Send Message</button>
</form>
Try this it is working fine for me
ini_set('display_errors', true);
require_once "PHPMailerAutoload.php";
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$Comments = $_POST['message'];
$mail = new PHPMailer;
//Enable SMTP debugging.
$mail->SMTPDebug = 3;
//Set PHPMailer to use SMTP.
$mail->isSMTP();
//Set SMTP host name
$mail->Host = "smtp.gmail.com";
//Set this to true if SMTP host requires authentication to send email
$mail->SMTPAuth = true;
//Provide username and password
$mail->Username = "ahmedkhangaditek#gmail.com";
$mail->Password = "yourpassword";
//If SMTP requires TLS encryption then set it
$mail->SMTPSecure = "tls";
//Set TCP port to connect to
$mail->Port = 587;
$mail->From = "ahmedkhangaditek#gmail.com";
$mail->FromName = "Ahmed Khan";
$mail->addAddress($email, $name);
$mail->isHTML(true);
$mail->Subject = $subject;
$mail->Body = $Comments;
$mail->AltBody = $Comments;
if(!$mail->send())
{
echo "Mailer Error: " . $mail->ErrorInfo;
}
else
{
echo "Message has been sent successfully";
}

Sending email with phpmailer, receiving a SMTP error

On my website I have a simple contact form but I can't figure out why I can't send email.
HTML:
<form id="contactForm" method="post" action="js/sendmail.php" >
<fieldset>
<p><label for="name" class="label">Name*:</label>
<input type="text" name="name" id="Text1" class="input required" />
</p>
<p><label for="email" class="label">Email*: </label>
<input type="email" name="email" id="contact_email" class="input email required" />
<span class="input_feedback"></span>
</p>
<p>
<label class="label">Your message*: </label>
<textarea class="textarea required" name="message" rows="10" cols="50" ></textarea>
</p>
<p class="submitButtons">
<input type="submit" value="Send email" id="senEmail" class="button"/>
</p>
</fieldset>
</form>
PHP:
<?php
include('class.phpmailer.php');
include("class.smtp.php");
session_set_cookie_params('3600'); // 1 hour
session_start();
$mail->SMTPSecure = "ssl";
$mail->SMTPKeepAlive = true;
$mail->Mailer = "smtp";
$mail = new PHPMailer(); // create a new object
$mail->SMTPDebug =2;
$mail->IsSMTP(); // send via SMTP
$mail ->Host = 'mrvnet.kundenserver.de';
$mail->Port= 465;
$mail->SMTPAuth = true;
$mail->Username = 'info#brightongatwicktransfer.com';
$mail->Password = '******';
$mail->SetFrom('myName#yahoo.com');
$mail->Subject = "Test";
$mail->Body = "Something";
$mail->AddAddress('info#brightongatwicktransfer.com');
if(!$mail->Send())
{ echo "Mailer Error: " . $mail->ErrorInfo;}
else
{ echo "Message has been sent";}
?>
I tried with 'ssl' and 'tls' but no results.
Host company said the host is correct and asked me to try with 587 and 25 ports but I always received the same error:
2013-12-04 11:40:00 SMTP ERROR: Failed to connect to server: A connection attempt
failed because the connected party did not properly respond after a period of time, or
established connection failed because connected host has failed to respond. (10060)
SMTP connect() failed. Mailer Error: SMTP connect() failed.
Not a PHP coder but here:
$mail->SMTPSecure = "ssl";
$mail->SMTPKeepAlive = true;
$mail->Mailer = "smtp";
$mail = new PHPMailer(); // create a new object
You're allocating the object after you use it, this is invalid, you should allocate it first:
$mail = new PHPMailer(); // create a new object
$mail->SMTPSecure = "ssl";
$mail->SMTPKeepAlive = true;
$mail->Mailer = "smtp";

Variable "to" address in PHP Mailer

I am trying to set the "to" address for an email sent via an html form utilizing phpmailer. The end goal is to utilize a droplist to let the user select which person they are contacting depending on their needs. However, as I don't know much about PHP I'm going step by step and am already stuck.
I'm using the example that came with the site I purchased, but trying to modify it to include a to field. Right now I'm simply trying to get a new variable to show up but have not succeeded. This is the HTML form I have in place right now:
<form name="contact" method="post" action="phpmailer.php" class="af-form" id="af-form">
<div class="af-outer af-required">
<div class="af-inner">
<label for="toemail" id="toemail_label">To Whom:</label>
<input type="text" name="toemail" id="toemail" size="30" value="" class="text-input span8"/>
<label class="error" for="toemail" id="toemail_error">To field is required.</label>
</div>
</div>
<div class="af-outer af-required">
<div class="af-inner">
<label for="name" id="name_label">Your Name:</label>
<input type="text" name="name" id="name" size="30" value="" class="text-input span8"/>
<label class="error" for="name" id="name_error">Name is required.</label>
</div>
</div>
<div class="af-outer af-required">
<div class="af-inner">
<label for="email" id="email_label">Your Email:</label>
<input type="text" name="email" id="email" size="30" value="" class="text-input span8"/>
<label class="error" for="email" id="email_error">Email is required.</label>
</div>
</div>
<div class="af-outer af-required">
<div class="af-inner">
<label for="input-message" id="message_label">Your Message:</label>
<textarea name="message" id="input-message" cols="30" class="text-input span8"></textarea>
<label class="error" for="input-message" id="message_error">Message is required.</label>
</div>
</div>
<div class="af-outer af-required">
<div class="af-inner">
<input type="submit" name="submit" class="form-button btn btn-large" id="submit_btn" value="Send Message!" />
</div>
</div>
The name, email, and message fields get picked up fine and populate in the email I recieve. However, the "toemail" field doesn't show up. Here is the process.php I am using for phpmailer.
<?php
if ((isset($_POST['toemail'])) && (strlen(trim($_POST['toemail'])) > 0)) {
$toemail = stripslashes(strip_tags($_POST['toemail']));
} else {$toemail = 'No to address entered';}
if ((isset($_POST['name'])) && (strlen(trim($_POST['name'])) > 0)) {
$name = stripslashes(strip_tags($_POST['name']));
} else {$name = 'No name entered';}
if ((isset($_POST['email'])) && (strlen(trim($_POST['email'])) > 0)) {
$email = stripslashes(strip_tags($_POST['email']));
} else {$email = 'No email entered';}
if ((isset($_POST['message'])) && (strlen(trim($_POST['message'])) > 0)) {
$message = stripslashes(strip_tags($_POST['message']));
} else {$message = 'No phone entered';}
ob_start();d
?>
<html>
<head>
<style type="text/css">
</style>
</head>
<body>
<table width="550" border="1" cellspacing="2" cellpadding="2">
<tr bgcolor="#eeffee">
<td>To Whom:</td>
<td><?=$toemail;?></td>
</tr>
<tr bgcolor="#eeffee">
<td>Name</td>
<td><?=$name;?></td>
</tr>
<tr bgcolor="#eeeeff">
<td>Email</td>
<td><?=$email;?></td>
</tr>
<tr bgcolor="#eeffee">
<td>Message</td>
<td><?=$message;?></td>
</tr>
</table>
</body>
</html>
<?
$body = ob_get_contents();
$to = 'personalemailtousefortesting.com';
$email = 'email#example.com';
$fromaddress = "you#example.com";
$fromname = "Online Contact";
require("phpmailer.php");
$mail = new PHPMailer();
$mail->From = "mail#yourdomain.com";
$mail->FromName = "Contact Form";
$mail->AddAddress("mypersonalemailfortestingpurposes#gmail.com","Name 1"); // addresses here
$mail->WordWrap = 50;
$mail->IsHTML(true);
$mail->Subject = "Contact form submitted";
$mail->Body = $body;
$mail->AltBody = "This is the text-only body";
if(!$mail->Send()) {
$recipient = 'mypersonalemailfortestingpurposes#gmail.com';
$subject = 'Contact form failed';
$content = $body;
mail($recipient, $subject, $content, "From: mail#yourdomain.com\r\nReply-To: $email\r\nX-Mailer: DT_formmail");
exit;
}
?>
The email I get populates the name, email, and message fields fine, however, I get my fall back error message of 'No to address entered' for the "toemail" input.
This is issue number one. I'll post the phpmailer.php that I'm using if anyone needs to see it. I do not know if I need to define the variable in there first.
After I get that working, I need to make it work for a dropdown list so the user cannot just send the email to whomever they would like.
THEN, I need to set that as the actual send-to address. I do not know how to set the actual address for sending purposes. I would assume that I change
$mail->AddAddress("mypersonalemailfortestingpurposes#gmail.com","Name 1"); // addresses here
to something like...
$mail->AddAddress($toemail,"Name 1"); // addresses here
...but odds are that's not correct.
Any help you can provide would be wonderful.
If you need to see my phpmailer.php I can post that too.
Thanks again,
-Rocky Colt TumTum
Just incase you didn't find this, this is what was listed on the PHP Manual site.
<?php
// multiple recipients
$to = 'aidan#example.com' . ', '; // note the comma
$to .= 'wez#example.com';
// subject
$subject = 'Birthday Reminders for August';
// message
$message = '
<html>
<head>
<title>Birthday Reminders for August</title>
</head>
<body>
<p>Here are the birthdays upcoming in August!</p>
<table>
<tr>
<th>Person</th><th>Day</th><th>Month</th><th>Year</th>
</tr>
<tr>
<td>Joe</td><td>3rd</td><td>August</td><td>1970</td>
</tr>
<tr>
<td>Sally</td><td>17th</td><td>August</td><td>1973</td>
</tr>
</table>
</body>
</html>
';
// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Additional headers
$headers .= 'To: Mary <mary#example.com>, Kelly <kelly#example.com>' . "\r\n";
$headers .= 'From: Birthday Reminder <birthday#example.com>' . "\r\n";
$headers .= 'Cc: birthdayarchive#example.com' . "\r\n";
$headers .= 'Bcc: birthdaycheck#example.com' . "\r\n";
// Mail it
mail($to, $subject, $message, $headers);
?>

Resources