Sending email from html form using php [duplicate] - php

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 6 years ago.
Ive been trying this out the whole day but I cant figure out how to send an email from my html contact form containing the information from the form to my email address. Im new to php.
Ive tried running this by uploading it to free web hosting. I get the message "success!" when I press the submit button on my html form but no email is actually sent.
Any help is appreciated.
PHP script:
<?php
//Subject
$subject ="Contact Form Submission";
// Name
$name =$_POST['InputName'];
// Message
$message =$_POST['InputMessage'];
//Mail of Sender
$email =$_POST['InputEmail'];
//From
$header = "From:$name<$email>";
$send_contact=mail("myemail#gmail.com",$subject,$message,$header);
//Check if mail was sent
if($send_contact){
echo "Success!";
}
else {
echo "Error!";
}
?>
EDIT: Figured it out after one whole day of trial and error. The problem was with the free web host I was using. Changed hosts and the code started working fine. Hope this helps someone in the future. Thanks all for the help.

I have a pretty good idea why your code is not working. It happened to me a long time ago. The reason why your code is not working is because :
When you pass "from" in headers, php expects an existing email account of your
server. For example : $headers = 'From: emailacc#yourserver.com';
So first thing you gotta do is create an email account on your server. And then put the From in header to the email address that you've just created.
The From field in the $headers is not the From as you think.
<?php
$email = $_POST["InputEmail"];
$subject = $_POST["InputSubject"];
$message = "From: ".$email.", ".$_POST["InputMessage"]; // you put the email address from the input form here
$headers = 'From: emailacc#yourserver.com'; // here is the email address specified from which u want to send the email.(i.e. your server email address)
mail($to, $subject, $message, $headers)
?>
I'm sure this will do the job :)

Have a shot at this.
I changed you're $header variable around a little bit, so that rather than setting the email as "$email", It'll actually pass through the posted email entered in the form. This apply's to the name too.
I also made it so that you pass the mail function through the parameters of the if statement, rather than setting a new variable.
$headers = "From: " . $name . "<" . $email . ">"; // notice new concatenation
if(mail("myemail#gmail.com", "Contact Form Submission", $message, $headers)){
// success message
} else {
// error message
}
Really hope this helps! :)

Try adding spaces after the "=" that might be the problem,
If that doesn't work you could try to use this
<?php
$emailvariable = $_POST['InputEmail']
$to = 'example#gmail.com';
$subject = "Form"
$message = $_POST['InputMessage'];
$headers = "From: $emailvariable";
mail($to, $subject, $message, $headers);
?>
Hope this helps

Related

send private copy of autoresponder mail

I have some php here that works great. I want a mail to be sent to the user who submits a form and I also want a copy of that mail sent to myself but I don't want my email address to be made available to the user.
Here's the php I'm using to govern the mail sending ...
$to = 'xxxx#xxxx.com' . ', ';
$to .= $email;
$subject = 'xxxx';
$message = "Thank you for submitting the form.";
$headers = "From: xxxx#xxxx.com\r\nReply-To: xxxx#xxxx.com";
$mail_sent = #mail( $to, $subject, $message, $headers );
echo $mail_sent ? "Mail sent" : "Mail failed";
When the code is parsed emails are duly sent to both the users submitted email ($email) and the address I enter in the first $to variable however the user can see the email address I enter as another recipient when they receive the email. Anyone know how I can get around this? Any help will be much appreciated. Thanks.
Use a BCC header instead of an additional To in your $headers string. It stands for "Blind Carbon Copy", and instructs the mail server to duplicate the mail to extra recipients, but remove that header from the original copy, so the main recipients can't know it was there.

php email script for just email address

Im new to this PHP stuff so please excuse my ignorance
Im after having just one input text box in my flash website where a person just enters there email address and at the click of a button it sends an email to me to a pre defined email address with a predefined subject heading and the email address that was entered in the body of the email
Anyone know of any links or can give some help
all the ones i have found want names email subject message and so on
Any help is appreciated
Mark
EDIT
ok I have the following
In flash I have an input text converted to a movieclip called "addy". Inside the movie clip which has the inputbox which has the variable name "emailaddy"
A Button called "email"
The code i Have running when "email" is clicked is
on (release) {
form.loadVariables("email.php", "POST");
}
the email.php script is as follows
<?php
$sendTo = "mark#here.co.uk";
$subject = "Subscribe to Website";
$headers = "From: Website";
$headers .= "<" . $_POST["addy"] . ">\r\n";
$headers .= "Reply-To: " . $_POST["addy"] . "\r\n";
$headers .= "Return-Path: " . $_POST["addy"];
$message = "Please Subscribe me to Website";
mail(recipient, subject, message, other headers);
mail($sendTo, $subject, $message, $headers);
?>
when I click the button nothing happens
what im after is when the button is clicked for and email to be sent in the following format
To: "mark#here.co.uk"
From: email address specified in text field "addy"
Subject: "Subscribe to Website";
body: "Please subscribe me to Website"
Your help is greatly appreciated
mark
The following code might help:
<?php
$user_mail=$_POST["mail"]; //or $user_mail=$_GET["mail"]; Set to your convenience!
$to_mail="abcde#xyz.com"; //Change to your email address
$message="New user's Email: ".$user_mail; //Change to your requirements
$subject="New user registered"; //Change to your preferred subject
$from="registration#yourwebsite.com"; //Change to your website mail id
mail($to_mail,$subject,$message,"From: $from\n");
To know more about the mail function, please see the documentation.
Well in depth you can do like this
<?php
if($_POST){
$userEmail = $_POST0["emailaddy"]; // textbox variable name comes here
$to = 'abc#xyz.com'; //write down here your email
$subject = 'Subscribe to website'; // your subject goes here
$message = 'Please Subscribe me to Website'; // Mail body message
$headers = 'From: ' . $userEmail . "\r\n" .
'Reply-To: ' . $userEmail . "\r\n" .
'Return-Path: ' . $userEmail; //can send x-Mailer also
mail($to, $subject, $message, $headers);
}else{
echo "Invalid Request";
return false;
}
Don't forget to check first $_POST is happened or not. No need to send Return-path instead of this use x-Mailer which sound good for other mail service providers.
To know more about this read documentation here.
If you want to connect to an SMTP server ,like Postfix or gmail there is a neat php library called PhpMailer.
It is well documentated, so you should be good to go by googleing it :)

PHP Sending Email Error

I'm trying to send an email using PHP code, I'm sure the code is right but I'm not receiving any emails. Can anyone see a problem with this code:
<?php
$to = 'i7906890#bournemouth.ac.uk';
$subject = 'Registration Complete';
$message = 'Thank you for joining us at Arsenic & Vice';
$header = 'From: admin#arsenicandvice.co.uk';
if (mail($to, $subject, $message, $header)){
echo('<p>Sent</p>');
} else {
echo('<p>Fail</p>');
}
?>
I've tested your code on my server and it works fine, I've received the e-mail.
I've also run it so you can receive it on i7906890#...
The problem is on your hosting. Either your e-mail server (qmail...) has a long queue or it is stuck. Contact your server admin.

How do I send an email to the user on submit of php form

How do I send an email to the user with the data they submitted in the form that includes a little message using there name and thanking them on submit of php form.
Here is my current php code. It currently just shows them a message that says there name and that the message has been sent and then sends me an email to my email address.
<?php
if(isset($_POST['submit'])){
$to = "benlevygraphics#gmail.com";
$headers = "From: " . $_POST['email'];
$subject = "Ben, you have been contacted...";
$body = "Name: " . $_POST['name'] . "\nEmail: " . $_POST['email'] . "\nWebsite: " . $_POST['web'] . "\nMessage: " . $_POST['message'];
if(mail($to, $subject, $body, $headers)){
echo("<p class=contactformsent>".$_POST['name'].", your message has been sent!</p>");
}
else{
echo("<p class=contactformnotsent>".$_POST['name'].", Message delivery failed...</p>");
}
}
?>
I am new to php and I have read stuff online and I still don't understand so if you could be clear in your examples or help I would greatly appreciate it very much. Thanks!
Assuming your current code is already working fine, you can do this to send yourself an email together with the recipient:
Set $to to $_POST['email']
Set $headers to "From: {$_POST['email']}\r\nBcc: benlevygraphics#gmail.com"
Adjust $body and $subject to your needs.
Btw, I can't say this often enough; make sure that your page has some form of CSRF protection.
How to properly add CSRF token using PHP
The above is just one way, there are others, just search for it :)
Look into your php.ini beacuse you have to enter a SMTP Server.
At my file it begins in line 1087 with "[mail function]"

php email - $to multiple recipients

Even this must have been be asked many times, I will ask again since I cannot get it to work.
I am using php mail($to, $subject, $message, "From: $mysite<$myemail>\nX-Mailer:PHP/" .phpversion()); to send email to a single recipient.
Now I need to sent it to more than one recipients. I know that normaly I could do:
$to = "emailA#here.com,emailB#there.com";
But I need the one of the recipients to be the user that fills in the form e.g.:
//get all form details
$email = $_POST['email'];
$to = "$email,emailB#there.com";
The above ($to) I don't know if it is correct or not but is not working for me...
If I leave only the $to = "$email"; it gets send to $email (meaning that my rest of the code is ok).
Any suggestion on what is or may be wrong here?
Thank you.
Add a CC to your header.
$header ="From: $mysite<$myemail>" . PHP_EOL;
$header .= 'CC: emailB#there.com' . PHP_EOL;
//Rest of headers here

Categories