Inquiries : Sometime getting, sometimes not? - php

I have added a quick contact form in every page so clients can easily get in touch. However, three different things are happening as below:
sometimes getting inquiries from client. (Genuine I can say, we worked together)
sometimes getting blank message. with Unknown sender every alternate day.
Getting nothing. (Whenever I am trying to check whether all pages working or not. I am not getting email on first time. (Sometimes second, third, fourth time works).
My code is as follows:
PHP script I am using:
<?php
header('Content-type: application/json');
$status = array(
'type'=>'success',
'message'=>' some message '
);
//echo '<pre>';print_r($_POST);exit;
$name = #trim(stripslashes($_POST['name']));
$email_from = #trim($_POST['email']);
$websitelink = #trim(stripslashes($_POST['websitelink']));
$subject = 'iMarketingJunkies: Inquiry';
$message = #trim(stripslashes($_POST['message']));
// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= 'From: <'.$email_from.'>' . "\r\n";
//$email_from = $email;
$email_to = 'myemail#email.com';//replace with your email
$body = 'Name: ' . $name . '<br><br>' . 'Email: ' . $email_from . '<br><br>' . 'URL: ' . $websitelink . '<br><br>' . 'Message: ' . $message;
$success = #mail($email_to, $subject, $body, $headers);
echo json_encode($status);
die;
?>
Can anybody please help me to get rid of this. I have tested all pages and I am sure we can't add submit button without any data. I am using Cloudfare.
Thank you

Related

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>';

Echoing image in php mail , showing error

I have a form when a user enters data and submits, it will go to his mail, now I am having problem with keeping my logo in the mail, below is my php code for mail
<?php
if(isset($_POST['submit'])){
$to = "contact#bolstersolutions.com"; // this is your Email address
$from = $_POST['name1']; // this is the sender's Email address
$first_name = $_POST['name2'];
$last_name = $_POST['email2'];
$last_name1 = $_POST['number1'];
$subject = "Referal";
$subject2 = "Your Friend " . $from . " Has Referred You To Us For UK Process";
$message = $from . " has refered the following student :" . "\n\n" . $first_name. "\n\n" .$last_name. "\n\n" .$last_name1;
$message2 = "Your friend " . $from . " has referred you to Bolster Solutions for UK process. We will be more than happy to process your applications, thus helping you in achieving your goals to study MS in UK." . "\n\n" . "Feel free to go to the following url for more information or else you can also call us # +91 9666999430 / 7661919191." . "\n\n" . "URL: https://consultancy.bolstersolutions.com/mail/" . "\n\n" . "Thanks and Regards." . "\n\n" . echo "<img src='https://consultancy.bolstersolutions.com/mail/assets/img/logo.png'>" ;
$headers = "From:" . $from;
$headers2 = "From: Bolster Solutions " . $to;
mail($to,$subject,$message,$headers);
mail($last_name,$subject2,$message2,$headers2);
}
?>
the following error is happening:
Parse error: syntax error, unexpected 'echo' (T_ECHO) in C:\xampp\htdocs\mail\index.php
Can anyone please help me with this, thanks in advance.
Remove echo in $message2
Set headers like this
$headers = "From: " . strip_tags($_POST['req-email']) . "\r\n";
$headers .= "Reply-To: ". strip_tags($_POST['req-email']) . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
Then send : mail($last_name,$subject2,$message2,$headers);
There is a syntax error near: ..."\n\n" . echo "<img src.... Removing echo should solve it.

PHP 500 Internal Server Error- mail function

I'm getting the 500 Internal Server Error. I called goDaddy and they told me it's not something on their end. I also don't have a .htaccess file. I checked the contact page and checked spelling it is calling the right .php file.
This code was working perfectly. I tried to change something it didn't work. I tried going back to the original code and now that doesn't work either. I don't have a backup. I commented out line by line to pin-point the problem if I erase the mail($to, $subject, $message, $headers); part I don't get an error but obviously don't receive anything. Here is the website if that helps to inspect.
http://www.crownjewelre.com/contacts.html
<?php
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$email = $_POST['email'];
$telephone = $_POST['telephone'];
$comments = $_POST['comments'];
// multiple recipients
$to = 'michaelgaraysi#yahoo.com' . ', '; // note the comma
// subject
$subject.= "CrownJewelRe question From: ".$first_name." ".$last_name. "\n";
// message
$message = "
<html>
<head>
<title>Contact Information</title>
</head>
<body>
<table>
<tr>
<td>Name: </td><th>".$first_name."</th><th>".$last_name."</th>
</tr>
<tr>
<td>Email: </td><th>".$email."</th>
</tr>
<tr>
<td>Telephone: </td><th>".$telephone."</th>
</tr>
</table>
<br><br>
<p>Comments: <strong>".$comments."</strong></th>
</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);
?>
like the others $subject.= is auto-expand of a non existing var but that not the problem here :)
1st : have you tested a blank page with only the function mail :
mail ('michaelgaraysi#yahoo.com','test','message corpus');
if its working its not a missconfig of youre PHP.
2nd : How can you use in 2015 the mail function, its have a high spam rate, instead use https://github.com/PHPMailer/PHPMailer or other.
I think it will solve your problem or maybe you just need to remove the "\n"; on $subject :)
try to change,
$to = 'michaelgaraysi#yahoo.com' . ', ';
to
$to = 'michaelgaraysi#yahoo.com';
and
$subject.= "CrownJewelRe question From: ".$first_name." ".$last_name. "\n";
to
$subject= "CrownJewelRe question From: ".$first_name." ".$last_name. "\n";
and finally, try to run this simple php mailer script, and then apply your changes,
<?php
$to = 'michaelgaraysi#yahoo.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: birthday#example.com' . "\r\n" .
'Reply-To: birthday#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
?>

getting warning Header may not contain more than a single header on wordpress

I'm learning WordPress and PHP and building a website at www.sunriselodging.com
When trying to enter data in a form, I get a PHP error warning saying 'header may not contain more than a single header' on line 6. I've tried solutions like checking the URL which are not related to PHP. I read a few solutions here that are PHP dependent and hence I cannot figure out what needs to be done here.
My code is below:
<?php
$reservation_message = $_POST['reservation_message'];
// Redirect back
Header('Location: '. $_POST['reservation-url'].'?message='.$reservation_message);
echo "<meta charset='utf-8'>";
// Loading variables from form
$blog_name = $_POST['blog_name'];
$blog_email = $_POST['blog_email'];
$reservation_email_subject = $_POST['reservation_email_subject'];
$reservation_email = $_POST['reservation_email'];
$reservation_email_switch = $_POST['reservation_email_switch'];
$room = $_POST['reservation-room'];
$checkin = $_POST['reservation-checkin'];
$checkout = $_POST['reservation-checkout'];
$people = $_POST['reservation-people'];
$name = $_POST['reservation-name'];
$email = $_POST['reservation-email'];
$phone = $_POST['reservation-phone'];
$message = $_POST['reservation-message'];
if($reservation_email_switch == 'on'){
// EMAIL TO CLIENT
// SET info to email
// From in format NAME <email>
$from = $blog_name.'<'.$blog_email.'>';
// Reply to in format NAME <email>
$reply = $blog_name.'<'.$blog_email.'>';
// Subject
$subject = $reservation_email_subject;
// Message
$message = $reservation_email;
//
$to = $email;
$headers = 'From: '. $from . "\r\n" .
'Reply-To: '. $reply . "\r\n" .
'X-Mailer: PHP/' . phpversion();
// Send mail
mail($to,$subject,$message,$headers);
// END OF EMAIL TO CLIENT
// ---------------------
}
// EMAIL TO RESERVATION CREW
$message = $_POST['reservation-message'];
$headers = 'From: '. $name . '<' . $email . '>' . "\r\n" .
'Reply-To: '. $name . '<' . $email . '>' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$subject = 'Reservation for room #' . $room;
$message = 'Name: ' . $name . '
Room: #' . $room . '
Check in: ' . $checkin . '
Check out: ' . $checkout . '
Number of people: ' . $people . '
Email: ' . $email . '
Phone: ' . $phone . '
' . $message;
$to = $blog_email;
// Send mail
mail($to,$subject,$message,$headers);
// END OF EMAIL TO RESERVATION CREW
// ---------------------
exit();
?>
The URL for the form is here - http://www.sunriselodging.com/reservation/. Apparently the form hasn't been sending emails either. Can someone please look at this and advise what's wrong with the code here?
I'll also appreciate if someone can direct me towards a good online PHP course for beginners (great if free).
Thank You,
Aadi
The whole problem with this here snippit is that you have 1) no conditions 2) no mail() function.
If you are going to use a header, you should probably have a condition. You should also probably have a condition to execute the emailer. Finally, you need to run this code before everything on the page (likely before the get_header() if on the page). Your workflow should go something similar to this:
<?php
// I assume you want the form to first process the email then forward
// so you need to check that the form was submitted
// Then check that the customer email is valid
if(isset($_POST['reservation-email']) && filter_var($_POST['reservation-email'], FILTER_VALIDATE_EMAIL)) {
// Loading variables from form
$blog_name = $_POST['blog_name'];
$blog_email = $_POST['blog_email'];
$reservation_email_subject = $_POST['reservation_email_subject'];
$reservation_email = $_POST['reservation_email'];
$reservation_email_switch = $_POST['reservation_email_switch'];
$room = $_POST['reservation-room'];
$checkin = $_POST['reservation-checkin'];
$checkout = $_POST['reservation-checkout'];
$people = $_POST['reservation-people'];
$name = $_POST['reservation-name'];
$email = $_POST['reservation-email'];
$phone = $_POST['reservation-phone'];
$message = $_POST['reservation-message'];
// Company email
$master = 'my#email.address';
// Use a from address
$header = "From: $email"."\r\n";
// Try mailing off the reservation to the hotel
if(mail($master,'Reservation Request',$message,$header)) {
// If success, send another email to the customer
$header = "From: no-reply#hotel.com"."\r\n";
mail($email,'Reservation Confirmation',$message,$header);
// What is the difference between this "reservation_message" and "reservation-message"??
$reservation_message = $_POST['reservation_message'];
// Redirect back
header('Location: '.$_POST['reservation-url'].'?message='.$reservation_message);
exit;
}
else {
echo 'Sorry, your reservation failed. Please contact customer service for reservations.';
exit;
}
}
?>
I was able to solve this after some research on Stack Overflow.
I added urlencode to the last string on the line containing header. I made:
Header('Location: '. $_POST['reservation-url'].'?message='.$reservation_message);
to look like this:
Header('Location: '. $_POST['reservation-url'].'?message='.urlencode($reservation_message));
To enable the mail function to send email I changed line 54 from
$headers = 'From: '. $name . '<' . $email . '>' . "\r\n" .
to this
$headers = 'From: Booking#hotel.com' . "\r\n" .
& line 70 from
$to = $blog_email;
to
$to = 'my#email.address';
Hope this will help anyone who's stuck in a situation similar to mine. A big THANK YOU to all the people who helped.

PHP Problem Sending Mail with headers

I have a contact form that sends 2 mails one to the user and the other to the admin.
The email that is sent to the user has no problems, yet the other is not working and giving a success code.
Here is my code:
This is the config file (emails and contents removed):
$adminEmail = "*********#*****.com";
$adminSubj = "Admin Subject";
$userSubj = "User Subject";
////////////////////////////////////////////////////////
//DON`T EDIT BELOW THIS LINE
////////////////////////////////////////////////////////
$userName = $_GET['name'];
$userEmail = $_GET['email'];
$userPhone = $_GET['phone'];
$userMessage = $_GET['message'];
$adminHeaders = 'MIME-Version: 1.0' . "\r\n";
$adminHeaders .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";;
$adminHeaders .= "To: " . $adminEmail . "<" . $adminEmail . ">" . "\r\n";
$adminHeaders .= "From: " . $userName ."<" . $userEmail . ">" . "\r\n";
$userHeaders = 'MIME-Version: 1.0' . "\r\n";
$userHeaders .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$userHeaders .= "To: " . $userName . "<" . $userEmail . ">" . "\r\n";
$userHeaders .= "From: website <no-reply#website>" . "\r\n";
And this is the file that sends the mail:
include("config.php");
$userSendMessage = "Your Message:<br />
$userMessage<br /><br /><br />
We will get back you.
<br /><br />
Thanks
Website Team.
";
$adminSendMessage = "
$userName<br />
$userEmail<br />
$userPhone<br />
$userMessage";
$toUser = mail($userEmail,$userSubj,$userSendMessage,$userHeaders);
$toAdmin = mail($adminEmail,$adminSubj,$adminSendMessage,$adminHeaders);
if($toAdmin && $toUser)
echo 1;
else
echo 0;
I guess that the problem is in the $adminHeaders
$adminHeaders = 'MIME-Version: 1.0' . "\r\n";
$adminHeaders .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";;
$adminHeaders .= "To: " . $adminEmail . "<" . $adminEmail . ">" . "\r\n";
$adminHeaders .= "From: " . $userName ."<" . $userEmail . ">" . "\r\n";
when I use the headers as this it works:
$adminHeaders = 'MIME-Version: 1.0' . "\r\n";
$adminHeaders .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";;
$adminHeaders .= "To: " . $adminEmail . "<" . $adminEmail . ">" . "\r\n";
$adminHeaders .= "From: Someone <someEmail#someServer.com" . "\r\n";
Any idea how to send the mail to the admin by the user email?
The hard work has been done for you. There are many libraries that do what you're needing with my favorite being here:
http://phpmailer.worxware.com/
It's easy to use, very robust and keeps the spam from webforms down quite well.
You can consider using PHP Mailer.
It can be used as a wrapper for the mail() function, and it's that easy:
<?php
require_once('PHPMailer_v5.1/class.phpmailer.php');
$mail = new PHPMailer(); // defaults to using php "mail()"
$body = file_get_contents('contents.html');
$mail->SetFrom($fromAddress, 'First Last');
$mail->AddReplyTo($replyToAddress, "First Last");
$mail->AddAddress($toAddress, "John Doe");
$mail->Subject = "PHPMailer Test Subject via mail(), basic";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->MsgHTML($body);
// $mail->AddAttachment("images/phpmailer.gif"); // attachment
// $mail->AddAttachment("images/phpmailer_mini.gif"); // attachment
if (!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
Some mail domains have checks to ensure the sender is not pretending to be some other domain. The from address is checked if no sender is specified.
This may be fixed by adding another header:
Sender: someone#yoursendingserver.com
Also please note your script is vulnerable to header manipulation as it's not properly escaped. This can be only easily be done in PHP 5.3 by using quoted_printable_encode e.g.
'From: ' . quoted_printable_encode($userName . '<' . $userEmail . '>');
earlier PHP versions don't have a simple way of doing this correctly without a library.
I agree with the other answers that you should look at using a library to do this, however I don't suggest you use php mailer as its no longer in active development (although it might be feature rich).
I use the mail class that comes from Zend Framework (as I use the framework as well):
http://framework.zend.com/manual/en/zend.mail.introduction.html
Apache Zeta components (formerly ez components), does a good implementation:
http://qafoo.com/blog/011_apache_zeta_components_doing_mail_right.html
When I write scripts like these, I always check whitespace and eliminate as much as I possibly can so they headers are sent correctly. Otherwise, glance at the examples on the mail function.
I noticed that you have 2 square brackets at the end of the line below when the adminHeaders are set.
$adminHeaders .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";;
When you changed the headers to the following:
$adminHeaders .= "From: Someone <someEmail#someServer.com" . "\r\n";
You missed a closing brace after the "Someone" email address.
In more of the examples on the manual page, they show the "To" and "From" parameters only as the email address. Try removing the braces and duplication of the email all together. You may also want to secure your values from the $_GET variable, which could potentially be exploited. It may also be worth debugging what the $_GET variables are giving you for the values of $userName and $userEmail (this can be as simple as echoing or vardumping the variables).
I was also facing the same issue and after some R&D i found that if we eliminate the newline characters from the message field then our PHP mail function start working
for e.g. The previous message which i was using --
$message = '<html>
<head>
<title>App Approval</title>
</head>
<body>
<p>Hi amir!</p>
<table>
<tr>
<td>Amir Khan has created a new app HH</td>
</tr>
<tr>
<td>Click the following link in order to view the app.</td>
</tr>
</table>
</body>
</html>
';
after changing it to
$message = '<html><head><title>App Approval</title></head><body><p>Hi amir!</p><table><tr><td>Amir Khan has created a new app HH</td></tr><tr><td>Click the following link in order to view the app.</td></tr></table></body></html>';
It started working.
Also if you are confused regarding how to use variables in message content in between single qoutes then my suggestion is to just use it as double quotes
as for this
$message = 'blah blah blah';
use this
$message = 'blah'.$blah.' blah';

Categories