How to trigger mail function when submit button clicked - php

I'm trying to make an application to send emails when a button is clicked. I tried to use isset function to do that. But my code doesn't work well. How do I fix it?
Demo below:
<!-- <button name="sendemail">send</button> -->
<input type="submit" value="send"/>
<?php
if(isset($_POST['Submit'])){
?>
<?php
// multiple recipients
// $to = 'aidan#example.com' . ', ';
// $to .= 'wez#example.com';
$to="example#outlook.com";
// subject
$subject = 'test01';
// message
$message = '
<html>
<head>
<title>Birthday Reminders for August</title>
</head>
<body>
<p>Here are the birthdays upcoming in August!</p>
</body>
</html>
';
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'To: Mary <mary#example.com>, Kelly <kelly#example.com>' . "\r\n";
$headers .= 'Cc: birthdayarchive#example.com' . "\r\n";
mail($to, $subject, $message, $headers);
}
?>

You can use the below code to fire an email on click of the Send Email button
<form action="" method="post">
<input type="submit" value="Send details to college" />
<input type="hidden" name="button_pressed" value="1" />
</form>
<?php
if(isset($_POST['button_pressed']))
{
$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);
echo 'Email Sent.';
}
?>

A couple of things to address here. First, your input button doesn't do anything, because it isn't a part of a form. So you either have to use some sort of JavaScript or just a HTML form to submit the button to, so we can process it in PHP. A simple form is done like this below. Note the metohd="POST", this allows us to gather the information from the $_POST-array in PHP later on (by default forms are set to use GET).
<form method="POST">
<input type="submit" name="submit" value="Send!" />
</form>
This form will send the data over the POST-array, to the same page it's placed on, so place your PHP here as well.
Secondly, you did not have a name attribute in your submit-button, and since PHP uses the names rather than the IDs (like JavaScript does), you need to add this. This can be seen in the code-snippet above.
Further, you never check if the mail was actually sent or not. You should never take anything for granted.
<?php
if (isset($_POST['submit'])) {
/* add headers, messages and such here */
// Check if the mail is being sent from the server
if (mail($to, $subject, $message, $headers)) [
// Mail was sent! Great!
} else {
// It failed sending the mail
}
}
?>

You'll need to set the name attribute for your input element, and the input element need to be in a form. Also, the $_POST['submit'] should be in lowercase to keep it consistent.
You should also check to see if mail() is sent successfully. mail() will return true when sent and false if not sent.
Just a note, the actual sending of mail will depend on your hosting/ server, even if it returns true, it does not necessarily mean that the mail is sent.
Find out more about mail() at http://php.net/manual/en/function.mail.php.
The following code should work:
<form method="post">
<input type="submit" name="submit" value="send" />
</form>
<?php
if(isset($_POST['submit'])){
// NO Change from your original code
if(mail($to, $subject, $message, $headers)) {
// You can change this to whatever you want PHP to do when mail is successfully sent
echo 'Mail successfully sent';
} else {
// You can change this to whatever you want PHP to do when mail failed to send
echo 'Mail failed to send';
}
?>
Hope this helps. Thanks!

Related

Sending PDF File with PHP Email

I'm trying to use the following code to try and send a PHP Email with a PDF attachment.
I get the message 'Mail Sent. Thank You'. However, No email is being sent.
Please could someone show me / explain to me where I am going wrong?
Thanks
<?php
if(isset($_POST['submit'])){
$to = "example#example.com"; // this is your Email address
$from = $_POST['email']; // this is the sender's Email address
$subject = "Subject Here";
$message = $_POST['message'];
$file = $_POST['upload'];
$headers = 'From: someone#gmail.com ' . "\r\n" .
'Reply-To: someone#gmail.com ' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$headers2 = "From:" . $to;
mail($to, $subject, $message, $file, $email, $headers);
echo "Mail Sent. Thank you.";
// You can also use header('Location: thank_you.php'); to redirect to another page.
}
?>
<!DOCTYPE html>
<head>
<title>Form submission</title>
</head>
<body>
<form action="" method="post">
Message:<br><textarea rows="5" name="message" cols="30"></textarea><br>
<input type="file" name="upload" accept="application/pdf,application/vnd.ms-excel" />
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
There are lot of issues with your code, and it is recommended to first check the syntax and documentation on official site. For instance your are passing $file as parameter to mail function, but there is no FILE parameter in mail function (https://www.php.net/manual/en/function.mail.php)
Then you are not saving the uploaded PDF to any disk to send from. Uploaded files are captured through $_FILES variable not $_POST variable. You need to see that too.
Then sending any file is tricky job, and your job can be ease by using some library function such as PHPMailer so try to refer those.

How to create contact form and mail to the owner of website [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 5 years ago.
I want to get customers details through contact form and that would be sent to my mail id. so I am trying to code but its not working. I have tried by watching YouTube videos
This is my HTML Code
<!DOCTYPE html>
<html>
<head>
<title>Form</title>
</head>
<body>
<form action="send.php" method="POST">
Name: <input type="text" name="name" required=""></input><br><br>
Mobile: <input type="text" required="" name="mobile"></input><br><br>
Message: <textarea rows="10" cols="50" name="mes" required=""></textarea><br><br>
<input type="submit" value="Send Mail"></input>
</form>
</body>
</html>
This is my PHP Code
<?php
$name =$_POST['name'];
$mobile =$_POST['mobile'];
$mes =$_POST['mes'];
mail("imjeevajk#gmail.com","Contact From Site",$mes,"From: $name\r\n","Mobile: $mobile\r\n");
echo "Thanks for Contacting Us";
?>
mail function as described here http://php.net/manual/en/function.mail.php
Example:
<?php
$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);
?>
So the header "From" must be an email address while you are putting a custom name.
How to get mail function delivery mail to 'to' address please read here
It is mostly correct.
You can't pass Mobile as the additional parameter, that will do nothing.
I have changed the script to append the mobile to the message body.
To set a from address, you need to have a from email, just a name by itself won't work.
Also depending on your php configuration and mail server configuration, you may not be able to change the from address and it will result in an error, or be ignored.
<?php
if ($_POST) {
$name = $_POST['name'];
$mobile = $_POST['mobile'];
$mes = $_POST['mes'];
// append mobile to message
$mes .= "\r\nMobile: $mobile\r\n";
$from_email = "imjeevajk#gmail.com';
mail("imjeevajk#gmail.com", "Contact From Site", $mes, "From: $name <$from_email>\r\n");
// mail("imjeevajk#gmail.com", "Contact From Site", $mes, "From: $name\r\n", "Mobile: $mobile\r\n");
echo "Thanks for Contacting Us";
exit;
}
Please set submit name field and update the php mail function as mentioned below.
<input type="submit" name="sendMail" value="Send Mail"></input>
<?php
if (isset($_POST['sendMail'])) {
$to = 'admin#dummyemail.com';
$subject = 'Contact From Site';
$from = "From: ".$_POST["name"]."\r\n";
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: '.$from."\r\n".
'Reply-To: '.$from."\r\n" .
'X-Mailer: PHP/' . phpversion();
$message = '<html><body>';
$message = "Name: ".$_POST["name"]."";
$message .= "Mobile: ".$_POST["mobile"]."";
$message .= "Message: ".nl2br($_POST["mes"])."";
$message .= '</body></html>';
if(mail($to, $subject, $message, $headers)){
echo 'Thanks for Contacting Us.';
} else{
echo 'Unable to send email. Please try again.';
}
}
?>
Better use PHP Mailer
some servers providers block mail function and that can be reason why it doesn't work.

PHP message sends "undefined"

I have mail.php which sends out an email. This is the code I'm using.
<?php
if(isset($_POST['submit'])){
$to = $_POST['email'];
$subject = 'Your Results!';
$message = $_POST['message'];
$headers = 'From: example#example.com' . "\r\n" .
'Reply-To: example#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
//mail($to, $subject, $message, $headers);
//header("Location: thankyou.php");
echo $message;
}
?>
The contents of the email is supposed to the .innerText of of a div I have on my index.php page. If I echo out the message, then the output appears the page (mail.php) but when I comment/uncomment the necessary parts to email myself the message I receive is just "undefined".
Is $message not defined?
Here is the form and javascript I'm using
<form action="mail.php" method="post" onsubmit="this.message.value = document.getElementById('box').innerText;">
<input type="email" name="email" required>
<input id="content" type="hidden" name="message" value="">
<input type="submit" name="submit">
</form>
Change .innerText to .textContent. .innerText is a nonstandard property that doesn't exist in Firefox.
I don't know why you didn't have the same problem when used echo $message in the PHP script, unless you tested that version of the script with a different browser (always try to minimize the number of differences when you're testing like this).

PHP mail() function is not sending

I am trying to send an email using PHP
I'm using this form:
<form class="pure-form pure-form-aligned" action="contact.php">
<fieldset>
<input type="Text" placeholder="Nome" name="Name">
<input type="email" placeholder="Email" name="email">
</fieldset>
<fieldset>
<button type="Reset" class="pure-button pure-button-primary">Limpar</button>
<button type="submit" class="pure-button pure-button-primary">Enviar</button>
</fieldset>
</form>
and this php code
<?php
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$field_name = $_POST['cf_name'];
$field_email = $_POST['cf_email'];
$mail_to = 'nelson.maia#geralucros.com';
$subject = 'GeraRelax: '.$field_name;
$body_message = 'De: '.$field_name."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$headers = 'De: '.$field_email."\r\n";
$headers .= 'Responder para: '.$field_email."\r\n";
$mail_status = #mail($mail_to, $subject, $headers);
if ($mail_status) { ?>
<script language="javascript" type="text/javascript">
alert('Obrigado pela contacto.');
window.location = 'index.php';
</script>
<?php
}
else { ?>
<script language="javascript" type="text/javascript">
alert('Envio falhado');
window.location = 'index.php';
</script>
<?php
}
?>
I have used it before but now it doesn't work. I'm guessing this have to do with server configurations?
Can someone enlighten me on this?
You need to add the form METHOD!
<form class="pure-form pure-form-aligned" action="contact.php" METHOD="POST">
Also missing . to add to headers.
$headers .= 'De: '.$field_email."\r\n";
Also type in NOME should be NAME
<input type="Text" placeholder="Nome" name="Name">
TYPO IN VARIABLE NAMES
$field_name = $_POST['name'];
$field_email = $_POST['Email'];
Your problem is your have created mail incorrectly, if you view the PHP manual: http://php.net/manual/pt_BR/function.mail.php // http://php.net/manual/en/function.mail.php
you will see that mail($to, $usbject, $message, $headers); and your version does not do this, you have no $message value in your mail item.
Also add
error_reporting(E_ALL);
ini_set('display_errors', 1);
To the top of your code to display error messages which will give you more feedback as to why your email sending is failing. And remove the # from your mail function until you are sure it works.
You should have
error_reporting(E_ALL);
ini_set('display_errors', 1);
...
$mail_status = mail($mail_to, $subject, $body_message, $headers);
Recommendation:
Use UTF-8 character set, or UTF-16 if possible.
Use a dedicated Mailer extension such as PHPMailer or Swift Mailer
Add error tracking (as exampled) across your code until you're sure it works.
Eat five fresh fruit and vegetables a day.
Also noted from PHP Mail:
When sending mail, the mail must contain a From header. This can be
set with the additional_headers parameter, or a default can be set in
php.ini.
Failing to do this will result in an error message similar to Warning:
mail(): "sendmail_from" not set in php.ini or custom "From:" header
missing. The From header sets also Return-Path under Windows.
Add the form method here:
<form class="pure-form pure-form-aligned" action="contact.php">
Add the submit button name here:
<button type="submit" name="submit" class="pure-button pure-button-primary">Enviar</button>
Change the php code to this :
<?php if(isset($_POST['submit'])){
// your code here..
.
.
.
// change the mail headers..
$headers .= 'De: '.$field_email."\r\n";
} ?>

Sending a message with name of current page in one click

I have a site that contain more than 10000 videos, and sometimes there is embeded video that doesnt work, i need to show to my visitors this message "click here if the video doesnt show anymore" and when they click to this message , i will recieve a notification from the page that has been clicked
I didnt found any joomla extension that can do this
If i put this php code in every page of my joomla website
<form action="" method="post">
<input type="submit" value="Send details to embassy" />
<input type="hidden" name="button_pressed" value="1" />
</form>
<?php
if(isset($_POST['button_pressed']))
{
$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);
echo 'Email Sent.';
}
?>
It will send me a message in one click.
How can i receive a mail containing the name of page where the user has clicked?
In Joomla with this html code we can obtain the name of the current page.
<? $doc =& JFactory::getDocument();
echo $doc->getTitle();?>
Something like
$message = 'Page:' . $doc->getTitle();
should do the trick.

Categories