Sending PDF File with PHP Email - php

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.

Related

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.

Simple PHP mail form not working on server

I've written an simple PHP mail form but it refuses to run on one server but will on another. Both servers are running PHP 5.4 and other PHP progs run on the problematic server but mail will not send.
I've pared the mailer back to the simplest code but am stumped as why one server wont send the mail. Is there a configuration I need to activate or a test I can perform to diagnose the problem?
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Simple PHP Mailer</title>
</head>
<body>
<form action="" method="post">
First Name: <input type="text" name="first_name"><br>
Last Name: <input type="text" name="last_name"><br>
Email: <input type="text" name="sender_email"><br>
Message:<br><textarea rows="5" name="message" cols="30"></textarea><br>
<input type="submit" name="subdata" value="Submit">
</form>
<?php
//Taken from: http://stackoverflow.com/questions/18379238/send-email-with-php-from-html-form-on-submit-with-the-same-script
if(isset($_POST['subdata'])){
$to = 'myname#gmail.com'; // this is your Email address
$from = $_POST['sender_email']; // this is the sender's Email address
$fname = $_POST['first_name'];
$lname = $_POST['last_name'];
$subject = "PHP Email Form Test";
$message = $fname . ' ' . $lname . ' (' . $from . ') wrote the following:' . '\n\n' . $_POST['message'];
$headers = 'From: user#gmail.com' . "\r\n" .
'Reply-To: user#gmail.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
if (mail($to, $subject, $message, $headers)) {
echo 'Mail Sent. Thank you ' . $fname . ', we will contact you shortly.';
}else{
echo 'Mail Error';
}
}
?>
</body>
</html>
Any help would be appreciated, many thanks.
Kw

How to trigger mail function when submit button clicked

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!

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).

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