PHP - Send copy of form input to user [duplicate] - php

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 7 years ago.
I'm trying to send a copy of a submitted form to the user of the client-side. I tried implementing some code I already found around here but can not get it to work successfully. Would you guys mind taking a look? ...All input appreciated, as always!
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "nonedesigns#gmail.com";
$email_subject = "SuitUP | Support";
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");
if ($success)
mail($email_from, $Subject, $Body, "From: <$email_From>");
// validation expected data exists
$error_message = "";
//get fields, and check if they are filled
$project_name = $_POST['first_name'];
required_field($project_name,"Project Name");
$last_name = $_POST['last_name'];
required_field($last_name,"Last Name");
$email_from = $_POST['email'];
required_field($email_from,"Email");
$contact_reason = $_REQUEST['contact_reason'];
$other_info = $_POST['other_info'];
required_field($other_info,"Additional Info");
//phone number manip
$phone_number_clean = clean_phone_number($phone_number);
//email manip
$email_exp = '/^[A-Za-z0-9._%-]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';

First you need to declare the variables, and then send out the email:
// validation expected data exists
$error_message = "";
//get fields, and check if they are filled
$project_name = $_POST['first_name'];
required_field($project_name,"Project Name");
$last_name = $_POST['last_name'];
required_field($last_name,"Last Name");
$email_from = $_POST['email'];
required_field($email_from,"Email");
$contact_reason = $_REQUEST['contact_reason'];
$other_info = $_POST['other_info'];
required_field($other_info,"Additional Info");
//phone number manip
$phone_number_clean = clean_phone_number($phone_number);
//email manip
$email_exp = '/^[A-Za-z0-9._%-]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "nonedesigns#gmail.com";
$email_subject = "SuitUP | Support";
$success = mail($email_to, $Subject, $Body, "From: <$email_from>");
if ($success)
mail($email_from, $Subject, $Body, "From: <$email_from>");
Try to be consistent in variable naming conventions (not mixing CamelCase and underscores). That will make it easier for you to stay on top of your code.

Try to send mail using PHPMailer. Try the below standard php code but first download phpmailer.php and save it.
// include the PHPMailer
require_once('PHPMailer.php');
$mail = new PHPMailer(); // defaults to using php "mail()"
$mail->IsSendmail(); // telling the class to use SendMail transport
$body = "hello";
$mail->AddReplyTo("name#yourdomain.com","First Last");
$mail->SetFrom('name#yourdomain.com', 'First Last');
$mail->AddReplyTo("name#yourdomain.com","First Last");
$address = "name#yourdomain.com";
$mail->AddAddress($address, "John Doe");
$mail->Subject = "PHPMailer Test Subject via Sendmail, basic";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->MsgHTML($body);
$mail->IsHTML(true);
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}

Related

PHP contact form, not receiving email [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 7 years ago.
So, here we go with silly question number too many to count!
I've made a very simple PHP contact form using tutorials from the internet (I still need to add security measures to it but I wanted to get it working first) When I click on the send button on my website I do get the message sent script however no email arrives in my in box.
Any ideas what I'm doing wrong? The website is currently hosted locally via XAMPP.
$name = $_POST['name'];
$company = $_POST['company'];
$email = $_POST['email'];
$tel = $_POST['tel'];
$message = $_POST['message'];
$from = 'From: me#mywebsite.co.uk';
$to = 'me#mywebsite.co.uk';
$subject = 'Enquiry';
$body = "From: $name\n Company: $company\n Email: $email\n Telephone: $tel\n Message: $message\n";
if ($_POST['send']) {
if(mail($to, $subject, $body, $from)) {
echo '<p> Your message has been sent!</p>';
} else {
echo '<p>Message could not be sent.<br>Please check that you have completed the name, email and message fields and try again</p>';
}
}
Alright:
Step 1: check your error logs for any problems with the mail not being sent. Normally when installing an Apache inside windows the most people skip to set the from server and credentials.
I used WAMP alot and that one works normally only with an external account.
Step 2: If anything fails.
Download a mailer library and use Gmail to send the emails. Here is an tutorial on that : http://phpmailer.worxware.com/?pg=examplebgmail
Works great. Sure there is a lot of files in phpmailer but it works and easy to upgrade when new software versions are released.
As when I've previously set up contact forms I've been doing so using Code Igniter I didn't realise that I couldn't use mail() without installing a mail server.
Thanks to Parris Varney and RiggsFolly for pointing this out and thanks again to Riggs for letting me know that Code Igniter uses the PHPMailer library.
By using PHPmailer I was able to correct the code and get the form working perfectly in very short order.
For anyone interested the new code used with the latest version of PHPmailer is:
$name = $_REQUEST['name'];
$co = $_REQUEST['company'];
$email = $_REQUEST['email'];
$tel = $_REQUEST['tel'];
$message = $_REQUEST['message'];
require("PHPMailerAutoLoad.php");
$mail = new PHPMailer();
$mail->isSMTP();
$mail->Host = "mail.mydomain.co.uk";
$mail->SMTPAuth = true;
$mail->Username = "me#mydomain.co.uk";
$mail->Password = "password";
$mail->SMTPAutoTLS = false;
$mail->From = $email;
$mail->addAddress("me#mydomain.co.uk", "Me");
$mail->WordWrap = 50;
$mail->isHTML(true);
$mail->Subject = "Enquiry";
$mail->Body = "From: $name<br>Company: $co<br>Email: $email<br>Telephone: $tel<br>Message: $message";
$mail->AltBody = "From: $name Company: $co Email: $email Telephone: $tel Message: $message";
if(!$mail->Send())
{
echo "Message could not be sent";
}
echo "Message has been sent";
?>

How to add form validation to phpmailer?

I am a newbie to PHP, and I am currently using phpmailer to let my clients send emails to me, but a bot is apparently taking advantage of it and sends 3 emails at exactly the same time everyday. So I've been trying to add validation to the form using PHP as javascript can be stopped to avoid validation.. Could you guys please help me add validation to this? I just need to validate name and email. Thank you so much in advance.
PS. I removed some parts of the code for privacy.
<?php
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$address = $_POST['address'];
$proptypes = $_POST['proptypes'];
$units = $_POST['units'];
$purchaseOrRefi = $_POST['purchase'];
$loans = $_POST['loans'];
$income = $_POST['income'];
$apt = $_POST['apartment'];
$message = $_POST['message'];
$body = "<b>[From]</b><br>$name<br><br> <b>[E-Mail]</b><br>$email<br><br> <b>[Phone #]</b><br>$phone<br><br> <b>[Address]</b><br>$address<br><br> <b>[Type of Property]</b><br>$proptypes<br><br> <b>[# of Units]</b><br>$units<br><br> <b>[Purchase or Refi?]</b><br>$purchaseOrRefi<br><br> <b>[Amnt of Loans Requested]</b><br>$loans<br><br> <b>[Total Income]</b><br>$income<br><br> <b>[Total Apt Expense]</b><br>$apt<br><br> <b>[Message]</b><br>$message<br><br>";
$mail->Subject = 'Someone wants to hear more about your mortgage programs!';
$mail->Body = $body;
$mail->From = '';
$mail->FromName = '';
// Add a recipient
$mail->addAddress('');
$mail->addAddress('');
$mail->addAddress('');
$mail->addAttachment($_FILES['attachment']['tmp_name'], $_FILES['attachment']['name'], 'base64', $_FILES['attachment']['type']); // Add attachments
$mail->isHTML(true);
if(!$mail->send()) {
echo 'Message could not be sent. Please try again.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
echo 'Your message has been sent! We will get back to you soon!';
}
?>

Converting PHP Contact Form to SMTP

had alot fo problems making a functional contact form tonight. After messing for hours i finally found out that i can only use SMTP with my web hoster.
Can anyone please advice me how i can complete my form?
This is my current SMTP PHP Form
$mail = new PHPMailer();
//Your SMTP servers details
$mail->IsSMTP(); // set mailer to use SMTP
$mail->Host = "localhost"; // specify main and backup server or localhost
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = "enquiries#c(hidden)y.co.uk"; // SMTP username
$mail->Password = "******"; // SMTP password
//It should be same as that of the SMTP user
$redirect_url = "http://".$_SERVER['SERVER_NAME']; //Redirect URL after submit the form
$mail->From = $mail->Username; //Default From email same as smtp user
$mail->FromName = "Display Name";
$mail->AddAddress("enquiries#c(hidden)y.co.uk", "chapnolo"); //Email address where you wish to receive/collect those emails.
$mail->WordWrap = 50; // set word wrap to 50 characters
$mail->IsHTML(true); // set email format to HTML
$mail->Subject = $_POST['subject'];
$message = "Name of the requestor :".$_POST['fullname']." \r\n <br>Email Adrress :".$_POST['email']." \r\n <br> Query :".$_POST['query'];
$mail->Body = $message;
if(!$mail->Send())
{
echo "Message could not be sent. <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}
echo "Message has been sent";
header("Location: $redirect_url");
}
?>
I need my form to have the following functionality for me to retrieve the requires e-mail message body.
$name = $_POST['name'];
$email = $_POST['email'];
$number = $_POST['number'];
$message = $_POST['message'];
$web = $_POST['web'];
$formcontent="From: $name \n Contact: $number \n Website: $web \n Message: $message";
$recipient = "enquiries#c(hidden)y.co.uk";
$subject = "Contact Form";
$mailheader = "From: $email ";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You!" . " -" . "<a href='contact.html' style='text-decoration:none;color:#ff0099;'> Return Home</a>";
Help will be greatly appreciated
Metexora - So you're not having any issues with the code, you just haven't told it to send you the name contact number, email and message body - just change:
$message = "Name of the requestor :".$_POST['fullname']." \r\n <br>Email Adrress :".$_POST['email']." \r\n <br> Query :".$_POST['query'];
to
$message = "Name of the requestor :".$name." \r\n <br>Email Adrress :".$email." \r\n <br> Phone number :".$number."\r\n <br> Message: ".$message."\r\n <br> Website: ".$web;
(Note: Be careful, you are re-declaring $message here, that isn't really recommended practise given they refer to different things, the user message and the email message, you should only use the change the contents of a variable if it still refers to the same thing)

PHPMail - can I send two unique emails

I'm using PHPMailer to successfully send an email upon a webform submission (so just using basic post data, no mysql databases or any lookups).
What I need to do is send two seperate emails, one version for the customer and the other for a customer service agent - so that when a user completes a webform, they will receive a 'marketing' version of the email, whilst the customer service agent will receive an email just containing the details submitted by the user.
See below what im using at the moment, but not sure how to best implement to send the secoind email? It presently fails and the script exits after sending only one email.
$body = ob_get_contents();
$to = 'removed';
$email = 'removed';
$fromaddress = "removed";
$fromname = "removed";
require("phpmailer.php");
$mail = new PHPMailer();
$mail->From = $fromaddress;
$mail->FromName = $fromname ;
$mail->AddAddress("email#example.com");
$mail->WordWrap = 50;
$mail->IsHTML(true);
$mail->Subject = "Subject";
$mail->Body = $body;
$mail->AltBody = "This is the text-only body";
if(!$mail->Send()) {
$recipient = 'email#example.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;
}
//Send the customer version
$mail=new PHPMailer();
$mail->SetFrom('email', 'FULLNAME');
$mail->AddAddress($mail_vars[2], 'sss');
$mail->Subject = "Customers email";
$mail->MsgHTML("email body here");
//$mail->Send();
In the customer version you are not setting the email's properties the same way you are in the first. For example you use $mail->From = $fromaddress; in the first and $mail->SetFrom('email', 'FULLNAME'); in the second.
Because you instantiated a new $mail=new PHPMailer(); you need to set the properties again.
Instead of just bailing out with a useless "something didn't work" message, why not have PHPMailer TELL you what the problem is?
if (!$mail->send()) {
$error = $mail->ErrorInfo;
echo "Mail failed with error: $error";
}

phpMailer Attachment not working

this is my first post and I hope that I can get some help regarding adding an attachment field in my phpMailer contact form. I have already added an uploader [browse] bar in the html website but I don't know how to link it with the phpmailer. do I have to declare it at the package which is phpmailer.php or do something with it?
I would really appreciate some help. Below is my desperate attempt.
Snippet from the code:
<?
$body = ob_get_contents();
$to = 'xxx#xxxx.com>';
$email = $email;
$subject = $subject;
$fromaddress = "xxx#xxxx.com";
$fromname = "Online Contact";
require("phpmailer.php"); //<<this is the original pack
$mail = new PHPMailer();
$mail->From = "xxx#xxxx.com";
$mail->FromName = "My Name";
$mail->AddBCC("xxx#xxxx.com","Name 1");
$mail->AddAddress( $email,"Name 2");
$mail->WordWrap = 50;
$mail->IsHTML(true);
$mail->Subject = $subject;
$mail->Body = $body;
$mail->AltBody = "This is the text-only body";
// attachment
$mail->AddAttachment('uploadbar', 'new_file.pdf'); // the name of my html uploader is uploadbar, clicking browse to locate a file
if(!$mail->Send()) {
$recipient = 'xxx#xxxx.com';
$subject = 'Contact form failed';
$content = $body;
mail($recipient, $subject, $content, "From: xxx#xxxx.com\r\nReply-To: $email\r\nX-Mailer: DT_formmail");
exit;
}
?>
Had the same problem, try this
$mail->AddAttachment($_SERVER['DOCUMENT_ROOT'].'/file-name.pdf');
First, make sure your upload form has enctype='multipart/form-data' as in <form method=post action=file.php enctype='multipart/form-data'>.
then, use this code in your phpmailer handler file
foreach(array_keys($_FILES['files']['name']) as $key) {
$source = $_FILES['files']['tmp_name'][$key];
$filename = $_FILES['files']['name'][$key];
$mail->AddAttachment($source, $filename);
}
Make sure attachment file name should not contain any special character and check the file path also correct.
eg: [file name.pdf] Should be like [filename.pdf]

Categories