Retrieve values from HTML form - php

I have a mail sending functionality in php code, I want to use define to set to address and from address.How can this be done.
The code is
<?php
$subject = 'Test email';
$message = "Hello World!\n\nThis is my first mail.";
$headers = "From: $from\r\nReply-To: webmaster#example.com";
//send the email
$mail = #mail( $to, $subject, $message, $headers );
?>
How to define $to and $from. Thanks in Advance for help

Unless it is absolutely imperative, I would recommend using a good old fashioned variable for this particular task, not a constant.
If you do want to use a constant:
define('MAIL_TO', 'mailto#gmail.com');
define('MAIL_FROM', 'mailfrom#gmail.com');
$subject = 'Test email';
$message = "Hello World!\n\nThis is my first mail.";
$headers = "From: " . MAIL_FROM . "\r\nReply-To: webmaster#example.com";
$mailResult = mail(MAIL_TO, $subject, $message, $headers);
FYI:
// Constants can also be retrieved with the constant() function
$mailTo = constant('MAIL_TO');
// ...which is the same as...
$mailTo = MAIL_TO;
With use of constants:
$mailTo = 'mailto#gmail.com';
$mailFrom = 'mailfrom#gmail.com';
$subject = 'Test email';
$message = "Hello World!\n\nThis is my first mail.";
$headers = "From: " . $mailFrom . "\r\nReply-To: webmaster#example.com";
$mailResult = mail($mailTo, $subject, $message, $headers);

Here's a very basic example. I'll leave it up to you to do validation.
HTML:
<form action="path/to/your/script.php" method="post">
<input type="text" name="from" />
<input type="submit" value="Send" />
</form>
PHP:
In PHP, you'll want to use $_REQUEST, $_POST or $_GET depending on the action parameter of the form in your HTML. If you're not sure, use $_REQUEST. The value that goes in the square brackets is the name of attribute of the input from your HTML.
define('TO_ADDRESS', 'user#example.org');
$headers = "From: " . $_REQUEST['from'] . "\r\nReply-To: webmaster#example.com";
$mail = mail(TO_ADDRESS, $subject, $message, $headers);

Related

Changing CGI-Mailer in PHP mail script [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 5 years ago.
I have a php script that sends an email from an HTML form. The issue is that the sender shows as CGI-Mailer in my inbox.
How can I set the sender address to be that of the sender and not CGI-Mailer?
<?php session_start();
if(isset($_POST['Submit'])) {
$youremail = 'info#complexny.com';
$fromsubject = $_POST['fname'];
$subject = $_POST['fname'];
$fname = $_POST['fname'];
$url = $_POST['url'];
$mail = $_POST['mail'];
$phone = $_POST['phone'];
$headers = "From: $mail \n";
$headers .= 'X-Mailer: PHP/' . phpversion();
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=iso-8859-1\n";
$message = $_POST['message'];
$to = $youremail;
$subject = ''.$fromsubject. ' is interested in a project with you.';
$body = '
Client: '.$fname.'
Phone Number: '.$phone.'
URL: '.$url.'
E-mail: '.$mail.'
Message:
'.$message.'
';
echo "<p style='text-align:center'>Thank you for your feedback. We will be in contact shortly.<br/>Continue to <a href='/'>The Company/a></p>";
mail($to, $subject, $body);
} else {
echo "You must write a message. </br> Please go to <a href='/contact.php'>Contact Page</a>";
}
?>
You are not passing the additional_headers parameter to the mail function. Change the line with the call to mail to:
mail($to, $subject, $body, $headers);
I would suggest using PHPMailer rather than mail(). You can see how to do what you're after in the answer to this question: Setting replyTo field in email
Quoting from that question:
$this->phpmailer->AddReplyTo($replyEmail,$fromName); //this is email2#example.com
$this->phpmailer->SetFrom($fromEmail, $fromName); //this is email1#example.com
You can find more info on PHPMailer here: https://github.com/PHPMailer/PHPMailer

sending mail from php when link in message body

i have a php script
<?php
$to = 'somebody#somedomain.com';
$subject = 'Test mail';
$message = 'mysitedomain.com';
$from = 'support#mysitedomain.com';
$headers = 'From:' . $from;
mail($to,$subject,$message,$headers);
echo 'Mail Sent.';
?>
When i run this code mail not send. If i change message to mysitedomaincom (without dot before com) the mail send succesfull.
Anybody have a solution for this?
This codes that tells the mailer and the recipient that the email contains well formed HTML that it will need to interpret
If you want you can change content of $message, now with this codes you can send HTML content mail.
<?PHP
$to = 'somebody#somedomain.com';
$subject = 'Test mail';
$headers = "From: Support <support#mysitedomain.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>mysitedomain.com</h1>';
$message .= '</body></html>';
mail($to, $subject, $message, $headers);
?>

Html email not sending using php

I am trying to send email in html format using php file but each time i receive email with this html code instead of html format of text ... if i move header code above message its not sending email so i put it at bottom
please help ... thank you
<?php
$name = $_POST['name'];
$to = $_POST['to'];
$from = $_POST['from'];
$subject = $_POST['subject'];
$message = "
<html>
<head>
<body>
<h1>
<center>Meeting invitation</center>
</h1>
</body>
</head>
</html>
";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=ISO-8859-1\r\n";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
?>
You forget ".", for string
$headers = "From:" . $from;
must be
$headers .= "From:" . $from;
Please add this
$headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
Try below code..
<?php
$name = $_POST['name'];
$to = $_POST['to'];
$from = $_POST['from'];
$subject = $_POST['subject'];
$message = "
<html>
<head>
<body>
<h1>
<center>Meeting invitation</center>
</h1>
</body>
</head>
</html>
";
// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
// More headers
$headers .= 'From: <gaurav#example.com>' . "\r\n";
mail($to,$subject,$message,$headers);
?>
Here is working example of mail code in PHP:
<?php
if($_GET["submit"] = "Send" )
{
$namezz = $_GET["name"];
$emailzz = $_GET["email"];
$cityzz = $_GET["city"];
$phonenozz = $_GET["phoneno"];
$subj = $_GET["subject"];
$commentszz = $_GET["msg"];
}
ini_set("SMTP","www.cccccc.in");
// email address to whoon to send
$email = "ccc#gmail.com";
// The subject
if( $subj == "No-Subject")
{
$subj = "New Enquiry form form ". $namezz;
$subject = $subj;
}
else
{
$subject = $subj;
}
// The message
$msgg = "Name :".$namezz ."\n
Email :".$emailzz ."\n
City :".$cityzz ."\n
Phone Number :".$phonenozz ."\n
Subject:".$subj ."\n
Message :".$commentszz;
$headers = "from :".$emailzz;
mail($email, $subject, $msgg, $headers);
echo 'thanks for sending email';
?>
Hope this will help you.
You could something along the lines of
<?php
$val=mail('your email address', $_POST['subject'], $_POST['message']);
?>
<p>Your email has been sent.</p>
With the html page containing
<input type="text" id="inputEmail" placeholder="Email" name="subject">
<textarea id="textarea" rows="10" name="message" placeholder="Entermsg:"></textarea>
Using the mail function in PHP is simple and straight forward
In your code you'll need to modify
$headers .= "From:" . $from;
You forgot to put a . in this piece of code
$headers = "From:" . $from;
So this is your final code
$headers .= "From:" . $from;
Because the . is used to concatenate in PHP.

Can't get php mail() to work

I've got Mamp running on my mac and trying to get mail() to work.
This is what I've got to work with.
$to = 'mymail#gmail.com';
$subject = 'The subject!';
$message = 'Hi there!';
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= "X-Mailer: PHP/".phpversion();
$headers .= 'From: Test <test#test.com>' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
// Mail it
if(mail($to, $subject, $message, $headers))
{ print 'success!'; }
else
{ print 'fail!'; }
?>
It just keeps on returning false. Any idea what I'm doing wrong?
Some settings with php/apache I need to check?
if you using your snippet on localhost, put on server and then try.
php mail() function needs to be on sever if you want it to work. on localhost you always get fail!
Try this:
<?php
$Name = "Da Duder"; //senders name
$email = "email#adress.com"; //senders e-mail adress
$recipient = "PersonWhoGetsIt#emailadress.com"; //recipient
$mail_body = "The text for the mail..."; //mail body
$subject = "Subject for reviever"; //subject
$header = "From: ". $Name . " <" . $email . ">\r\n"; //optional headerfields
ini_set('sendmail_from', 'me#domain.com');
mail($recipient, $subject, $mail_body, $header);
?>
http://be.php.net/manual/en/function.mail.php
each line of text may not be bigger than 70 chars and needs to be cut off with a LF (\n)
EDIT: as #brad suggested: SwiftMailer is realy good!

php mail function

<?php
$sendto = "account#gmail.com";
$subject = "email confirmation"; // Subject
$message = "the body of the email - this email is to confirm etc...";
# send the email
mail($sendto, $subject, $message);
?>
this is the code that i wrote to test mail function on localhost.
i have ran the script in browser for several times and still dun receive any email in my mail box.
Do I need any additional configurations?
thx in advance!
Basically is hard to send a mail from localhost to any mail providers.
They have big restrictions on the incoming mails, and the simply mail() won't work.
You need to use an SMTP server.
and define that server in php configuration
smtp = localhost #(here should be your smtp server)
smtp_port = 25
if you don't have an SMTP server, try to pass all headers like in PHP examples:
$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);
http://www.php.net/manual/en/function.mail.php
You need to make sure that you have your PHP installation set up to use a working SMTP Server. You might find what you're looking for in answers to this question. Failing that you'll likely need to test your script on your live web server.
<?php
$name = $_POST['name'];
$visitor_email = $_POST['email'];
$message = $_POST['message'];
?>
<?php
$email_from = 'yourname#yourwebsite.com';
$email_subject = "New Form submission";
$email_body = "You have received a new message from the user $name.\n".
"Here is the message:\n $message"
?>
<?php
$to = "inspiretechpark#gmail.com";
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $visitor_email \r\n";
mail($to,$email_subject,$email_body,$headers);
?>
<?php
$to = "name1#website-name.com, name2#website-name.com,name3#website-
name.com";
mail($to,$email_subject,$email_body,$headers);
?>
<?php
$to = "name1#website-name.com, name2#website-name.com,name3#website-
name.com";
$headers = "From: $email_from \r\n";
$headers .= "Reply-To: $visitor_email \r\n";
$headers .= "Cc: someone#domain.com \r\n";
$headers .= "Bcc: someoneelse#domain.com \r\n";
mail($to,$email_subject,$email_body,$headers);
?>
Try This Guys..This Is For Sending Mail
Try this:
<?php
$sender = 'email#example.com';
$recipient = 'email#example.com';
$subject = "php mail test";
$message = "php test message";
$headers = 'From:' . $sender;
if (mail($recipient, $subject, $message, $headers))
{
echo "Message accepted";
}
else
{
echo "Error: Message not accepted";
}
?>
If you are working with localhost then, i hope it will never work. It will work only on a mail configured server. Please try on it.

Categories