sendmail.php does not work on mobile - php

Strange issue - my sendmail.php is working perfectly on desktop and on mobile devices only when requesting desktop websites (in Chrome app), but when using mobile site he does not work at all.
Can someone help me figure this out?
here is the code:
<?php
if(isset($_POST['email'])) {
if (!check_email($_POST['email']))
{
echo 'Please enter a valid email address<br />';
}
else send_email();
}
exit;
function check_email($emailAddress) {
if (filter_var($emailAddress, FILTER_VALIDATE_EMAIL)) {
return TRUE;
} else {
return FALSE;
}
}
function send_email() {
$message = "\nName: " . $_POST['name'] .
"\nEmail: " . $_POST['email'] ;
$message .= "\nMessage: " . $_POST['comment'] .
"\n\nBrowser Info: " . $_SERVER["HTTP_USER_AGENT"] .
"\nIP: " . $_SERVER["REMOTE_ADDR"] .
"\n\nDate: " . date("Y-m-d h:i:s");
$siteEmail = $_POST['receiver'];
$emailTitle = $_POST['subject'];
$thankYouMessage = "Thank you for contacting us, we'll get back to you shortly.";
if(!mail($siteEmail, $emailTitle, $message, 'From: ' . $_POST['name'] . ' <' . $_POST['email'] . '>'))
{
echo 'error';
}
else
{
echo 'success';
}
}
?>

You must make sure the mobile form has these two elements:
It is being submitted using method="POST".
Your email input has the attribute and value name="email".
I demonstrate where these things are set in the following code. This is incomplete, of course, it's just designed to show you where the two required parts must be.
<form ... method="POST">
...
<input type="text" name="email" ... >
...
</form>
I need to mention one more thing ...
That being said, what you're doing here is EXTREMELY INSECURE. You are allowing someone to set an email's from and two address on a web form. A (not so) clever hacker can easily write a script to turn your server into an open relay for spam or other evil activities. At minimum, you should remove $_POST['receiver'] and replace with with a hard-coded email address or at least not something that can be altered by an end-user when they POST to your form.

So I drilled down and found that the mobile form is directing submissions through this PHP file which was 404.
The issue now is, even when this file is avaiable, emails are not sent...
' . "\r\n"; $headers .=
'From: ' . "\r\n"; $headers = 'MIME-Version: 1.0' .
"\r\n"; $headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
$message .= "Name: " . $name . ""; $message .= "Subject: "
. $subject . ""; $message .= "Email: " . $email . ""; $message .= "Message: " . $message_text . "";
mail($to, $subject, $message, $headers, "-f noreplay#info.com");
echo 1; }
if(isset($_POST['sendmail']) && $_POST['sendmail'] == 1){
send_mail(); } ?>

Related

How to Send emails to multiple recipients in PHP [duplicate]

This question already has answers here:
PHP mail: Multiple recipients?
(5 answers)
Closed 1 year ago.
I need to send my forms to multiple recipients, but I can't figure it out which line I need to edit. Please see below. I appreciate your help.
I already tried adding more values to the emailto, but I can't get it to work.
I need to send my forms to multiple recipients, but I can't figure it out which line I need to edit. Please see below. I appreciate your help.
I already tried adding more values to the emailto, but I can't get it to work.
Hello there,
I need to send my forms to multiple recipients, but I can't figure it out which line I need to edit. Please see below. I appreciate your help.
<?php
// Configure your Subject Prefix and Recipient here
$errors = array(); // array to hold validation errors
$data = array(); // array to pass back data
if($_SERVER['REQUEST_METHOD'] === 'POST') {
$subjectPrefix = $_POST['subject'];
$privacyPolicy = $_POST['privacy-policy'];
$emailTo = stripslashes(trim($_POST['email-to']));
$name = stripslashes(trim($_POST['name']));
$email = stripslashes(trim($_POST['email']));
$phone = stripslashes(trim($_POST['phone']));
$message = stripslashes(trim($_POST['message']));
$spam = $_POST['textfield'];
$confirmMsg = $_POST['confirm'];
$captcha = $_POST['captcha'];
if (empty($name)) {
$errors['name'] = 'Please fill in all required fields.';
}
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$errors['email'] = 'Please fill in all required fields.';
}
if (empty($message)) {
$errors['message'] = 'Please fill in all required fields.';
}
if (empty($captcha)) {
$errors['captcha'] = 'TEST CAPTCHA';
}
if (empty($privacyPolicy)) {
$errors['privacy_policy'] = 'Please fill in all required fields.';
}
// if there are any errors in our errors array, return a success boolean or false
if (!empty($errors)) {
$data['success'] = false;
$data['errors'] = $errors;
} else {
$subject = "Message from $subjectPrefix";
$body = '
<strong>Name: </strong>'.$name.'<br />
<strong>Email: </strong>'.$email.'<br />
<strong>Phone: </strong>'.$phone.'<br />
<strong>Message: </strong>'.nl2br($message).'<br />
';
$headers = "MIME-Version: 1.1" . PHP_EOL;
$headers .= "Content-type: text/html; charset=utf-8" . PHP_EOL;
$headers .= "Content-Transfer-Encoding: 8bit" . PHP_EOL;
$headers .= "Date: " . date('r', $_SERVER['REQUEST_TIME']) . PHP_EOL;
$headers .= "Message-ID: <" . $_SERVER['REQUEST_TIME'] . md5($_SERVER['REQUEST_TIME']) . '#' . $_SERVER['SERVER_NAME'] . '>' . PHP_EOL;
$headers .= "From: " . "=?UTF-8?B?".base64_encode($name)."?=" . " <$email> " . PHP_EOL;
$headers .= "Return-Path: $emailTo" . PHP_EOL;
$headers .= "Reply-To: $email" . PHP_EOL;
$headers .= "X-Mailer: PHP/". phpversion() . PHP_EOL;
$headers .= "X-Originating-IP: " . $_SERVER['SERVER_ADDR'] . PHP_EOL;
if (empty($spam)) {
mail($emailTo, "=?utf-8?B?" . base64_encode($subject) . "?=", $body, $headers);
}
$data['success'] = true;
$data['confirmation'] = $confirmMsg;
}
// return all our data to an AJAX call
echo json_encode($data);
}
You can separate receivers by comma like
$to = "somebody#example.com, somebodyelse#example.com";
As indicated in the mail() documentation you can use this:
$emailTo = $mail1 . ', ' . $mail2;

Mail Function is giving false return in PHP

I have tried sending email from a basic html form by php mail() function and It has given me false return
Code:
<?php
if(isset($_POST['submit'])){
$title = $_POST['title'];
$name = $_POST['first_name'];
$name1 = $_POST['last_name'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$company=$_POST['lead_object'];
$skype= $_POST['skype_id'];
$to = 'himanshu#webkidukan.com';
$subject = 'Advertiser Form Details';
$from = 'ssing648#gmail.com';
// 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";
// Create email headers
$headers .= 'From: '.$from."\r\n".
'Reply-To: '.$from."\r\n" .
'X-Mailer: PHP/' . phpversion();
// Compose a simple HTML email message
$message = '<html><body>';
$message .= '<h1 style="color:#f40;">Hi Sir</h1>';
$message .= '<p style="color:#080;font-size:18px;">Details of Advertiser Form</p>';
$message .= '<p style="color:#080;font-size:18px;"><?php $title . " " . $name . " " . "?>wrote the following:"<?php . "\n\n" . $name . "\n\n" . $name1 . "\n\n" .$email . "\n\n" .$phone . "\n\n" .$company. "\n\n" .$skype. "\n\n"?> </p>';
$message .= '</body></html>';
$t = mail($to, $from, $message, $headers,$subject);
var_dump($t);exit;
if ($t) {
echo "Mail Sent. Thank you " . $name . " .We will contact you shortly.";
}else{
echo "Failed to send email. Please try again later";
}
echo "<script type='text/javascript'>alert('Thanks for filling out our form! We will look over your message and get back to you soon.')</script>";
echo "<script> window.location.href = 'advertiser.php';</script>";
}
?>
I have this issue in three forms together , hence posting one for the understanding the mistake, that I am doing. Can anyone of you help me with the same.Or should I go for the SMTP mail option.Also I am sending the form details in mail to the user.So also check that the way to send the flyer is right or not.
There are some unwanted PHP tags while appending message. try like this
$message = '<html><body>';
$message .= '<h1 style="color:#f40;">Hi Sir</h1>';
$message .= '<p style="color:#080;font-size:18px;">Details of Advertiser Form</p>';
$message .= '<p style="color:#080;font-size:18px;">'.$title." ".$name." wrote the following:"."\n\n".$name."\n\n".$name1."\n\n".$email."\n\n".$phone."\n\n".$company."\n\n".$skype."\n\n".'</p>';
$message .= '</body></html>';

How do I connect my PHP code to Heroku's addon 'SendGrid'?

I purchased a template that included a PHP contact form using AJAX I'm using Heroku to deploy my web application,(my portfolio), and SendGrid as my add-on in order to send API-driven emails from my portfolio. When I submit my form, I get a '500: Internal Server Error', so my question is, how to I integrate SendGrid into the PHP code I already have?
I'm unfamiliar with PHP and during research I did download composer and these files were automated:
- A vender folder
-composer.json file
-composer.lock file
Here is my Repo: https://github.com/Mendiolac/Portfolio
Here is my Portfolio link: https://cm-portfolio.herokuapp.com/
I tried the SMTP route where SendGrip gives you the steps to configure your application by connecting it their server and when I tested it was unsuccessful without an error message.
<?php
require '../vendor/autoload.php';
if(!isset($_SERVER['HTTP_X_REQUESTED_WITH']) AND strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') {
die('Sorry Request must be Ajax POST');
}
if(isset($_POST)) {
$name = filter_var($_POST["name"], FILTER_SANITIZE_STRING);
$email = filter_var($_POST["email"], FILTER_SANITIZE_EMAIL);
$subject = filter_var($_POST["subject"], FILTER_SANITIZE_STRING, FILTER_FLAG_NO_ENCODE_QUOTES);
$message = filter_var($_POST["message"], FILTER_SANITIZE_STRING);
if(!$contact_email_to || $contact_email_to == 'contact#example.com') {
die('The contact form receiving email address is not configured!');
}
if(strlen($name)<3){
die($contact_error_name);
}
if(!$email){
die($contact_error_email);
}
if(strlen($subject)<3){
die($contact_error_subject);
}
if(strlen($message)<3){
die($contact_error_message);
}
if(!isset($contact_email_from)) {
$contact_email_from = "contactform#" . #preg_replace('/^www\./','', $_SERVER['SERVER_NAME']);
}
$headers = 'From: ' . $name . ' <' . $contact_email_from . '>' . PHP_EOL;
$headers .= 'Reply-To: ' . $email . PHP_EOL;
$headers .= 'MIME-Version: 1.0' . PHP_EOL;
$headers .= 'Content-Type: text/html; charset=UTF-8' . PHP_EOL;
$headers .= 'X-Mailer: PHP/' . phpversion();
$message_content = '<strong>' . $name_title . '</strong> ' . $name . '<br>';
$message_content .= '<strong>' . $email_title . '</strong> ' . $email . '<br>';
$message_content .= '<strong>' . $message_title . '</strong> ' . nl2br($message);
$sendemail = mail($contact_email_to, $subject_title . ' ' . $subject, $message_content, $headers);
if( $sendemail ) {
echo 'OK';
} else {
echo 'Could not send mail! Please check your PHP mail configuration.';
}
}
?>
You are doing it correctly on https://github.com/Mendiolac/Portfolio/commit/0ad2fc815523a26f272f91bb192123c4caf41632 , just need to add keys to your .env file

Multiple field PHP form submission

The problem I'm having is that I want to pass through data from a number of fields in a form into one email send via PHP, the concerned code is as follows:
$mailcheck = spamcheck($_POST['inputemail']);
if ($mailcheck==FALSE)
{
echo "Invalid input";
}
else
{
// This still needs to be debugged online as soon as possible
$from = $_POST['inputemail'];
$name = $_POST['inputname'];
$phone = $_POST['inputphone'];
$date = $_POST['inputdate'];
$time = $_POST['inputtime'];
$data = $_POST['inputprotection'];
$subject = $_POST['inputdropdown'];
$message = $_POST['inputmessage'];
$message = wordwrap($message, 70);
mail("UP498701#myport.ac.uk",$subject,$message,"From: $from\n" . "Name: $name\n" . "Phone: $phone\n" . "Date: $date\n" . "Time: $time\n" . "Keep Data? $data\n");
echo "Thank you for sending us feedback, you'll be redirected in 5 seconds";
}
}
This is where I'm declaring all the information to be passed as a header for the email. The current PHP passes syntax check as works to the extent that an email is sent containing the $to, $subject, $message all fine, but the $header only passes through the final part (Keep Data? $data\n). When I remove all the other fields and simply keep the $data part, the email stops sending any $header. I also have an issue with the redirect, which has been removed from the below code and will be inserted as soon the current issue is resolved. The current full PHP is:
<html>
<body>
<?php
function spamcheck($field)
{
// Sanitize e-mail address
$field=filter_var($field, FILTER_SANITIZE_EMAIL);
// Validate e-mail address
if(filter_var($field, FILTER_VALIDATE_EMAIL))
{
return TRUE;
}
else
{
return FALSE;
}
}
?>
<?php
if (isset($_POST['inputemail']))
{
$mailcheck = spamcheck($_POST['inputemail']);
if ($mailcheck==FALSE)
{
echo "Invalid input";
}
else
{
// This still needs to be debugged online as soon as possible
$from = $_POST['inputemail'];
$name = $_POST['inputname'];
$phone = $_POST['inputphone'];
$date = $_POST['inputdate'];
$time = $_POST['inputtime'];
$data = $_POST['inputprotection'];
$subject = $_POST['inputdropdown'];
$message = $_POST['inputmessage'];
$message = wordwrap($message, 70);
mail("UP498701#myport.ac.uk",$subject,$message,"From: $from\n" . "Name: $name\n" . "Phone: $phone\n" . "Date: $date\n" . "Time: $time\n" . "Keep Data? $data\n");
echo "Thank you for sending us feedback, you'll be redirected in 5 seconds";
}
}
?>
</body>
and is temporarily hosted at http://luke-hale.com/temp/contact.html. I'm sure this is just a case of being misinformed on my part, but any help would be great, cheers!
EDIT:
okay so that issue is sorted with:
$subject = $_POST['inputdropdown'];
$message = $_POST['inputmessage'] . "\r\n\r\n" . $_POST['inputname'];
$message = wordwrap($message, 70);
$headers = "From: " . $_POST['inputemail'];
mail("UP498701#myport.ac.uk",$subject,$message,$headers);
echo "Thank you for sending us feedback, you'll be redirected in 5 seconds";
Which works though doesn't display the $headers anywhere, but I'm not too fussed about this, the next thing is the redirect, which I would usually run through via:
header("refresh:5;url=http://www.domain.com")
Though this was not working correctly earlier, I will apply an edit when tested for anyones future reference.
EDIT
So the form works but the re-direct does not. The site is still hosted on the same domain, but now the full PHP looks like this:
<html>
<body>
<?php
function spamcheck($field)
{
// Sanitize e-mail address
$field=filter_var($field, FILTER_SANITIZE_EMAIL);
// Validate e-mail address
if(filter_var($field, FILTER_VALIDATE_EMAIL))
{
return TRUE;
}
else
{
return FALSE;
}
}
?>
<?php
if (isset($_POST['inputemail']))
{
$mailcheck = spamcheck($_POST['inputemail']);
if ($mailcheck==FALSE)
{
echo "Invalid input";
}
else
{
$subject = $_POST['inputdropdown'];
$message = "Name: " . $_POST['inputname'] . "\r\n\r\n" . "Email: " . $_POST['inputemail'] . "\r\n\r\n" . "Phone: " . $_POST['inputphone'] . "\r\n\r\n" . "Date: " . $_POST['inputdate'] . "\r\n\r\n" . "Time: " . $_POST['inputtime'] . "\r\n\r\n" . "Retain Data? " . $_POST['inputprotection'] . "\r\n\r\n" . "Message: " . $_POST['inputmessage'];
$message = wordwrap($message, 70);
$headers = "From: " . $_POST['inputemail'];
mail("UP498701#myport.ac.uk",$subject,$message,$headers);
echo "Thank you for sending us feedback, you'll be redirected in 5 seconds";
}
}
header("refresh:5;url=http://www.domain.com")
?>
</body>
The form still sends fine but the return states that the line beginning "header" cannot be passed because the browser data has already been modified. I'm not sure sure what I've done or where so if anyone could lend a hand, that'd be great!
FINAL EDIT
My final, fully working, code:
<?php
function spamcheck($field)
{
$field=filter_var($field, FILTER_SANITIZE_EMAIL);
if(filter_var($field, FILTER_VALIDATE_EMAIL))
{
return TRUE;
}
else
{
return FALSE;
}
}
?>
<?php
ob_start();
if (isset($_REQUEST['inputemail'])&&($_REQUEST['inputname'])&&($_REQUEST['inputmessage']))
{
$mailcheck = spamcheck($_POST['inputemail']);
if ($mailcheck==FALSE)
{
echo "Invalid email, you'll be redirected ";
header("refresh:5;url=http://www.luke-hale.com/");
}
else
{
$subject = $_POST['inputdropdown'];
$message = "Name: " . $_POST['inputname'] . "\r\n\r\n" . "Email: " . $_POST['inputemail'] . "\r\n\r\n" . "Phone: " . $_POST['inputphone'] . "\r\n\r\n" . "Date: " . $_POST['inputdate'] . "\r\n\r\n" . "Time: " . $_POST['inputtime'] . "\r\n\r\n" . "Retain Data? " . $_POST['inputprotection'] . "\r\n\r\n" . "Message: " . $_POST['inputmessage'];
$message = wordwrap($message, 70);
$headers = "From: " . $_POST['inputemail'];
mail("UP498701#myport.ac.uk",$subject,$message,$headers);
echo "Thank you for your messgae, I'll get back to you as soon as possible! You'll be redirected in 5 seconds.";
}
header("refresh:5;url=http://www.luke-hale.com/");
}
else
{
echo "You did not fill all the required fields, please try again.";
header("refresh:5;url=http://www.luke-hale.com/");
}
?>
Try putting your form data into the $message variable, insert \n after each attribute to give a new line. Do not use the header.
Location and Refresh both require an absolute URI
header('Location: http://www.domain.com');
Examples for mail:
https://stackoverflow.com/a/22833890/3489793
https://stackoverflow.com/a/22831825/3489793
mail("Your Email Address Here",$subject,$therest,"subject: $subject\n");
U said this:
"The mail function is also not the issue, and correct syntax is" mail($to,$subject,$message,$headers,$parameters)
Explanation:
your email adress = $to
$subject = $subject
$therest = $message (rename it if you want)
"subject: $subject\n" = the header (change it to $headers if you want that)
Header example (as in the documentation)
$headers = 'From: webmaster#example.com' . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
You could echo a Javascript at the bottom of your PHP script which redirects instead, like this:
$url = "url";
$time = 5000 // time in milliseconds
print "<script>window.setTimeout(function(){window.location='".$url."'},".$time.") </script>"

The PHP script is not sending emails to my account

Can anyone please help me with this php code. I am using a php code in my contact us page to send an email to my account. Somehow the code is not working. The basic function of this code is to capture the details inputted by an user and send the details to my email account. But somehow the PHP code is executing and not showing any error but is not sending email to my account. So, anyone please suggest me where i am going wrong... Thanks all...
Here is the PHP code which i am using:
<?php
error_reporting(0);
error_reporting (E_NONE);
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$mob_no = $_POST['mob_no'];
$email = $_POST['email'];
$course = $_POST['course'];
$location = $_POST['location'];
$city = $_POST['city'];
$to = "example#mywebsite.com";
$subject = 'Request Assistance';
$headers = array();
$headers[] = 'MIME-Version: 1.0';
$headers[] = 'Content-type: text/html; charset = iso-8859-1';
$headers[] = 'Content-Transfer-Encoding: 7bit';
$headers[] = 'From: ' . $first_name . " " . $last_name;
$message .= "First Name: " . $first_name . "\n";
$message .= "Last Name: " . $last_name . "\n";
$message .= "Mobile No: " . $mob_no . "\n";
$message .= "E-Mail: " . $email . "\n";
$message .= "Course: " . $course . "\n";
$message .= "Location: " . $location . "\n";
$message .= "City: " . $city . "\n";
?>
<?php
$success = mail($to, $subject, $message, $headers);
if (($_POST['first_name'] == "") || ($_POST['last_name'] == "") || ($_POST['mob_no'] == "") || ($_POST['email'] == "") || ($_POST['course'] == "") || ($_POST['location'] == "") ||($_POST['city'] == "")) {
echo '<h1>Sorry for the inconvenience!</h1>';
echo '<p>We have encountered a problem, Please Note:<br />';
echo 'Make sure you fill in all the details<br />';
echo 'Do not use spaces or special characters, except in E-Mail.<br />';
echo 'Inconvenience is deeply regretted!!!</p>';
}
if ((!ctype_alpha($first_name)) || (!ctype_alpha($last_name)) || (!ctype_digit($mob_no)) || (!ctype_alpha($course)) || (!ctype_alpha($location)) || (!ctype_alpha($city))) {
echo '<h1>Sorry for the inconvenience!</h1>';
echo '<p>We have encountered a problem, Please Note:<br />';
echo 'Make sure you fill in all the details<br />';
echo 'Do not use spaces or special characters, except in E-Mail.<br />';
echo 'Inconvenience is deeply regretted!!!</p>';
}
if ($success) {
echo '<h1>Congratulations!</h1>';
echo '<p>The following message has been sent:<br /><br />';
echo '<b>To:</b> ' . $to . '<br />';
echo '<b>From:</b> ' . $email . '<br />';
echo $message;
}
?>
Your code itself doesn't look wrong to me, see following:
1. Make sure your host enables mail function
2. Using mail may result in the junk folder sometimes as many spam uses this
3. Try using phpmailer, similar to mail but more configurable

Categories