phpmailer add more contents to body of email - php

I am brand new to phpmailer.. i have made some progress by creating a simple form that allows users to send emails to our inbox:
<form id="contact-form" class="contact-form" method="post" action="send-mail.php">
<input type="text" name="name" id="name" placeholder="Name" tabindex="1">
<input type="text" name="subject" id="trade" placeholder="Your Trade" tabindex="3">
<input type="text" name="email" id="email" placeholder="Your Email" tabindex="2">
<input type="text" name="number" id="number" placeholder="Contact Number" tabindex="4">
<textarea id="message" rows="8" name="message" placeholder="Message" tabindex="5"></textarea>
<div class="grid-col one-whole">
<button type="submit">Send Your Message</button>
</div>
</form>
.php:
<?php
require('PHPFiles/PHPMailerAutoload.php');
require('PHPFiles/class.smtp.php');
require('PHPFiles/class.phpmailer.php');
$mail = new PHPMailer;
//$mail->SMTPDebug = 3;
$mail->IsSMTP();
$mail->Host = '74.208.5.2';
$mail->SMTPAuth = true;
$mail->Username = 'info#mydomain.com';
$mail->Password = '*******';
$mail->SMTPSecure = 'tls';
$mail->Port = 25; connect to
$mail->From = isset($_POST["email"]) ? $_POST["email"] : "";
$mail->FromName = isset($_POST["email"]) ? $_POST["email"] : "";
$mail->addAddress('info#mydomain.com');
$mail->Subject = isset($_POST["subject"]) ? $_POST["subject"] : "";
$mail->Body = str_replace("\n",'<br>', $_POST['message']);
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->isHTML(true);
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
At the moment, the Body of the email contains the contents of the textare field. How can the body contain this, but then also a line break, then, the contents of the number field?

You might want to try this. Make a separate variable for all the data that needs to be in the body of the message.
$message = "Name :- " . $_POST['name'] . "<br>" . " Number :- " . $_POST['number'];
$mail->Body = $message;

so doing it like:
$mail->Body = str_replace('\n','<br />', $_POST['message']);
or:
$mail->Body = str_replace("\\n","<br />", $_POST["message"]);
should fix the problem of string getting the "line breaks".
btw. it`s always good to:
var_dump($data);
die();
to see what are you getting at each step of a code

Related

PHPMailer not attaching files larger than 100KB - PHP

I am trying to set up a contact form which handles multiple file attachments. I am using PHPMailer and built the below script from the PHPMailer example for attaching multiple files.
The below script works great until attachments exceed 100KB. If a file is larger than 100KB, it is skipped when attaching. Only files smaller than 100KB are attached and sent.
I have seen this StackOverflow question which looked promising, but the values in my machine's php.ini file were all set to 32MB or higher.
I am using Mailgun as the SMTP server, and can see in the logs that the attachments that exceed 100KB aren't getting to Mailgun at all so it must have something to do with this script or my PHP environment.
Can anyone help me resolve this?
<?php
require 'PHPMailer/PHPMailerAutoload.php';
$host = 'smtp.mailgun.org';
$username = 'postmaster#domain.com';
$password = 'password';
$email_from = 'from#domain.com';
$email_to = 'to#domain.com';
$send = false;
$subject = "Quote Request from Website";
$name = addslashes(strip_tags($_POST['name']));
$email = addslashes(strip_tags($_POST['email']));
$project_type = addslashes(strip_tags($_POST['project_type']));
$message = addslashes(strip_tags($_POST['message']));
$htmlmessage = <<<MESSAGE
<html>
<head>
<title>$subject</title>
</head>
<body>
<p><strong>Name:</strong> $name</p>
<p><strong>Email:</strong> $email</p>
<p><strong>Project Type:</strong> $project_type</p>
<p><strong>Message:</strong> $message</p>
</body>
</html>
MESSAGE;
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->Username = $username;
$mail->Password = $password;
$mail->Host = $host;
$mail->Port = 587;
$mail->setFrom($email_from, $name);
$mail->addAddress($email_to);
$mail->addReplyTo($email, $name);
// $mail->addCC('cc#example.com');
// $mail->addBCC('bcc#example.com');
// Attach multiple files one by one
$total = count($_FILES['attachments']['tmp_name']);
echo $total;
for ($ct = 0; $ct < $total; $ct++)
{
$uploadfile = tempnam(sys_get_temp_dir(), sha1($_FILES['attachments']['name'][$ct]));
$filename = $_FILES['attachments']['name'][$ct];
if (move_uploaded_file($_FILES['attachments']['tmp_name'][$ct], $uploadfile)) {
echo $filename;
$mail->addAttachment($uploadfile, $filename);
} else {
$msg .= 'Failed to move file to ' . $uploadfile;
echo $msg;
}
// $name = $_FILES['attachments']['name'][$ct];
// $path = $_FILES['attachments']['tmp_name'][$ct];
// echo $name . ' - ' . $path . '<br>';
// $mail->addAttachment($path, $name);
}
$mail->isHTML(true);
$mail->Subject = $subject;
$mail->Body = $htmlmessage;
// $mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
The form:
<form action="contact/quote.php" method="post" id="quote-form" class="validate" role="form" enctype="multipart/form-data">
<label>Name</label>
<input type="text" name="name" id="name" required>
<label>Email</label>
<input type="email" name="email" id="email" required>
<label>Project Type</label>
<select name="project_type" id="project_type" required>
<option value="" selected>Please Select</option>
<option value="option1">Option 1</option>
<option value="option2">Option 2</option>
</select>
<label>Upload Files</label>
<input multiple="multiple" type="file" name="attachments[]" value="">
<label>Message</label>
<textarea name="message" id="message" rows="5" required></textarea>
<button type="submit" id="submit">Submit</button>
</form>
Any help would be much appreciated!
Thanks.
You're missing the MAX_FILE_SIZE option in your form, which won't help, and it defaults to 100k, which exactly matches what you're seeing. See the docs.
The send_file_upload example provided with PHPMailer shows how to set it correctly.
<input type="hidden" name="MAX_FILE_SIZE" value="1000000">

PHPMailer not sending from HTML form, returns blank page

I have a problem with PHPMailer on my website. In short, I want someone who wants to contact me to be able to send me an e-mail via contact form on my website. Said code looks like this:
index.html:
<form action="formularzeng.php" method="POST">
<div class="form-group">
<label for="InputEmail">E-mail address</label>
<input type="email" class="form-control" id="InputEmail" placeholder="E-mail" name="email">
</div>
<div class="form-group">
<label for="InputName">Full name</label>
<input type="name" class="form-control" id="InputName" placeholder="Full name" name="name">
</div>
<div class="form-group">
<label for="InputText">Message</label>
<textarea id="InputText" class="form-control" rows="5" placeholder="Message" name="message"></textarea>
</div>
<input type="submit" class="btn btn-default" value="Send" name="submit">
</form>
And formularzeng.php looks like this:
<!DOCTYPE html>
<?php
$to = "example#gmail.com";
$from = $_POST['email'];
$name = $_POST['name'];
$subject = "Contact form";
$message = $name . " " . " wrote:" . "\n\n" . $_POST['message'];
require 'phpmailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'smtp.gmail.com';
$mail->SMTPAuth = true;
$mail->Username = 'example#gmail.com';
$mail->Password = 'password';
$mail->SMTPSecure = 'ssl';
$mail->Port = 465;
$mail->setFrom('example#gmail.com', 'Mailer');
$mail->addAddress('$to',);
$mail->addReplyTo('$from', 'Mailer');
$mail->isHTML(true);
$mail->Subject = '$subject';
$mail->Body = '$message';
$mail->AltBody = '$message';
And a html section below with all the content saying "Thank you for your message. After uploading all the files on the server, filling the form and clicking on "Send" all I get is a white page, without any content. I'm totally green with php, but I want a working form on my portfolio website. Why is it not working?
Change all of your '$var_name' to "$var_name" to make PHP parse your strings....
For example, turn this :
$mail->Subject = '$subject';
$mail->Body = '$message';
$mail->AltBody = '$message';
Into :
$mail->Subject = "$subject";
$mail->Body = "$message";
$mail->AltBody = "$message";
Turn out it was one coma too much in line:
$mail->addAddress('$to',);
removing it fixed the problem, now it's
$mail->addAddress('$to');
And it works.

Jquery Ajax, PHPMailer doesn't send emails even if there's no error but works if i use stand alone page with fix values

I have this simple bootstrap form that i run on my localhost, it accepts name email and message and a custom captcha in a form of a math question; On submit i used jquery ajax to fetch the data to emailme.php that contains the phpmailer code.. My problem is, no email was sent even if there's no error and the network status is 200 and i can also see this:
http://localhost/emailme.php?name=myname&email=myemail#gmail.com&message=test
however it works if i just use the phpmailer stand alone code with fixed values. how can i make it work?
HTML:
<form id="emailme" method="post">
<div class="form-group">
<input type="text" class="form-control" name="name" id="name" placeholder="e.g. John Doe">
<span id="err_name" class="text-danger"></span>
</div>
<div class="form-group">
<input type="text" class="form-control" name="email" id="email" placeholder="e.g. example#domain.com">
<span id="err_email" class="text-danger"></span>
</div>
<div class="form-group">
<textarea name="message" id="message" cols="30" rows="10" class="form-control" placeholder="Your message"></textarea>
<span id="err_message" class="text-danger"></span>
</div>
<div class="form-group">
<label for="human">5 + 2 = ?</label>
<input type="text" id="human" name="human" class="form-control" placeholder="Your Answer">
<span id="err_human" class="text-danger"></span>
</div>
<input type="submit" class="btn btn-primary btn-sm" value="Send">
</form>
JQUERY:
$("#emailme").on('submit',function(e){
e.preventDefault();
var name = $("#name").val();
var email = $("#email").val();
var message = $("#message").val();
var human = $("#human").val();
var emailRegex = /(.+)#(.+)\.(com|edu|org|etc)$/g;
if(name!==''&&email!==''&&message!==''&&human!==''){
if(email.match(emailRegex)){
if(human==7){
$.ajax({
url:'emailme.php',
typ:'post',
data:{name:name,email:email,message:message},
success:function(response){
console.log(response);
$("#mail-status").html(response);
$("#emailme")[0].reset();
},
error:function(XMLHttpRequest,textStatus,errorThrown){
console.log(textStatus+errorThrown);
}
});
}
else{
$("#mail-status").html("unable to submit form, you're not human");
}
}
else{
$("#err_email").append("Invalid email format");
}
}
else if(name==''){
$("#err_name").append("Name cannot be empty");
}
else if(email==''){
$("#err_email").append("Email cannot be empty");
}
else if(message==''){
$("#err_message").append("Message cannot be empty");
}
else if(human==''){
$("#err_human").append("Please answer the security question");
}
});
PHP:
<?php
date_default_timezone_set('Etc/UTC');
require 'phpmailer/PHPMailerAutoload.php';
// Set the email address submissions will be sent to
$email_address = 'myemail#gmail.com';
// Set the subject line for email messages
$subject = 'Test email from localhost using PHPMailer';
// Check for form submission
if(isset($_POST['name'], $_POST['email'], $_POST['message'])){
//Create a new PHPMailer instance
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug = 2;
$mail->Debugoutput = 'html';
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->Username = "myemail#gmail.com";
$mail->Password = "mypassword";
$mail->setFrom($_POST['email'], $_POST['name']);
$mail->addAddress($email_address, 'myname');
$mail->Subject = $subject;
$mail->Body = 'From:'.$_POST['name'].'(' . $_POST['email'] . ')'.'<p><b>Message</b><br/>' . $_POST['message'] . '</p>';
$mail->AltBody = 'This is a plain-text message body';
//send the message, check for errors
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
}
?>

My website doesn't send the mail even though it works perfectly on wamp

Here is my website: www.accurateaccountsinc.tk
The problem is that the website just loads and doesn't do anything.
Everything works properly in localhost/wamp but it doesn't work on the actual site when running the task.
here's the code for the form
<form method="POST" action="index.php">
<input type="text" required name="name" id="name" placeholder="Enter Your Name" />
<input type="text" required name="email" id="email" placeholder="Enter Your Email" />
<input type="text" required name="phone" id="phone" placeholder="Phone Number" />
<textarea name="message" required id="message" placeholder="Enter Your Message"></textarea>
<input type="submit" name="mailed" value="Submit" />
</form>
Here's the php:
<?php
include 'dbconn.php';
if(isset($_POST['mailed'])){
$name = mysqli_real_escape_string($con,$_POST['name']);
$emailadd = mysqli_real_escape_string($con,$_POST['email']);
$contact = mysqli_real_escape_string($con,$_POST['phone']);
$entrymessage = mysqli_real_escape_string($con,$_POST['message']);
include "class.phpmailer.php";
include "class.smtp.php";
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMPTDebug = 1;
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'tls';
$mail->Host = "smtp.gmail.com";
$mail->Port = 587;
$mail->IsHTML(true);
$mail->Username = "email here";
$mail->Password = "password here";
$mail->SetFrom("email here", 'Website Entry');
$subject = "Client Entry";
$message = "<br>Client Name: " . $name;
$message .= "<br>Email Address: " . $emailadd;
$message .= "<br>Contact Number: " . $contact;
$message .= "<br>Message: " . $entrymessage;
$mail->Subject = $subject;
$mail->Body = $message;
$mail->AddAddress("email here", $name);
if($mail->Send()){
unset($_POST['mailed']);
echo "<script>window.open('index.php','_self')</script>";
}
}
?>
if there's any more info you'd like to know please do ask..
also note that I only used the class.phpmailer.php and class.smtp.php to minimize the work needed if that's related to the problem..
Typo: $mail->SMPTDebug should be $mail->SMTPDebug, and you should set it to 2 for useful feedback.
You're also not displaying any errors that it may have caused, so echo $mail->ErrorInfo; if sending fails.
You can't send through gmail on port 25, only to gmail. Stick to tls on Port 587, like all the examples and docs say. I don't know where you got your code, but it looks like you used an old example - you should base it on the examples provided with PHPMailer, particularly the ones for gmail.

phpmailer not sending variables

I am trying to use phpmailer to send a user's input from an html form
Form is like this
<form action="senditpm.php" role="form">
<input type="text" class="form-control" id="name" name="name"><br>
<input type="email" class="form-control" id="email" name="email">>br>
<input type="text" class="form-control" id="phone" name="phone">>br>
<input type="submit" value="Submit" class="submit-button btn btn-default">
</form>
senditpm.php is like this
<?php
$name = $_POST['name'];
$phone = $_POST['phone'];
$email= $_POST['email'] ;
require 'phpmailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->Host = 'xxxxxxxxx';
$mail->SMTPAuth = false;
$mail->Username = 'xxxxxxxx';
$mail->Password = 'xxxxxxxx';
$mail->Port = 25;
$mail->setFrom('xxx#xxxxs.co.uk', 'xxxxx');
$mail->addAddress('xxx#xxx', 'xxx xxxx');
$mail->isHTML(true);
$mail->Subject = 'Here is the subject';
$mail->Body="
Name: $name <br>
Email: $email <br>
Phone: $phone <br>";
;
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Message has been sent';
}
?>
When I complete the form and click Submit, I receive an email like this
Name:
Email:
Phone:
As you can see, the php isn't sending the variables
Please could someone let me know where I am going wrong. Thanks in advance
<form> defaults to a GET method if a POST method is not implied and you are using POST arrays.
So change
<form action="senditpm.php" role="form">
to
<form action="senditpm.php" role="form" method="post">

Categories