This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 7 years ago.
I am trying to send an email from php. The mail sends successfully but is never delivered. The code is below..
if(isset($_POST["Referred"])) {
if(isset($_POST["RequestedEmail"])) {
$messagebody="You are invited to join Fasttask. please click on the link below \n http://www.fasttask.net/EmailRequestRedirect.php?User_Id=".$_SESSION['user_id'];
$To=$_POST["RequestedEmail"];
if(mail($To,"Join Fasttask", $messagebody)) {
$query="Update monthlygiveaway set TotalReferrals=TotalReferrals+1 where UserId=?";
$stmt=$mysqli->stmt_init();
$stmt->prepare($query);
$stmt->bind_param('i', $UserId);
$UserId=$_SESSION['user_id'];
$stmt->execute();
echo"Successfully sent mail:".$To;
} else {
echo "Failed to send mail";echo $_SESSION['user_id'];
}
}
}
Everything goes fine.. and the echo message is delivered to callin php function. I checked so the $To variable does have the address of the recipient. So please help me to trace the issue. :)
Have you try header parameter? may be it works.
$to = 'nobody#example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster#example.com' . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
For more detail http://php.net/manual/en/function.mail.php
Related
i am having a problem to do a webhook with the facebook api on a lead ads form.
Last day it was working like a charm. On the 'Lead Ads Testing Tool' i received a status = success. Now i received a status = failure, error code = 102, error message = server failure
image of the error 102
Anyone know this issue ?
here my webhook.php :
if (isset($_GET['hub_verify_token'])) {
if ($_GET['hub_verify_token'] === 'your verify token') {
echo $_GET['hub_challenge'];
return;
} else {
echo 'Invalid Verify Token';
return;
}
}
$input = json_decode(file_get_contents('php://input'), true);
$emailfrom = "test#test.com";
$emailto = "my#email.com";
$subject = "You've got a new submission Test";
$headers = "From: " . $emailfrom . "\r";
$headers .= "Reply-To: ". $emailfrom . "\r";
$headers .= "MIME-Version: 1.0\r";
$headers .= "Content-Type: text/html; charset=utf-8\r";
$body = 'hello';
mail($emailto, $subject, $body, $headers);
and the callback_url is good = "success": true image graph api subscription to webhook
Thanks
This question already has answers here:
How can I send an email using PHP?
(20 answers)
Closed 7 years ago.
At the bottom of my site there is a "send message" button. I want it to take the message and contact information(email, name) and send it to my email address. How could I possibly do this? I'm new at this website stuff by the way.
You can use PHP mail function.
<?php
if (!empty($_POST)) {
$name= $_POST['name'];
$mail_id= $_POST['email_id'];
$email= '';//email address on which you want to receive website details
$message_field = $_POST['message'];
$header = "MIME-Version: 1.0" . "\r\n";
$header .= "Return-Path: \r\n";
$header .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$header= "From: TriaaHousing";
$message="Name:".$name."\r\n";
$message .=" ";
$message .= "EmailId:".$mail_id."\r\n";
$message .=" ";
$message .= "Message:".$message_field;
if(mail($email, "Subject", $message, $header)){
echo 1;
}else{
echo 0;
}
}
?>
Use can use default PHP's mail() functionality OR you can use PHPMailer (Mail send helper). Both are safe and correct. but if you need some other stuff then use PHPMailer.
1. Using PHP's mail() function it's possible. Remember mail function will not work in Local server.
<?php
$to = 'nobody#example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: from#example.com' . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
?>
NOTE: You need to config SMTP on your local server if you are using SMTP. Take a look at this similar post.
2. You could also use PHPMailer class at https://github.com/PHPMailer/PHPMailer .
It allows you to use the mail function or use an smtp server transparently. It also handles HTML based emails and attachments so you don't have to write your own implementation.
Here is an example from the page above:
<?php
require 'PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp1.example.com;smtp2.example.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'user#example.com'; // SMTP username
$mail->Password = 'secret'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable encryption, 'ssl' also accepted
$mail->From = 'from#example.com';
$mail->FromName = 'Mailer';
$mail->addAddress('webmaster#example.com', 'Webmaster User'); // Add a recipient
$mail->addAddress('webmaster#example.com'); // Name is optional example
$mail->addReplyTo('info#example.com', 'Information');
$mail->addCC('cc#example.com');
$mail->addBCC('bcc#example.com');
$mail->WordWrap = 50; // Set word wrap to 50 characters
$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$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';
}
Use addReplyTo, addCC, addBCC if require.
Hope this help you well!
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
i just wanted to ask if there is any way possible for the details inputted in a form field to be forwarded to a cell phone number as a text message.
Here is the code i have for form to email, so if there are any changes i have to make to this code, you can let me know.
<?php
if(isset($_POST['email'])) {
$email_to = "you#yourdomain.com";
$email_subject = "Your email subject line";
function died($error) {
// your error code can go here
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}
// validation expected data exists
if(!isset($_POST['first_name']) ||
!isset($_POST['last_name']) ||
!isset($_POST['email']) ||
!isset($_POST['telephone']) ||
!isset($_POST['comments'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$first_name = $_POST['first_name']; // required
$last_name = $_POST['last_name']; // required
$email_from = $_POST['email']; // required
$telephone = $_POST['telephone']; // not required
$comments = $_POST['comments']; // required
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email_from)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$first_name)) {
$error_message .= 'The First Name you entered does not appear to be valid.<br />';
}
if(!preg_match($string_exp,$last_name)) {
$error_message .= 'The Last Name you entered does not appear to be valid.<br />';
}
if(strlen($comments) < 2) {
$error_message .= 'The Comments you entered do not appear to be valid.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details below.\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "First Name: ".clean_string($first_name)."\n";
$email_message .= "Last Name: ".clean_string($last_name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Telephone: ".clean_string($telephone)."\n";
$email_message .= "Comments: ".clean_string($comments)."\n";
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
#mail($email_to, $email_subject, $email_message, $headers);
?>
<!-- include your own success html here -->
You will need to use an external provider to be able to send text messages to cellphones.
A few examples are Twilio and plivo
You will need to connect to their API. However, it may depend on your country what service you are able to use.
There are several API providers that let you send SMS to cellphones over the internet.
For example
https://www.twilio.com/sms
They have a simple API that you can use with your preferred programming language to send SMS over the web to cellphones.
Just use the values submitted from your form and feed it to the API objects and submit them to Twillio.
In my PHP code, I have tried to make it so that after the user successfully sends an email, it redirects back to the contact page and then displays a popup box informing the user that the email has been successfully sent: however, the program only ever runs the top line, and does not run the popup box code. How do I fix this?
<? php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: #';
$to = '#';
$subject = 'Customer Message - Online Form';
$body = "From: ".$name.
"\r\n E-Mail: ".$email.
"\r\n Message: \r\n".$message;
if (isset($_POST['submit'])) {
if (mail($to, $subject, $body, $from)) {
header('Location: http://www.google.com.au');
echo "<script>alert('Message successfully sent.');</script>";
} else {
echo '<p>Something went wrong, go back and try again!</p>';
}
}
?>
You are redirecting, by setting your header, before you print your alert. Try redirecting in the JS.
<? php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: #';
$to = '#';
$subject = 'Customer Message - Online Form';
$body = "From: ".$name.
"\r\n E-Mail: ".$email.
"\r\n Message: \r\n".$message;
if (isset($_POST['submit'])) {
if (mail($to, $subject, $body, $from)) {
echo "<script>alert('Message successfully sent.'); window.location='http://www.google.com.au'</script>";
//header('Location: http://www.google.com.au');
} else {
echo '<p>Something went wrong, go back and try again!</p>';
}
}
?>
Note
This will cause a JS alert before switching pages. If you want to alter on the page you land on you will want to send either a $_POST or $_GET variable and listen for it on the page you land on
You can't redirect the user away from the page and expect the page to still display something to the user.
If you want to display a message on a different page than store it in session and then, on the other page, check if something is stored in session and display that.
you are redirecting using your headers, if you try to echo first and then use header to redirect you will get an error, saying the headers are allready set.
You could add the redirect to your frontend, by returning something else like
if (isset($_POST['submit'])) {
if (mail($to, $subject, $body, $from)) {
echo '<script>
alert("Message successfully sent.");
location.href="http://www.google.com.au";
</script>';
} else {
echo '<p>Something went wrong, go back and try again!</p>';
}
}
the location redirect should wait for the alert to be dismissed, before executing
I have one web page. In that I have contact form.
What I want that after submit the form it should redirect on same page and display success message(message should be fadeout after few seconds) and the post data of form should be clear. In form i am also cheking verification code and sending mail.
if(md5($_POST['vcode']).'a4xn' == $_COOKIE['tntcon'])
{
$msg="message";
$to = 'test#gmail.com';
$subject = 'subject';
$message = $msg;
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .="From: ".$_POST['email']." \r\n";
$mail=mail($to, $subject, $message, $headers);
if($mail==true)
{
echo '<script>$("#deletesuccess").fadeIn();$("#deletesuccess").delay(4000).fadeOut();window.location="index.php";</script>';
//here i want to redirect on same page with clear post data and display success message.
}
}
else
{
echo '<script type="text/javascript">document.getElementById("disp_wrong_code").style.display="inline";</script>';
// This is verification code error.
}
This is custom php page.
For redirecting you can use header function. For storing data between page loads use $_SESSION. Some simplified example:
session_start();
if ($can_do_post)
{
// do some actions here
$mail=mail($to, $subject, $message, $headers);
if ($mail==true) {
// set SESSION to know that it's success result
$_SESSION['form_filled'] = true;
// redirect to the same page
header("Location: /script.php");
die();
}
}
else
{
// no post data here
// check session:
if (isset($_SESSION['form_filled']) && $_SESSION['form_filled']) {
echo '<script>$("#deletesuccess").fadeIn();$("#deletesuccess").delay(4000).fadeOut();window.location="index.php";</script>';
}
}