PHP Mailer is not sending 'field input' - php

Sorry folks, but I think I have a little problem (easy to solve) but I am not getting the solution by myself. Could you help me out?
The problem I am facing is this:
- the PHP script (handled by PHP mailer) is sending an e-mail, but not the information people fill in at the form.
Here is the php script I use:
<?php
$to = "user#domain.com";
$subject = "Request";
$message = "<h1>Request</h1>";
$message .= "<strong>street</strong>: $_POST['street']";
$message .= "<strong>store</strong>: $_POST['store']";
$headers = "From: request#domain.com";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html\r\n";
$sent = mail($to, $subject, $message, $headers);
if( $sent = mail($to, $subject, $message, $headers) ){ echo "SENT"; } else { echo "There
was a problem"; }
?>
Thanks in advance!

<?php
$to = "user#domain.com";
$subject = "Request";
$message = "<h1>Request</h1>";
$message .= "<strong>street</strong>:".$_POST['street'];
$message .= "<strong>store</strong>:".$_POST['store'];
$headers = "From: request#domain.com";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html\r\n";
$sent = mail($to, $subject, $message, $headers);
if( $sent = mail($to, $subject, $message, $headers) ){ echo "SENT"; } else { echo "There
was a problem"; }
?>

There were a few problems with your code.
Your headers $headers .= "MIME-Version: 1.0\r\n"; were malformed and were adding 1.0 etc. to the From: therefore resulting in 1.0 <request#domain.commime-version>
Also, your mail() function was being executed twice, therefore sending 2 emails at the same time.
I fixed your headers including your mail() by removing $sent = mail($to, $subject, $message, $headers); and modifying the if condition.
Assuming this is what you are using for an HTML form which I used to test it with:
<form method="POST" action="mail_handler.php">
<p>
<label>Street:<br>
<input name="street" type="text">
</label>
</p>
<p>
<label>Store:<br>
<input name="store" type="text">
</label>
</p>
<input name="submit" type="submit" value="Submit" />
</form>
mail_handler.php
<?php
$to = "user#domain.com";
$subject = "Request";
$message = "<h1>Request</h1>";
$message .= "<strong>street</strong>:".$_POST['street'];
$message .= "<br>";
$message .= "<strong>store</strong>:".$_POST['store'];
$headers = "MIME-Version: 1.0\n";
$headers .= "Content-Type: text/html; charset=iso-8859-1\n";
$headers .= "From: user#domain.com" . "\r\n" .
"Reply-To: user#domain.com" . "\r\n" .
'X-Mailer: PHP/' . phpversion();
if(mail($to, $subject, $message, $headers) ){
echo "SENT"; } else { echo "There was a problem"; }
?>
More information on headers and the mail() functions can be found by visiting the PHP.net Website at the following URL: http://php.net/manual/en/function.mail.php

Related

How to put paragraphs inside PHP contact form?

I programmed a contact form in PHP and I want to include paragraphs in the message that gets send, so that the user would have to write in certain paragraphs and not just one block of text.
I've tried to split the message into multiple textareas, but I don't know how I should adjust the PHP code so that these textareas would get send inside one message in the right order. Each textareashould've presented one paragraph.
This is my PHP:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: mywebsite.com';
$to = 'myemailadress#adress.com';
$subject = 'subject line';
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html\r\n";
$headers .= 'From: '. $email. "\r\n" .
$headers .= "Reply-To: ". $email. "\r\n";
$headers .= "X-Mailer: PHP/" . phpversion();
$status = mail($to, $subject, $message, $headers);
if ($status) {
echo '<p style="color: white">Your Message was sent!</p>';
} else {
echo '<p style="color: white">Something went wrong. Please try again.</p>';
}
?>
This is my HTML:
<div class="contact-form">
<form id="contact-form" method="post" action="contact-form-handler.php">
<input name="name" type="text" class="form-control" placeholder="Your Name" required>
<br>
<input name="email" type="email" class="form-control" placeholder="Your Email">
<br>
<textarea name="message" class="form-control" placeholder="Message" rows="30" required></textarea><br>
<input type="submit" class="form-control" value="SEND MESSAGE">
</form>
</div>
https://www.php.net/manual/en/function.nl2br.php
When you use nl2br you can change the "\n" from the textarea to a "".
You can send a content-type: text/plain mail to and don't have to transform the newline characters into tags.
I don't see multiple textareas in your code so i guess nl2br is the function you're looking for.
I have add:
$message = nl2br($message); // Inserts HTML line breaks <br />
this should Insert HTML line breaks.
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: mywebsite.com';
$to = 'myemailadress#adress.com';
$subject = 'subject line';
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html\r\n";
$headers .= 'From: ' . $email . "\r\n" .
$headers .= 'Reply-To: ' . $email . "\r\n";
$headers .= 'X-Mailer: PHP/' . phpversion();
$message = nl2br($message); // <-- added to Inserts HTML line breaks.
$status = mail($to, $subject, nl2br($message), $headers);
if ($status) {
echo '<p style="color: white">Your Message was sent!</p>';
} else {
echo '<p style="color: white">Something went wrong. Please try again.</p>';
}

Sending email with inline image

My code for sending email
<?php
$to = 'piyu.mulay#gmail.com';
$subject = 'Website Change Request';
$headers = "From: chintamani.mulay#gmail.com \r\n";
$headers .= "CC: chintamani.mulay#gmail.com\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$message = '<html><body>';
$message .= '<h1>Hello, World!</h1>';
$message .= '<img src="../1.jpg">';
$message .= '</body></html>';
echo mail($to, $subject, $message, $headers);
?>
When i put link in <img src="http://weber-steinach.de/files/339349"> it works well but when I put path of file which is in my localserver I won't show and Image in email.
Finally I come to know how to do this. Here is the source link from where I come to know
$bound_text = "----*%$!$%*";
$bound = "--".$bound_text."\r\n";
$bound_last = "--".$bound_text."--\r\n";
$headers = "From: youremail#host.com\r\n";
$headers .= "MIME-Version: 1.0\r\n" .
"Content-Type: multipart/mixed; boundary=\"$bound_text\""."\r\n" ;
$message = " you may wish to enable your email program to accept HTML \r\n".
$bound;
$message .=
'Content-Type: text/html; charset=UTF-8'."\r\n".
'Content-Transfer-Encoding: 7bit'."\r\n\r\n".
'
<html>
<head>
<style> p {color:green} </style>
</head>
<body>
A line above
<br>
<img src="cid:http://localhost/a/img/1.jpg">
<br>
a line below
</body>
</html>'."\n\n".
$bound;
$file = file_get_contents("http://localhost/a/img/1.jpg");
$message .= "Content-Type: image/jpeg; name=\"http://localhost/a/img/1.jpg\"\r\n"
."Content-Transfer-Encoding: base64\r\n"
."Content-ID: <http://localhost/a/img/1.jpg>\r\n"
."\r\n"
.chunk_split(base64_encode($file))
.$bound_last;
echo mail($to, $subject, $message, $headers) ;
?>

Sending my html email

I know there is lots of information everywhere about this subject but I seem to be going around in circles and would greatly appreciate any help. I am trying to send a simple html email with some inline css. The email does not seem to arrive...
<?php
// Email the user their activation link
$to = "name#website.com";
$from = "me#website.com";
$subject = 'Test HTML Email';
$headers = "";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$message = '<div style="padding:10px; background:#000; font-size:46px; font-weight: 900; color:#fff; ">
My Website
</div>
<div style="padding:24px; font-size:17px; background:#DCDCDC ; ">
This is a message sent from my website
<br>
<br>
Login in to your account
<br>
<br>
</div>';
mail($to, $subject, $message, $headers);
?>
rewrite this code:
$headers = "";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
to this:
$headers = "";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$headers .= "To: ". $to. "\r\n";
$headers .= "From: ". $from;
Try to add this :
$headers .= "From: $from" . "\r\n" .
"Reply-To: $from" . "\r\n" .
'X-Mailer: PHP/' . phpversion();

echo form value doesn't pass to mail function

I have tried to search online for similar problem, but i just couldn't get it work, it's driving me nuts. haiz. your_email just couldnt pass.
The form
echo '<form class=location_submit name="togo" action="" method="post" >';
echo '<input class="user_email" type="text" name="your_email" placeholder="Enter your email here" required size="55"> <br />';
echo '<input class="location_submit_button" type="image" name="button" src="images/send_location_button.png" />';
echo '<input type="hidden" name="button_pressed" value="1" />';
echo '</form>';
The mail function
if(isset($_POST['button_pressed']))
{
$to = $your_email;
$subject = "Sent from Utourpia!";
$message = "Message body";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "From: Terence Leong # Utourpia <terence#utourpia.me>" . "\r\n";
$headers .= "Reply-To: terence#utourpia.me" . "\r\n";
$headers .= 'Bcc: terence#popby.me' . "\r\n";
$headers .= "X-Mailer: PHP/" . phpversion();
mail($to, $subject, $message, $headers, '-f terence#utourpia.me');
echo '<p class=email_sent>Email Sent.</p>';
}
Things worth to note is email sent does execute when i click the submit button, but $your_email simply returns NULL :((
you need to extract the values from the $_POST using their names;
$to = $_POST['your_email'];

empty spaces in form and other issues with my contacts page

I'm having some problems with my contact page.
Here are the parts:
<form method="post" action="mail.php">
<input name="nome" type="text" style="width: 265px;" placeholder="Nome e Cognome">
<input name="mail" type="email" style="width: 263px;" placeholder="E-mail">
<textarea name="messaggio" placeholder="Messaggio"></textarea>
<button type="submit" name="invia" style="margin-left: 0; margin-top: 10px;">Invia</button>
</form>
and..
<?php
$to = "mail";
$subject = "Modulo proveniente dal sito www.miosito.it";
$body = "Contenuto del modulo:\n\n";
$body .= "Nome: " . trim(stripslashes($_POST["nome"])) . "\n";
$body .= "Email: " . trim(stripslashes($_POST["mail"])) . "\n";
$body .= "Messaggio: " . trim(stripslashes($_POST["messaggio"])) . "\n";
$headers = "From: Valle srl <info#vallesrl.com>";
"Content-Type: text/html; charset=iso-8859-1\n";
if(#mail($to, $subject, $body, $headers)) {
header("Location: http://www.alessandrogiordano.me/test/valle02/sent.php");
} else {
header("Location: http://www.alessandrogiordano.me/test/valle02/nosent.php");
}
?>
First of all.. if I click on the submit button with all blank the email is sent blank.
Even if I make some errors and I get the else message the email is sent.. blank obviously.
I'm going crazy.. I'm making this website for free for a friend but I'm a graphic designer not a web developer. Never again! :D
Help!! Thank you very much.
put this line in your headers
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
Before sending mail you have to validate all the fields whether they are empty or not
Like this
if( trim($_POST["nome"]) != "" )
{
// send mail
$isMailSend = mail($to, $subject, $body, $headers);
}
{
//show error
}
if( $isMailSend ) {
header("Location: http://www.alessandrogiordano.me/test/valle02/sent.php");
} else {
header("Location: http://www.alessandrogiordano.me/test/valle02/nosent.php");
}
This
$headers = "From: Valle srl <info#vallesrl.com>";
"Content-Type: text/html; charset=iso-8859-1\n";
Should be,
$headers = "From: Valle srl <info#vallesrl.com>";
$headers. = "Content-Type: text/html; charset=iso-8859-1\n";
you can't use type="email",there should be type="text"

Categories