not able to send details via email [duplicate] - php

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 5 years ago.
$sql = "insert into book (uid,interest,tid,lid)values('$id','$interest','$tid','$lid') ";
$result = $conn->query($sql);
if($result)
{
echo"<script type='text/javascript'>
alert('added');
</script>";
$message = "Your have interest in ".$interest."";
$to=$email;
$subject="Booked for ".$title."";
$from = 'vkcvkc8#gmail.com';
$body="Booked for ".$title."located in".$location.".You will be charged with".$cost.
".Contact:".$contact."";
$headers = "From:".$from;
mail($to,$subject,$body,$headers);
}
else
{
echo"<script type='text/javascript'>
alert('error');
</script>";
}
not sending email
not sending emailnot sending emailnot sending emailnot sending emailnot sending emailnot sending email?????

You have to use smtp settings to be able to send the mail as most hosting providers now have closing the php mail function due to security reasons, this is the script for a proper smtp mail function and dont use third party tools like php mailer just ask your hosting provider to activate mail function in pear packages
<?php
require_once "Mail.php";
$from = "Web Master <webmaster#example.com>";
$to = "Nobody <nobody#example.com>";
$subject = "Test email using PHP SMTP\r\n\r\n";
$body = "This is a test email message";
$host = "SMTPhostname";
$username = "webmaster#example.com";
$password = "yourPassword";
$headers = array ('From' => $from, 'To' => $to, 'Subject' => $subject);
$smtp = Mail::factory('smtp', array ('host' => $host, 'auth' => true, 'username' => $username, 'password' => $password)); $mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
echo("<p>" . $mail->getMessage() . "</p>"); }
else
{ echo("<p>Message successfully sent!</p>");
}
?>

As mentioned - your code is at risk of sql injection so it would be wise to use a prepared statement as below.
try{
$sql='insert into book ( uid, interest, tid, lid ) values (?,?,?,?)';
$stmt=$conn->prepare( $sql );
if( $stmt ){
/*
assuming parameters are
-----------------------
uid=integer
interest=string
tid=integer
lid=integer
*/
$stmt->bind_param('isii', $id, $interest, $tid, $lid );
$result=$stmt->execute();
if( $result && $stmt->affected_rows==1 ){
$message = "You have interest in {$interest}";
$to=$email;
$from='vkcvkc8#gmail.com';
$subject="Booked for {$title}";
$body="{$message}\n\nBooked for {$title} located in {$location}.\n\nYou will be charged with {$cost}\n\nContact:{$contact}";
$headers=array();
$headers[]="MIME-Version: 1.0";
$headers[]="Content-type: text/plain; charset:utf-8";
$headers[]="To: {$to}";
$headers[]="From: {$from}";
$headers[]="Reply-To: {$from}";
$headers[]="X-Mailer: PHP/".phpversion();
$status = mail( $to, $subject, $body, implode( "\r\n", $headers ) );
throw new Exception( $status ? 'success - mail sent' : 'fail - mail not sent' );
} else {
throw new Exception('Failed to insert data');
}
} else {
throw new Exception('Unable to prepare sql query');
}
}catch( Exception $e ){
exit( "<script>alert('{$e->getMessage()}');</script>" );
}

Related

How to send two emails in a single contact form using PEAR Mail

I have a contact form and I want to receive message through mail and simultaneously send mail to the client. I have the below code it works some times. That is some times I get the message and client also get the message but sometimes only one email is sent either to client or to me but never both all the time.
I have seen example in the following link https://teamtreehouse.com/community/how-to-send-two-different-email-with-different-bodies-using-phpmailer but it uses PHP Mailer I want the same functionality but by using PEAR Mail.
I have seen other similar question over here PEAR Mail using gmail SMTP won't send 2 emails in sucession but the answer given in that question doesn't solve my problem
Below is the code which works some time.
<?php
require_once('Mail.php');
require_once('Mail/mime.php');
if (isset($_POST['Captcha'])) {
$name = filter_var($_POST['Name'] , FILTER_SANITIZE_STRING);
$email = filter_var($_POST['Email'] , FILTER_SANITIZE_EMAIL);
$phone = filter_var($_POST['Phone'] , FILTER_SANITIZE_STRING);
$budget = filter_var($_POST['Budget'] , FILTER_SANITIZE_STRING);
$timeline = filter_var($_POST['Timeline'] , FILTER_SANITIZE_STRING);
$description = filter_var($_POST['Description'] , FILTER_SANITIZE_STRING);
$spam = filter_var($_POST['usernameoftest'] , FILTER_SANITIZE_STRING); // Bot trap
if($spam)
{ // If the hidden field is not empty, it's a bot
die("No spamming allowed bitch!");
} else {
$from = "sales#ganeshghate.com";
$to = "ganeshghate#hotmail.com";
$subject = "Message from : ".$name;
$body = "Client's Name : ".$name."\r\n";
$body .= "Client's Email ID : ".$email."\r\n";
$body .= "Client's Phone Number : ".$phone."\r\n";
$body .= "Client's Budget : ".$budget."\r\n";
$body .= "Client's Timeline : ".$timeline."\r\n";
$body .= "Client's Message : ".$description."\r\n";
$host = "myhost";
$port = "myport";
$username = "myusername";
$password = "mypassword";
$crlf = "\n";
$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject);
$mime = new Mail_mime($crlf);
$mime->setTXTBody($body);
$body = $mime->get();
$headers = $mime->headers($headers);
$smtp = Mail::factory('smtp',
array ('host' => $host,
'port' => $port,
'auth' => true,
'username' => $username,
'password' => $password));
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
$output["errorclientemail"] = "<div>
<h2 class='error'>".$mail->getMessage().$mail->getUserInfo()."</h2>
<h2>Oops there was some error we have not received your message</h2>
<h2>Please report the above error to us via skype , email , phone details are on the left side</h2>
</div>"."\r\n";
} else {
$output["successclientemail"] = "Thank you we will get back to you soon";
}
$bodyback = "We have received your message :".$body."\r\n";
$bodyback .= "We will get back to you soon"."\r\n";
$bodyback .= "With Regards "."\r\n";
$bodyback .= "Ganesh Ghate "."\r\n";
$subjectback = "Thank You for contacting us";
$headersreply = array ('From' => $from,
'To' => $email,
'Subject' => $subjectback);
$crlf1 = "\n";
$mime = new Mail_mime($crlf1);
$mime->setTXTBody($bodyback);
$bodyback = $mime->get();
$headersreply = $mime->headers($headersreply);
$smtp = Mail::factory('smtp',
array ('host' => $host,
'port' => $port,
'auth' => true,
'username' => $username,
'password' => $password));
$mail = $smtp->send($email, $headersreply, $bodyback);
if (PEAR::isError($mail)) {
$output["errorreplyemail"] = "<div>
<h2 class='error'>".$mail->getMessage().$mail->getUserInfo()."</h2>
<h2>Oops there was some error we have not delivered confirmation email to you</h2>
<h2>Please report the above erorr to us via skype , email , phone details are on the left side</h2>
</div>"."\r\n";
} else {
$output["successreplyemail"] = "We have sent confirmation email to you. Please check your inbox / junk mail";
}
echo json_encode($output);
}
}
?>
Thanks in advance

My php mail function is not working?

Ive got the following php code:
<?
if ($_POST['emailme']) {
$yemail = $_POST['email'];
$host = $_SERVER['HTTP_HOST'];
$email = "<autoreply#$host>";
if (mail('$yemail', 'This is a Subject', 'This is the body of the email', 'From: $email')) {
echo "Message sent!";
} else {
echo "Message failed sending!";
}
}
?>
This is my HTML:
<FORM METHOD="POST" ACTION=""><INPUT TYPE='TEXT' CLASS='BOX' NAME='email' /><INPUT TYPE='SUBMIT' NAME='emailme' CLASS='SUBMITBOX' VALUE='Send!' /></FORM>
Any ideas why its not sending the email ? it says Message Sent but im not receiving any emails in my inbox
All help is much appreciated, thanks
PS: ive tried the following (with double quotes):
if (mail("$yemail", "This is a Subject", "This is the body of the email", "From:" . $email)) {
echo "Message sent!";
} else {
echo "Message failed sending!";
}
but still no luck
try this
if (mail('akh40#hotmail.co.uk', 'This is a Subject', 'This is the body of the email', 'From:'. $email))
Apparently most hosting companies are dropping support for php's mail() function in favour of SMTP versions of email scripts.
See http://www.thesitewizard.com/php/protect-script-from-email-injection.shtml for an explanation of why.
Try this for an SMTP/SSL script:
<?php
require_once "Mail.php";
$from = "Sandra Sender <sender#example.com>";
$to = "Ramona Recipient <recipient#example.com>";
$subject = "Hi!";
$body = "Hi,\n\nHow are you?";
$host = "ssl://mail.example.com";
$port = "465";
$username = "smtp_username";
$password = "smtp_password";
$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject);
$smtp = Mail::factory('smtp',
array ('host' => $host,
'port' => $port,
'auth' => true,
'username' => $username,
'password' => $password));
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
echo("<p>" . $mail->getMessage() . "</p>");
} else {
echo("<p>Message successfully sent!</p>");
}
?>

sending email through php not working

I have a piece of code that will not work i don't understand why, i am trying to get the information sent to the database and also sent to the email address, it is saving it to the database but will not send to the email, even though it echoes your email was successfully sent. Any help appreciated.
<?php
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
$to = "edonaghy--#hotmail.co.uk";
$subject = "Dress fitting";
$time = $_REQUEST['time'];
$date = $_REQUEST['date'];
$headers = "From: $email";
$sent = mail($to, $date, $headers);
if($sent)
{
print "Your mail was sent successfully";
}
else
{
print "We encountered an error sending your mail";
}
mysql_select_db("lr", $con);
mail("edonaghy--#hotmail.co.uk", $time, $date, $place, $comments);
$sql="INSERT INTO fitting (time, date, place, comments) VALUES ('$_POST[time]','$_POST[date]','$_POST[place]','$_POST[comments]')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "Information has been sent!";
?>
First of all, it wont send your e-mail if your localhost werent properly configured, so I suggest you to test on your server if you have one.
There are libraries that can help you with a better code like php-simple-mail
Example:
require 'class.simple_mail.php';
$mailer = new SimpleMail();
$time = $_REQUEST['time'];
$date = $_REQUEST['date'];
$message = "<strong>My message on date:".$date." ".$time."</strong>";
$send = $mailer->setTo('edonaghy--#hotmail.co.uk', 'Your Email')
->setSubject('Test Message')
->setFrom('no-reply#domain.com', 'Domain.com')
->addMailHeader('Reply-To', 'no-reply#domain.com', 'Domain.com')
->addMailHeader('Cc', 'bill#example.com', 'Bill Gates')
->addGenericHeader('Content-Type', 'text/html; charset="utf-8"')
->setMessage($message)
->setWrap(100)
->send();
echo ($send) ? 'Email sent successfully' : 'Could not send email';
You're using mail wrong.
It's meant to be used like this:
mail(to,subject,message,headers,parameters)
So yours would be:
$to = "email#hotmail.co.uk";
$subject = "Dress fitting";
$headers = "From: email#hotmail.co.uk";
$message = "Time:".$_REQUEST['time']."\r\n";
$message .= "Date:".$_REQUEST['date']."\r\n";
mail($to,$subject,$message,$headers);
This is assuming your $_REQUEST's are working.
Your second mail() is using variables that you haven't even set yet ($comments, $place) etc.
Set them first:
$place = $_POST['place'];
$comments = $_POST['comments'];

Cannot get email from query string

I'm having trouble getting an email address from $_GET.
Here is my code:
<?php
$eadd = $_GET['email'];
echo("<p>Please check your inbox on your email $eadd.</p>");
?>
I went to this link:
http://localhost/file.php?email=myemail#company.com
Yet the output is only:
Please check your inbox on your email .
EDIT
Here's my complete code :
<?php
require_once "Mail.php";
//Get link posted info's needed
//Email
$eadd = $_GET['email'];
$from = "OtakuJam Registration <no-reply#comp.com>";
$to = " $unick < $eadd >";
$subject = "Thank you for registering";
$body = "Dear $unick ,
\n Thank you for registering to OtakuJam. To activate your account, \n
";
$host = "mail.srv.com";
$username = "name#comp.com";
$password = "mypass";
$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject);
$smtp = Mail::factory('smtp',
array ('host' => $host,
'auth' => true,
'username' => $username,
'password' => $password));
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
echo("<p>" . $mail->getMessage() . "</p>");
} else {
echo("<p>Please check your inbox on your email $eadd.</p>");
}
?>
`Please ignore the $unick there, also the mail won't send unless i put an email on the code instead of < $eadd >
turning my $to syntax to $to = $unick . "<" . $eadd . ">"; as #Charlie gave me helped me. Also to those who told me to use ".$variable.". to my echo helped. thanks

sending mail from my account. Not webhosts

I got a contact form that succesfully sends emails to me.
heres the extract:
$_POST['message'] = wordwrap($_POST['message'], 70);
mail ('myemail#test.com', $_POST['subject'], $_POST['message'] , $_POST['email']);
echo "<div class='registertext'>Your email was succesfully sent to a member of the administration team. Please wait 24 hours for as to reply and ensure you check your junk mail!<br />To login please click <a href='login.php'>here</a></div>";
The issue I have is, the email gets sent from my host. Not a email I want to specify. How would I overcome this?
You can specify it in the email headers:
$recipient = "recipient#test.com";
$from = "You#yoursite.com";
$replyTo = "You#yoursite.com";
$subject = "Hi!";
$text = "<p>This is a test!<p>";
$headers = "MIME-Version: 1.0\r\n"
."Content-Type: text/html; charset=utf-8\r\n"
."Content-Transfer-Encoding: 8bit\r\n"
."From: =?UTF-8?B?". base64_encode([Your Name]) ."?= <$from>\r\n"
."Reply-To: $replyTo\r\n"
."X-Mailer: PHP/". phpversion();
//send it!
if (mail($recipients, $subject, $text, $headers, "-f $from")){
echo "sent";
} else {
echo "did not send";
};
but there's a good chance it will get caught be SPAM filters. Your best bet in this case would be to use a PHP mailing library that handles SMTP emailing and uses your actual account to send mail (there are several packages that can handle this for you: Pear Mail, and PHP Mailer amongst others.
You can use PEAR mail which will use a SMTP account. Here is some code from my mail form I use
$from = "Name <webmaster#domain.com>";
$to = "Name <address#domain.com>";
$subject = "Subject";
$body = 'A message!';
$host = "ssl://domain.com";
$port = "465";
$username = "username";
$password = "password";
$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject);
$smtp = Mail::factory('smtp',
array ('host' => $host,
'port' => $port,
'auth' => true,
'username' => $username,
'password' => $password));
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
echo("<p>" . $mail->getMessage() . "</p>");
} else {
echo("<p>Message successfully sent!</p>");
}

Categories