InMotion Hosting Support Center

You can use the PHP mail() function to send an email with PHP. The simplest way to do this is to send a text email. This is one way to handle sending you the results when a visitor to your website fills out a form.

Basic PHP email() fuction code

Below is the code for the baic email function. We can take the script and actually use a form on our website to set the variables in the script above to send an email.

<?php
//if "email" variable is filled out, send email
  if (isset($_REQUEST['email']))  {
  
  //Email information
  $admin_email = "someone@example.com";
  $email = $_REQUEST['email'];
  $subject = $_REQUEST['subject'];
  $comment = $_REQUEST['comment'];
  
  //send email
  mail($admin_email, "$subject", $comment, "From:" . $email);
  
  //Email response
  echo "Thank you for contacting us!";
  }
  
  //if "email" variable is not filled out, display the form
  else  {
?>

 <form method="post">
  Email: <input name="email" type="text" /><br />
  Subject: <input name="subject" type="text" /><br />
  Message:<br />
  <textarea name="comment" rows="15" cols="40"></textarea><br />
  <input type="submit" value="Submit" />
  </form>
  
<?php
  }
?>

So let’s now review what the form is actually doing.

1. The first part checks to make sure the email input field is filled out. If it is not, then it will display the HTML form on the page. If the email is in fact, set (after the visitor fills out the form), it is ready to send.

2. When the submit button is pressed, after the form is filled out, the page reloads and reads that the email input is set, so it sends the email.

Keep in mind, this is a basic tutorial to explain how to use the mail() function in PHP. Using the method, exactly the way it is, can be insecure and should not be used on your website. This tutorial is aiming to provide you the basic of how to use phpmail() and for further use, you may want to look into securing your code to possible hacks.

To learn more about the PHP email function, please see the article on How to create a custom PHP contact form with more information on validation and error checking. If you need further assistance please feel free to ask a question on our support center.

Support Center Login

Social Media Login

   
Social Login Joomla

Related Questions

Here are a few questions related to this article that our customers have asked:
Emails sent from my website have the server name in them
Can I get a code that I want to send an email from my created website
any form php send email
I'm trying to get a Cron script to work
Would you like to ask a question about this page? If so, click the button below!
Ask a Question
n/a Points
2014-04-10 3:26 am

Hi , 

This code is working properly and display ed the msg as Thank you for contacting us but mail is not send to the correspomding mail address.. pls give any suggestions...

Staff
9,968 Points
2014-04-10 4:56 am
Hello Sankar,

It's possible that the message is ending up in a spam folder in the recipient's email address due to the mail coming directly from a PHP script.

If that is the case, you could use something like phpMailer in order to send an SMTP authenticated email.

Either that or there is some incorrect information typed in to your script. The Thank you for contacting us! message is displayed if the email form is submitted, but it doesn't actually check to ensure that the mail was delivered. If you have access to the server's mail logs, you should be able to see if the PHP mail attempt is making it to the local server to even attempt a delivery out.

It looks like we might need to update the example script used in this guide to at least wrap the code with if (!mail( ...)){ echo "Failure" } code to see if the server accepts the PHP mail attempt. However this is more just a proof of concept then something you'd probably want to use on a live contact form for your website.

- Jacob
n/a Points
2014-04-11 12:07 am

Hi Jacob, 

 i was alredy used the phpmailer code it was shown the error of Message was not sent.Mailer error: SMTP connect() failed.. 

Staff
9,968 Points
2014-04-11 12:20 am
Hello Sankar,

If you're getting a SMTP connect() failed error, then this means your PHP script can't establish a SMTP connection with the server you've entered into your script.

Are you possibly trying to connect to a secure mail server by chance? You might need some additional settings in your phpMailer script such as:

$mail->SMTPSecure = 'tls';
$mail->Host = "secure.example.com";


If you're still having issues let us know.

- Jacob
n/a Points
2015-04-10 7:23 pm

please am facing the challenge in using php to send mail. this is my code

<?php 

 error_reporting(0);

 

 $to = $_REQUEST['sendto'] ; 

 $from = $_REQUEST['Email'] ; 

 $name = $_REQUEST['Name'] ; 

 $headers = "From: $from"; 

 $subject = "Web Contact Data"; 

 

 $fields = array(); 

 $fields{"Name"} = "Name"; 

 $fields{"Company"} = "Company"; 

 $fields{"Email"} = "Email"; 

 $fields{"Phone"} = "Phone"; 

 $fields{"list"} = "Mailing List"; 

 $fields{"Message"} = "Message"; 

 

 $body = "We have received the following information:\n\n"; foreach($fields as $a => $b){ $body .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]); } 

 

 $headers2 = "From: noreply@YourCompany.com"; 

 $subject2 = "Thank you for contacting us"; 

 $autoreply = "Thank you for contacting us. Somebody will get back to you as soon as possible, usualy within 48 hours. If you have any more questions, please consult our website at www.oursite.com";

 

 if($from == '') {print "You have not entered an email, please go back and try again";} 

 else { 

 if($name == '') {print "You have not entered a name, please go back and try again";} 

 else { 

   $send = mail('user@example.com', $subject, $message, $headers); 

   mail($to, $subject, $body, $headers); 

$send2 = mail($from, $subject2, $autoreply, $headers2); 

 if($send) 

 {header( "Location:localhost/thankyou.html" );} 

 else 

 {print "We encountered an error sending your mail, please notify webmaster@YourCompany.com"; } 

 }

}

 ?> 

 
Staff
17,726 Points
2015-04-13 9:55 am
Hello Jones,

Thank you for your question. I recommend reading our guide on How to create a custom PHP contact form with validation, since it provides a successful example.

If you have any further questions, feel free to post them below.

Thank you,
John-Paul
n/a Points
2015-07-16 9:12 am

Sir,

     The mail is not sent with my mail() function coding that is"<?php 

 $to = "someone@example.com";

  $subject = "ABCD";

  $comment = "Abcdefgh";

  $header = "From:das.dhananjay@gmail.com \r\n";  

  //send email

  $send=mail($to, $subject, $comment, $header);

if($send)

{

echo "Message has been successfully sent";

}

?>

 ".

What will be correct mail() function.

Sir, Please advice me.

Staff
17,726 Points
2015-07-16 1:44 pm
Hello Dhananjay,

Thank you for contacting us. Have you tried following the above guide? We also provide a helpful example in our guide How to create a custom PHP contact form with validation.

Thank you,
John-Paul
n/a Points
2015-04-22 9:08 pm

alter the line mail($admin_email, "$subject", $comment, "From:" . $email); to

mail($email, "$subject", $comment, "From:" . $admin_email);

it will work.

n/a Points
2015-07-08 11:48 pm

nice post

n/a Points
2014-04-25 2:13 am

mail is not send to the correspomding mail address.. pls give any suggestions...and i m a new devoloper to php socan u sugest me how to use

 phpMailer script...

 

Staff
11,186 Points
2014-04-25 7:52 am
for information on using phpMailer, I recommend that you review our article on using phpMailer to send mail using PHP.
n/a Points
2014-07-02 3:30 am

how to work SMTP i Dont no can anyone explais pls.... advance thank

Staff
29,164 Points
2014-07-02 7:38 am
Hello Darshan,

SMTP settings are explained here. If you need to send an SMTP authenticated message you may want to check out the phpmailer article.

Kindest Regards,
Scott M
n/a Points
2014-05-13 9:23 pm

Just wanted to mention an issue I just ran across in the hopes that it might help someone else.  In our situation we use inmotion for web hosting, but host email for the same domain locally in our network. When the domain was set up on inmotion, thedefault setting was to use InMotion's MX server, so all email sent to address on our domain were attempted delivery to inmotion rather than our mail server.  I think I've fixed this by setting the mx servers to be remote instead of local.

Staff
9,968 Points
2014-05-14 5:44 pm
Hello Nathan, and thanks for the comment!

Yes you are correct that if your domain's email is not hosted with us, when you try to deliver from a PHP script it will try to deliver it locally unless you change DNS records to have email hosted by a 3rd party.

You also need to set the Email Routing option to Remote Mail Exchanger instead of the local one like you mentioned.

Thanks again for your comment!

- Jacob
n/a Points
2015-08-28 2:53 am

hiii sir please give me a mail code with calling database..

please fast reply please.... sir.....

Staff
5,084 Points
2015-08-28 11:21 am
Hello Snehal,

While we do not help provide custom solutions such as that you may find the following article helpful to go along with the PHP code in this article.

Best Regards,
TJ Edens
n/a Points
2014-06-27 5:28 am

Please, i am sending email via phpmail() with a link, the link work perfectly but did not display any information posted to the link from sql. And even the email is not  entering into inbox of my client.

can you help me? 

Staff
29,164 Points
2014-06-27 9:09 am
Hello Olorunnisola,

If you are not getting any errors or messages saying the email was not sent, but the recipient is not able to see the email, you will want to contact our Live Support team to see if they can confirm the email actually left the server. If it did, then the script worked properly and the issue maybe on the receiving end. If not, they may be able to see something specific to advise you on a next step.

Kindest Regards,
Scott M
n/a Points
2014-06-27 5:33 am

Please someone teach me how to create ds text formats and attached files form computer to a textarea on a webpage.

Staff
29,164 Points
2014-06-27 9:12 am
Hello Olorunnisola,

We are unfamiliar with what you mean by ds text format. Could you be more specific in what you are trying to accomplish?

Kindest Regards,
Scott M
n/a Points
2014-06-27 10:27 am

Thank you scott, 

here are my codes:

    //var_dump($link);

//$msg = wordwrap($msg,70);

   $subject = "Call For Meeting";

   $message = "Dear"." "."Sir\Madam,".

"You had been invited to attend a meeting by the name organization"." ".$org_ind." "."that is to hold on"." ".$date." "."by"." ".$start_time." "."via online, Based on"." ".$meeting_title."."."Your feedback about this proposed meeting will be of better help for the preparation.

Please click to give your view."." "."<a href='http://exolvetechnologies.com/meeting/Notification.php?call_for_meeting.php = $meetingId'>Notification</a>"." "."Thank you.<br>";

// 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";

$headers .= 'From: Gentlesoul <emmanuelabiodun2011@live.com>'. "\r\n";

   foreach($_POST['division_name'] as $key =>$email)

  {

      $mail = mail($email,$subject,$message,$headers);

      //var_dump($email);

        if ($mail) {

      echo "<p>Message sent!</p>";

     } else {

       echo "<p>Message delivery failed...</p>";

     }

  }

The project is my own personal project that i want to host by tuesday, please help me.

have not being here before, this will be my first time.

About the text format! I mean to give client opportunity to bold,italic or number list to typing text has he or she type.

 

 

   echo "<h4>Meeting set successful!</h4>";

}

n/a Points
2014-06-27 10:33 am

on my own system here, if i view my mail send has a text, i saw everything.

But on other peoples laptop, they saw the message and the link but inside the link all the field i call from mysql server to that notification page was not showing.

Staff
17,726 Points
2014-06-27 12:00 pm
Hello Olorunnisola,

Thank you for contacting us today. Since you are having trouble coding a PHP contact form, I recommend following our updated guide on How to create a custom PHP contact form with validation.

That tutorial provides a successful example that should help you code your form.

If you have any further questions, feel free to post them below.
Thank you,

-John-Paul
n/a Points
2014-06-30 4:35 pm

I am using Wordpress WooCommerce and my order confirmation emails are not being sent. I am using an email forward to send mail (example@mydomain.com forwards to an AOL email account). Would this prevent emails from being sent if I am using the default DNS records? I also have not changed the Email Routing option to Remote Mail Exchanger.

Staff
9,968 Points
2014-06-30 5:36 pm
Hello Fawn,

I would recommend sending your order confirmations directly to an email account hosted on the same domain as your WordPress site to rule out any 3rd party delivery errors.

If you are missing any order confirmations at your AOL.com address I would suggest that you login to webmail and check for them locally on the server.

I do see some delivery errors from your account to AOL.com and this will happen from time to time, as AOL offers free email accounts and sometimes will reject automated messages from servers, especially on their first delivery attempts. This is also referred to as temporary defferment and is mentioned on this AOL page about 421 errors which is what some of your mails are getting rejected by.

Please let us know if you had any further questions.

- Jacob
n/a Points
2014-07-06 2:37 pm

i'm new in programming, i created a form to send email from my website to yahoomail but i got this error

Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set()

what can i do

Staff
9,968 Points
2014-07-06 6:42 pm
Hello Morphy,

As your PHP error states, it can't connect to the server localhost on port 25.

You'll need to check with your web host to find out what mail server you should be using. You might need to send mail with phpMailer instead of the default PHP mail() function depending on your server configuration.

You more than likely need to define some SMTP settings, and use the same server and account information you would use when setting up a normal email account on your desktop. Or your web host might let you know you need to define another SMTP port rather than 25 in your php.ini file.

- Jacob
n/a Points
2015-01-06 7:49 am

how to send emaim using form

form indlude all field which is send me

n/a Points
2014-07-06 11:41 pm

hi, i want to create auto responder and get the data into database and its like send a data differently to sender. help me

how could i create it. im very new in php

Staff
29,164 Points
2014-07-07 7:12 am
Hello Eihton,

While not a difficult task, it would require a few more pieces to be added. To auto respond to the customer, you would need to add another piece of code that emails out that the information was received. Then, to add the data to the database you would need to have the proper tables set up in an existing database, code for php to connect to the database, then build your SQL query using the information input by the user as the data to insert. Finally you would insert that data into the database.

Unfortunately we currently do not have that type of tutorial information available, however there are many articles and tutorials around the web for each part that is required. I have given you the layout, so go from there and see if you can find code samples to assist. Be sure to also read up on error checking and avoiding SQL injection from malicious users to protect your database. We do have an article that may help you get started.

Kindest Regards,
Scott M
n/a Points
2014-07-09 11:17 am

Warning: mail(): Failed to connect to mailserver at &quot;localhost&quot; port 25, verify your &quot;SMTP&quot; and &quot;smtp_port&quot; setting in php.ini or use ini_set() in C:\wamp\www\design\mail.php on line 13

what will i do now

Staff
29,164 Points
2014-07-09 11:36 am
Hello Antara,

When you use 'localhost' in your settings, that means it will look on the current computer. If you are using this on your local computer, yuo likely do not have it set up as a mail server, which is why you are getting the message. You will need to have it point to a server with mail serving functionality, such as your webhosting server.

Kindest Regards,
Scott M
n/a Points
2014-08-21 1:02 am

Q .As u ve mentioned here

$admin_email = "someone@example.com";to send mail to that particular mail address.. I m using phpmailer n its working fine for me to send mail to any gmail or yahoo mail but not to any custom mail address. 

I want to send mail to any custom domain address by phpmailer. Any help here  ??

Staff
29,164 Points
2014-08-21 7:39 am
Hello Pooja,

If the you are successfully sending emails to some addresses but not others, then the issue would not likely be with the function. You will want to test by sending an email from your server via webmail to the address that is not receiving it. Look for a return message in that email account for the reason why it is not getting through.

Kindest Regards,
Scott M
n/a Points
2014-08-26 5:50 am
should give the explanatory example like on w3 schools (try yourself).
Staff
17,726 Points
2014-10-06 10:28 am
Hello priya,

Thank you for your suggestion. For anyone else who may find this post, here is a helpful link to the w3Schools page on PHP 5 mail() Function.

Thank you,
John-Paul
n/a Points
2014-09-05 6:34 pm

Hi there, i have made use of the basic PHP mail code thanks to you but the issue im having is that the email comes through to me from my hosting server and no comments added to the email... subject is the visitors email address as i set it... see below and please advise if i am doing it wrong:

<?php//if "email" variable is filled out, send email  if (isset($_REQUEST['email']))  {    //Email information  $to = "admin@mydomain.co.za";  $name = $_REQUEST['name'];  $email = $_REQUEST['email'];  $telephone = $_REQUEST['telephone'];  $comments = $_REQUEST['comments'];    //send email  mail($to,$name,$comments);    //Email response  echo "Thank you for contacting us!";  }    ?>

Staff
26,137 Points
2014-09-05 6:50 pm
Hello Mo,

Make sure that you when you use the code above, that it's spaced out with the comments on their own line - like the following:

//if "email" variable is filled out, send email

if (isset($_REQUEST['email'])) {

//Email information

$to = "admin@mydomain.co.za";
$name = $_REQUEST['name'];
$email = $_REQUEST['email'];
$telephone = $_REQUEST['telephone'];
$comments = $_REQUEST['comments'];

//send email

mail($to,$name,$comments);

//Email response

echo "Thank you for contacting us!";
}
?>


Your email form requires should be asking for the comment as well. As per the code above, the form section has "comment" set in the text area. I hope that helps to clarify the issue! Please let us know if you have any further questions.

Regards,
Arnel C.

n/a Points
2014-09-05 8:36 pm

Hi,

Thank you for the speedy reply.

I have made changes to the PHP code and now have the message come through in the body of the mail but still cannot get the visitors email to come up as FROM:.... just comes from my webhost name. see code below:

<?php

//if "email" variable is filled out, send email  if (isset($_POST ['submit'] ))  {    //Email information  $to = "admin@squareonline.co.za";  $from = 'email';  $name = $_POST['name'];  $email = $_POST['email'];  $message = $_POST['message'];    //send email  mail($to,$email, $name, $message,$from);    //Email response  echo "Thank you for contacting us! We will contact you shortly ";  }  ?>

Staff
26,137 Points
2014-09-05 8:54 pm
Hello Mo,

If you look closely at your code, take a close at your $from. I think that's the problem. Make sure you're using the right variable there and then everything should be okay.

I hope that helps!

Regards,
Arnel C.
n/a Points
2014-10-02 5:57 am

pls i am new in web designing and development. how can i design a php webpage that i can use to send and recieve mail like that of yahoo and gmail. it will have all the features like the trash box, draft box, inbox, sent and delet links. if you can send m the codes and syntax it will be welcome. pls forward to my email

Staff
29,164 Points
2014-10-02 1:27 pm
Hello Chike,

While your question is about email, it does not relate to this article directly. I have moved your question to be a brand new question. You can find it here.

Kindest Regards,
Scott M
n/a Points
2014-10-13 5:00 am

following code is not working

-----------------------------------------------------

 

<?php    $to = 'satbir4@gmail.com';        $subject = 'Website Change Reqest';        $message="Message Goes Here";    $headers = "MIME-Version: 1.0\r\n";    $headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";    if (mail($to, $subject, $message, $headers))    {      echo 'Your message has been sent.';    }        else    {      echo 'There was a problem sending the email.';    }?>

Staff
29,164 Points
2014-10-13 10:43 am
Hello Satbir,

Are you getting an error message? If so, what is it? Where are you trying to use this code? Is it a webserver or a WAMP, XAMP, LAMP setup?

Kindest Regards,
Scott M
n/a Points
2014-10-24 1:37 pm

I use LAMP postfix centos and this script :)) i recive Thank you for contacting us! but i dont recive the mail please help me

Staff
29,164 Points
2014-10-24 2:56 pm
Hello,

You will need to check in your email logs to see if the email went out or if there is an error message.

Kindest Regards,
Scott M
n/a Points
2014-10-28 9:48 pm

this code is selecting an email address from my database and is going to send a message to the selected email. the code works fine but not the mail function for $to = $address

$to = $address;

(mail($to,$subject,$txt,$headers);

 

 

// select statement        $SQLString = "SELECT email\n"        . "FROM tutor\n"        . "WHERE MATCH(text_data) AGAINST ('{$messageText}' IN BOOLEAN MODE)\n"        . "GROUP BY id\n"        . "ORDER BY sum(5) DESC";    echo $SQLString;          $result = mysql_query($SQLString);        // Verify it worked        if (!$result) echo mysql_error();        // execute select statement        $row = mysql_fetch_row($result);        // Should show you an integer result.        $address = print_r($row, true);        echo $address;                $to = $address;        $subject = "testing...";        $txt = $messageText;        $headers = $senderPhone;                if (mail($to,$subject,$txt,$headers))        {        echo "successful";        }        else        {        echo "error";        }

 

thanks for any help in advance :)

Staff
29,164 Points
2014-10-29 9:07 am
Hello Kaleb,

From the way you phrased your question, it seems that you are having issues with $to taking the value of $address. The code snippet you provided does not show where the $address variable is filled. What do you get when you echo $address? The $to = $address is not a function, but an assignment, making $to the value of whatever is in $address at the time. Do the other variables ($subject, $txt, etc) receive their values?

The mail($to,$subject,$txt,$headers) function is called but you do not have any code to catch and display the return code to know why it is failing. You will need to do that in order to understand what is going on.

Kindest Regards,
Scott M
n/a Points
2014-11-11 5:36 am

Hello Iam Alaa i have error and i can not find it can help me when i sent to email see this massage what can i do

Deprecated: Function eregi() is deprecated in C:\wamp\www\Light\js\sendmail.php on line 49

 

 

Staff
11,186 Points
2014-11-11 10:40 am
This means that you are using a function that has been removed from newer versions of PHP within that code located at C:\wamp\www\Light\js\sendmail.php. You will need to further investigate the code of that file and re-write the PHP to allow it to work on newer versions of PHP.
n/a Points
2014-12-05 7:25 am
when use this code this error ocur(" Warning: mail(): Failed to connect to mailserver at &quot;localhost&quot; port 25, verify your &quot;SMTP&quot; and &quot;smtp_port&quot; setting in php.ini or use ini_set()") and we can not handle this :O
Staff
5,084 Points
2014-12-05 11:13 am
Hello Kiran,

Unfortunately PHP's mail() function does not support SMTP protocols. I did however find an alternative on a Stack Overflow thread.

Kindest Regards,
TJ Edens
n/a Points
2014-12-07 8:40 am

I am use your phpmail function ok, function is work fine & mail send & receive ok,

when i am add my more input box main name input box then create a problem.

<?php//if "email" variable is filled out, send email  if (isset($_REQUEST['email']))  {    //Email information  $admin_email = "someone@gmail.com";  $name = $_REQUEST['name'];  $company = $_REQUEST['comname'];  $email = $_REQUEST['email'];  $subject = $_REQUEST['subject'];  $comment = $_REQUEST['comment'];    //send email  mail($admin_email,$name,$company,$subject,$comment, "From: . $email");    //Email response  echo "Thank you for contacting us!";  }    //if "email" variable is not filled out, display the form  else  {?> <form method="post">  Name: <input name="name" type="text" /><br />  Company: <input name="comname" type="text" /><br />  Email: <input name="email" type="text" /><br />  Subject: <input name="subject" type="text" /><br />  Message:<br />  <textarea name="comment" rows="15" cols="40"></textarea><br />  <input type="submit" value="Submit" />  </form>  <?php  }?>

Problem::Warning: mail() expects at most 5 parameters, 6 given in /home/mailsend.php on line 22

 

how to solve it.

Staff
29,164 Points
2014-12-08 3:39 pm
Hello Nayeem,

The php mail() function needs at least 3 arguments, and has two optional ones, totaling 3-5 arguments. These arguments are specific arguments and not ones you can create on your own. The required arguments are 'to', 'subject', and 'message' with the optional ones being 'additional headers' and 'additional parameters'.

Your call to the function has some different arguments and totals 6, so you will need to fit those custom arguments into your message and then reformat the function call to match the standard php mail() function call.

You can learn more about that function here at thephp manual page.

Kindest Regards,
Scott M
n/a Points
2014-12-16 3:42 am

Hi,  

I have the same problem. I am using the PHPmail script. I config this script and it shows the message "Mail has been sent". But didnt reached at desired email location. Please Anyone helps me to solve this issue.

Staff
29,164 Points
2014-12-16 9:16 am
Hello Arvinder,

If you are working with the phpmail function and got the "Mail has been sent" message, it should have been sent from the server. You will now need to check the server logs and see if the message was indeed sent and if it was received properly by the other server.

Kindest Regards,
Scott M
n/a Points
2015-01-08 10:20 am

Thanks for so nice form. I wanted to know that how to send email for CC and BCC alingwith main admin email.

Staff
29,164 Points
2015-01-12 3:35 pm
Hello Anant,

To add CC and BCC you need to include those as part of your headers. Replace the 'From' section with a $headers variable. Prior to calling that, you need to add the header content. It will look like something below:

$headers = "From: no-reply@example.com\r\n" .
"X-Mailer: php\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$headers .= "CC: admin@example.com\r\n";
$headers .= "Bcc: admin@example.com\r\n";


This should assist you. Note that this is a sample and has not been tested, further modification may be required.

Kindest Regards,
Scott M
n/a Points
2015-01-14 7:35 am

Thanks for your kindly reply.

Next, you are requested to provide the complete code at one place with facility of CC and BCC email and CAPTCHA protection.

 

Thanks and regards,

Anant

Staff
29,164 Points
2015-01-15 11:13 am
Hello Anant,

While we are happy to provide assistance, we do not provide coded solutions upon request. With the article above plus the information provided in the support center, you should be able to put together a form in the format you desire.

Kindest Regards,
Scott M
n/a Points
2015-01-15 3:52 am

<?php if(isset($_POST['submit'])){    $to = "Harshakratn@gmail.com"; // this is your Email address    $from = $_POST['txtemail']; // this is the sender's Email address    $name = $_POST['txtname'];    $email = $_POST['txtemail'];    $number = $_POST['txtphone'];    $city = $_POST['txtcity'];    $subject = "User Information";    //$subject2 = "Copy of your form submission";    $message = $name . " " . $email . " " . $number ." ". $city ;    //$message2 = "Here is a copy of your message " . $first_name . "\n\n" . $_POST['message'];    $headers = ""    $headers = "From:" . $from;    //$headers2 = "From:" . $to;    mail($to,$subject,$message,$headers);    //mail($from,$subject2,$message2,$headers2); // sends a copy of the message to the sender    echo "Mail Sent. Thank you " . $first_name . ", we will contact you shortly.";    // You can also use header('Location: thank_you.php'); to redirect to another page.    }?>

 

Dear sir,

              i am working on this code and it is not working . i want send the information filled by the user to our mail but this code doing nothing please suggesed me .

Staff
29,164 Points
2015-01-15 10:44 am
Hello Harshak,

Unfortunately this snippet of code will not be helpful in fully troubleshooting the issue. What troubleshooting steps have you tried? Are you getting an error message? Are you able to echo out your specific variables to ensure they are being filled?

Kindest Regards,
Scott M
n/a Points
2015-01-21 4:18 am

can i fill

$admin_email = "someone@example.com";

with

$admin_email = "someone@example.com, someone2@example.com "; ?

Staff
26,137 Points
2015-01-21 11:23 am
Hello Kevin,

Thanks for the question. There is not anything saying that you can't have two admins, so you're welcome to try it. If you want to be sure that the email makes it to BOTH email admins, then set up a forwarder for the administrator email address so that it forwards to your second administrator email address.

Regards,
Arnel C.
n/a Points
2015-02-05 5:45 pm

Thank you!!

n/a Points
2015-02-06 5:55 am

PHP must be configured correctly in the php.ini file with the details of how your system sends email. Open php.ini file available in /etc/ directory and find the section headed [mail function].

Windows users should ensure that two directives are supplied. The first is called SMTP that defines your email server address. The second is called sendmail_from which defines your own email address.

The configuration for Windows should look something like this:

[mail function]
; For Win32 only.
SMTP = smtp.secureserver.net

; For win32 only
sendmail_from = xyz@yourdomain.com

Linux users simply need to let PHP know the location of their sendmail application. The path and any desired switches should be specified to the sendmail_path directive.

The configuration for Linux should look something like this:

[mail function]
; For Win32 only.
SMTP = 

; For win32 only
sendmail_from = 

; For Unix only
sendmail_path = /usr/sbin/sendmail -t -i

 

make the required modification in the php.ini file and try once again.

n/a Points
2015-02-19 9:32 am

I have a problem about my mail function, Its working on all other domains but not working on our domain. we have a .ae domain that is working(I can recieve emails from them) but the .com is not (we are using Gmail as our hosting provider).

 

Thanks in advance

 

Staff
17,726 Points
2015-02-19 3:34 pm
Hello Romeo,

Thank you for your question. We are happy to help, but will need some additional information. What happens when you try to send the emails?

Are you getting any errors? What are they?

What is the .com domain? Have you confirmed the MX records are pointed to Gmail correctly?

Have you made any DNS changes to the .com domain in the last 24 hours?

Thank you,
John-Paul
n/a Points
2015-02-22 12:49 am

I dont reveieve any error messages, Email is working on other domains. I have a domain of romeo@travelats.com and romeo@travelats.ae, I was able to get the email using the travelats.ae but not the .com. And I have not made any changes on the DNS server.

here is my code

 

$order=$_POST["order"];

$amount=$_POST["amount"];

$bank=$_POST["bank"];

$mail=$_POST["email"];

$sender = "ATS Travel <info@travelats.com>";

 

 

// multiple recipients

$to  = $mail;

// subject

$subject = 'Payment Receipt';

 

// message

 

  $body = <<<EOD

<br><hr><br>

Your transaction is succesful! <br>

Order: $order <br>

Amount: $amount<br>

Bank Reference Number: $bank<br>

EOD;

$headers .= "From: $sender\r\n";

$headers .= 'MIME-Version: 1.0' . "\r\n";

$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

$success = mail($to, $subject, $body, $headers);

$theResults = <<<EOD

EOD;

 

echo "$theResults";

 

;

Thanks,

 

Romeo 

Staff
29,164 Points
2015-02-23 5:00 pm
Hello Romeo,

You will want to place code around the mail function to test any error codes that it may be throwing.

Kindest Regards,
Scott M

n/a Points
2015-02-23 3:00 am

my not receiving any mail,can you please let me know thw mistake

<tr><td><input type="submit" img src="/support/../images/1.png"  name="add_main" value="ADD"/>  </td></tr>

<?php

if($_POST['add_main']){

$from    ='xyz@gmail.com';

$to      = 'rt@gmail.com';

$subject = 'test';

$message = 'test completed';

$headers = 'From: supernona151992@gmail.com' . "\r\n" ;

mail( $from, $to, $subject, $message, $headers);

echo"sent";

 

}

?>

Staff
29,164 Points
2015-02-23 5:02 pm
Hello Mav,

Have you gotten any error messages? Have you tried echoing out the variables to be sure they are all correct? Have you tried any error trapping on the mail function to see if it is throwing any other error types?

Kindest Regards,
Scott M
n/a Points
2015-03-07 9:10 pm

Hi. This code doesn't work for me either. Would be great if there was something like this that addressed my and other people's issues. Some clear instructions for newbies would be much appreciated. Back to googling for simple form guidance...

Staff
29,164 Points
2015-03-10 7:49 am
Hello Guy,

Are you getting any particular error messages? If so, paste them here and we will be glad to take a look for you.

Kindest Regards,
Scott M
n/a Points
2015-03-16 6:02 am

i want to send the copy of mail to users mail id how can i do this

Staff
17,726 Points
2015-03-16 9:49 am
Hello Ramesh,

Thank you for your question. The above guide allows users to send a text email to you from a form.

There are many ways for you to send an email to users, but I recommend reading our guide How to send a message to your phpList mailing list.

Thank you,
John-Paul
n/a Points
2015-03-18 2:29 am

hi,

what the conclusion of above disscussion..............??????

plz tell me exact solution that what changes should i make in php.ini and sendmail.ini file to sending rhe mail to user(that he should recive the mail).

The output is only the messge.how should i know that mail is deliverd to the user???

i dont want to use phpmailer() function.

thanks

Staff
26,137 Points
2015-03-18 10:55 am
Hello Purva,

Apologies, but I'm not sure what you're asking. The comments in this section are FOR using the php mail function. If you don't want to use it, then you need to be using something else and your comment should be in an article appropriate to your topic. Changes in the php.ini and sendmail.ini files will NOT send email to a user. You will need to have some code that will do that. There built-in functions (such as phpmail and mail) in PHP that can be coded for that purpose, but we do not provide code beyond what we have provided in the articles written within the Support Center. If you want a tutorial on how to use phpmail, please see phpmail function.

Kindest regards,
Arnel C.

n/a Points
2015-03-19 8:53 am

hello sir,

 

How to give HTML lINK in mail function of php.

Staff
17,726 Points
2015-03-20 3:47 pm
Hello vandana,

Thank you for your question. I found a post via online search, where they are discussing possible ways to embed a link in php mail function. This may be a good starting point in determining how to code this.

Thank you,
John-Paul
n/a Points
2015-04-09 1:43 am

Hello!

please help me i want to send mail to group, thats i maintain in database(email addresses)

Staff
29,164 Points
2015-04-09 9:18 am
Hello Muhammad,

You can either add more email addresses to the To section or create a forwarder on the receiving email that sends to the other desired email addresses.

Kindest Regards,
Scott M
n/a Points
2015-04-18 12:10 am

Sir Scott....Hmmm, thats fine Sir......one thing more....Can I add multiple headers more than we use in routine? How?

 

Staff
29,164 Points
2015-04-20 8:17 am
Hello Muhammad,

You could simply add a variable named headers and then add as many as you need. For instance:
$headers = 'From: webmaster@example.com' . "\r\n" .
'Reply-To: webmaster@example.com' . "\r\n"


You then insert that variable as one of the parameters in the mail() function.

Kindest Regards,
Scott M


n/a Points
2015-04-24 9:54 am

I wanted my email to be send as HTML format and this is the code. The recieved email is a text with html tags.

// if no errors send email

if(!isset($error_message)) {

// Header

$headers = "From: " . strip_tags($_POST['email_address']) . "\r\n";

$headers .= "Reply-To: ". strip_tags($_POST['email_address']) . "\r\n";

$headers .= "MIME-Version: 1.0\r\n";

$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";

 

// Email

$email_msg   = '<html><body>';

$email_msg  .= "<a href=\"http://" . $URL . "/\">" . "<img src=http://" . $URL . "/images/logo.png  " . "width=\"219\" height=\"71\"></a>";

$email_msg  .= "<p>Email was sent from: " . $URL . "</p>";

.

.

.

$email_msg  .= "</table></body></html>";

 

$mailTo = $mailTo;

 

//send email

mail($mailTo, $subject, $email_msg, "From:" . $email_address);

 

if i added $headers to my mail line it will not work 

mail($mailTo, $subject, $email_msg, $headers);

 

Please advice

Thanks

Staff
26,137 Points
2015-04-24 4:02 pm
Hello Mohammad,

Unfortunately, we can't really provide coding support. However, we do try to point you in the right direction. Check out this Sending Nice HTML email with PHP. This post provides information that you could use to send email as HTML using the PHP mail function.

I hope this helps to answer your question, please let us know if you require any further assistance.

Regards,
Arnel C.
n/a Points
2015-04-28 5:11 am

what are the necessary settings to be made while sending e-mail using mail() or phpmailer in php?

Staff
29,164 Points
2015-04-28 10:07 am
Hello girish,

With phpmailer you will want to ensure you are using the proper SMTP settings.

Kindest Regards,
Scott M
n/a Points
2015-05-11 10:43 am

HI,

I am new to coding and would like some advice on the easiet and simplest way to set up a contact form page. I was thinking of using a webtemplate and my gmail to recieve the contact info. The webplate does not include any php help. I use microsoft webexpression for editing. Can you recommend a simple article to follow in a step by step. The more I read the more complex this is looking. Thank You in Advance for any guidance you can give me!

Staff
5,084 Points
2015-05-11 2:25 pm
Hello Karen,

You would just need to go to page -> Code and you would insert the code from this page. You would need to change the admin email address to your gmail.com account so the form knows where to send the emails to.

Best Regards,
TJ Edens
n/a Points
2015-05-12 10:57 am

HI,

Thank You for your quick response. I tried the code at the top of this page and it appears to work but no email shows up at my gmail email. Here is the link to my page http://ecbiz172.inmotionhosting.com/~efabri6/contact2.php

 

I copied and pasted the code and inserted my email, any ideas what I might need to change.

 

<?php

//if "email" variable is filled out, send email

  if (isset($_REQUEST['email']))  {

  

  //Email information

  $admin_email = "nebraskaantiques@gmail.com";

  $email = $_REQUEST['email'];

  $subject = $_REQUEST['subject'];

  $comment = $_REQUEST['comment'];

  

  //send email

  mail($admin_email, "$subject", $comment, "From:" . $email);

  

  //Email response

  echo "Thank you for contacting us!";

  }

  

  //if "email" variable is not filled out, display the form

  else  {

?>

 

 <form method="post">

  Email: <input name="email" type="text" /><br />

  Subject: <input name="subject" type="text" /><br />

  Message:<br />

  <textarea name="comment" rows="15" cols="40"></textarea><br />

  <input type="submit" value="Submit" />

  </form>

  

<?php

  

  }

?>

 

 

Staff
26,137 Points
2015-05-13 2:53 pm
Hello Karen,

Based on your account I was able to take a quick look at your email logs and I could see that the email was working. I then briefly tested the code myself with my own email address and it also worked. I had it send to my Gmail account and I received messages. You might be forwarding your messages somewhere else, or there's a filter that's removing messages - you will need to check to make sure that a spam filter or something else is not in the way. I hope this helps to let you know that the function is working.

If you have any further questions or comments, please let us know.

Regards,
Arnel C.














n/a Points
2015-05-20 4:41 am

Dear Friends,

i want to send mail from php. but each time i run it in browser it displays error message

Message could not be sent.Mailer Error: Extension missing: openssl

i am posting my code below

<?phprequire 'PHPMailer/phpmailerAutoload.php';require 'PHPMailer/class.phpmailer.php';//require'phpmailer/class.phpmailer.php'; $mail = new PHPMailer; $mail->isSMTP();                                      // Set mailer to use SMTP$mail->Host = 'smtp.gmail.com';                       // Specify main and backup server$mail->SMTPAuth = true;                               // Enable SMTP authentication$mail->Username = 'anything@gmail.com';                   // SMTP username$mail->Password = '*************';               // SMTP password$mail->SMTPSecure = 'ssl';                            // Enable encryption, 'ssl' also accepted$mail->Port = 995;                                    //Set the SMTP port number - 587 for authenticated TLS$mail->setFrom('THIS IS TESTING MESSAGE');     //Set who the message is to be sent from$mail->addReplyTo('anything@yahoo.com');  //Set an alternative reply-to address$mail->addAddress('anything@yahoo.com');  // Add a recipient$mail->addAddress('anything@yahoo.com');               // Name is optional$mail->addCC('anything@yahoo.com');$mail->addBCC('anything@yahoo.com');$mail->WordWrap = 50;                                 // Set word wrap to 50 characters//$mail->addAttachment('/usr/labnol/file.doc');         // Add attachments//$mail->addAttachment('/images/image.jpg', 'new.jpg'); // Optional name$mail->isHTML(true);                                  // Set email format to HTML $mail->Subject = 'Here is the subject';$mail->Body    = 'This is the HTML message body <b>in bold!</b>';$mail->AltBody = 'This is the body in plain text for non-HTML mail clients'; //Read an HTML message body from an external file, convert referenced images to embedded,//convert HTML into a basic plain-text alternative body//$mail->msgHTML(file_get_contents('contents.html'), dirname(__FILE__)); if(!$mail->send()) {   echo 'Message could not be sent.';   echo 'Mailer Error: ' . $mail->ErrorInfo;   exit;} echo 'Message has been sent'; 

if some one had solution about this, plez contact me on my email id,....vijaybhat1981@gmail.com

Staff
26,137 Points
2015-05-21 10:14 am
Hello Vijay,

If you're missing the OpenSSL extension, it typically has something to do with the PHP.INI file. If you are an InMotion Hosting customer, then there should be a PHP.INI file local to your installation. Check out this post in concerning the issue.

Regards,
Arnel C.
n/a Points
2015-05-20 3:08 pm

 Warning: mail(): Failed to connect to mailserver at &quot;localhost&quot; port 25, verify your &quot;SMTP&quot; and &quot;smtp_port&quot; setting in php.ini or use ini_set() in C:\wamp\www\icone-cnam\Contact.php on line 51

 

Staff
26,137 Points
2015-05-21 10:22 am
Hello Boukeri,

You should first try changing the port. Port 25 is typically blocked by many ISP's. Try using 587. If you continue to have a problem with it, then please see your documentation for WAMP. We do not provide support for WAMP issues as they are non-service related. If you were running the code on our server, then the WAMP code should not be referenced in the error message.

I hope this helps to answer your question, please let us know if you require any further assistance.

Regards,
Arnel C.
n/a Points
2015-05-22 3:54 am

hai,i am getting this error

Warning: mail(): Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in D:\wamp\www\s\oc.php on line 13 can't get conformation mail 

Staff
17,726 Points
2015-05-26 3:45 pm
Hello simhaa,

Thank you for contacting us. We are happy to help, but will need some additional information.

Have you checked that port 25 is open on your server? The error suggests this may be the problem.

Thank you
John-Paul
n/a Points
2015-05-27 12:35 am

port 25 is open i have checked it 

and changed it to 465and tryed it but cam to be same erroe port 465 and smtp()

 and how vll we know whether port is open or not

Staff
26,137 Points
2015-05-28 2:34 pm
Hello Simhaa,

We're not sure where you're trying to run your code, but check out this post. You may require some server setup that is missing. This article will show you how to check on an port. I hope that helps to answer your question! If you require further assistance, please let us know!

Regards,
Arnel C.
n/a Points
2015-06-05 4:04 pm

Can the PHP mail() function work in localhost environment. Will the Email be sent.

Staff
29,164 Points
2015-06-05 5:28 pm
Hello Sunny,

Unless you have set up your local environment to act as a mail server, it will not do so. You will need to test from a hosting server that can actually send the emails. You may also want to use an emulator such as the one linked below. We have not tested this program nor do we endorse it.

http://www.toolheap.com/test-mail-server-tool/

Kindest Regards,
Scott M
n/a Points
2015-06-15 7:52 am

Hi, I have a question that I cannot seem to find an answer for online. Probably because it contains words that give me other info. I have a website that will allow users to sign up to. When they create their new member information, once all validation is done and all is correct it sends them an email with a validation link. They click on the link which then redirects back to the website. This allows for their info to be written in a temp table and once validated written to the actual table and the info from temp table deleted. All works fine except that I have to use the URL they click on to send the validation key back to the website which means acquiring the data via GET rather than POST... I am trying to find out if there is a way to send it via POST so that it is not accessible to eyes through the URL. Any direction to where to look how to do this would be great.... trying to search for information because of the words email, validation, post etc are bringing up everything but what I really want.

Staff
29,164 Points
2015-06-15 3:17 pm
You should be able to set a variable with either GET or POST so you can process it. Here is a thread on a popular forum that explains different ways to send variables to a waiting page. That should help you with your situation.
n/a Points
2015-06-15 8:32 pm

Hi, Thank you. What I am looking for is to send a confirmation code back from someone's email to a webpage using POST rather than GET. I could code a form to be sent within the email and when they click validate it is a form submit so able to use the POST but I want to avoid using a form.I have no idea of any other way to send it from their computer back to the site as POST so the confirm code is not attached to the URL.  I have my form on the website which collects the user data, I process it with heavy validation and then they are sent an email with a link... I was using a link with the confirm code appended to it and then a GET in the webpage to validate but I have read in several places that is not a good idea yet I cannot seem to find how to get the user from their email back to the webpage without using GET. I am aware of using POST/GET in a webpage form, what I need is to use POST so that the data coming back from the validation email to validate that the person actually joined the website is not visable within the URL.

Thanks again.

n/a Points
2015-06-24 9:36 pm

i create website and i want to send messages to my contact to their phone i'm  as admin,i need information about that

Staff
17,726 Points
2015-06-25 11:16 am
Hello suma,

Thank you for contacting us. It is possible to code/develop this in PHP, here is a link to a post where they discuss possible solutions.

If you are using a CMS such as WordPress, Joomla, or Drupal, they may plugins, or addons for accomplishing this as well.

Thank you,
John-Paul
n/a Points
2015-07-09 9:35 am

How to set in html form?

Staff
17,726 Points
2015-07-09 3:52 pm
Hello ,

Thank you for your question. I found a guide online online titled "How do I add PHP code/file to HTML(.html) files" where provide an .htaccess rule that makes this possible.

Thank you,
John-Paul
n/a Points
2015-08-02 9:58 am

can you please help me i tried the code at the top of these page and it run but no email show up in my yahoo and gmail emails.. heres the code that i tried 

<html>

<body bgcolor="black">

<center><font siZe="200" color="white"><br><br><br><br>

This Websites Contains<br>Unknown Quantity<br></font><font color="white">Please Type your E-Mail

<?php

//if "email" variable is filled out, send email

  if (isset($_REQUEST['email']))  {

  

  //Email information

  $email = "anton_paraggua@yahoo.com";

  $admin_email = $_REQUEST['admin_email'];

  $subject = $_REQUEST['subject'];

  $comment = $_REQUEST['comment'];

  

  //send email

  mail($admin_email, "$subject", $comment, "From:" . $email);

  

  //Email response

  echo "Thank you for contacting us!";

  }

  

  //if "email" variable is not filled out, display the form

  else  {

?>

 <form method="post">

  Email: <input name="email" type="text" /><br />

  Subject: <input name="subject" type="text" /><br />

  Message:<br />

  <textarea name="comment" rows="15" cols="40"></textarea><br />

  <input type="submit" value="Submit" />

  </form>

  

<?php

  }

?>

 <form method="post"><br>

  <input name="email" type="text" /><br /><br>

  <input type="submit" value="Submit" />

  

  </form>

  

<?php

  }

?>

</body>

</html>

can you please help me

Staff
29,164 Points
2015-08-03 12:46 pm
You may want to contact Live Support and see if they can identify the email as leaving the server. Then you will be able to work from there.
n/a Points
2015-08-07 6:33 pm

do the code on top of this page can send email in local host?? i'm using xampp

please the code runs smoothly but there's no email shows up in my emails..

Staff
17,726 Points
2015-08-10 9:25 am
Hello anton,

Thank you for contacting us. I recommend checking your mail logs for any record of transmission. This will help determine if it is failing in the code, or it is being delivered, and the server is rejecting it, etc.

Thank you,
John-Paul
n/a Points
2015-08-07 1:23 am

hello,

 

i want a php script which sends email on customer's birthday without submiting code.simply means automatic mail sending.

Staff
17,726 Points
2015-08-07 1:02 pm
Hello bhoomi,

Thank you for contacting us. We do not have any guides on how to make a script that sends birthday emails, but i found several suggestions via Google search. Here are some examples: birthday script with mail functions to customer, and Send mail to different customer on their birthday . These offer some possible solutions.

Thank you,
John-Paul
n/a Points
2015-08-08 11:32 am
hi . sorry am not good in english ( i have a page php . in the page ther is 3 one for the e-mail and two for the username and the password . i want when some one write his e-mais . the page send his e-mail to my email . how can i do this please ?
Staff
17,726 Points
2015-08-10 9:53 am
Hello mahdi,

Thank you for contacting us. The above guide explains how to get the form to email information. For a more detailed information, see our guide on How to create a custom PHP contact form with validation. It provides a more complete tutorial, but you will have to customize the code to fit your need.

Thank you,
John-Paul

Post a Comment

Name:
Email Address:
Phone Number:
Comment:
Submit

Please note: Your name and comment will be displayed, but we will not show your email address.

119 Questions & Comments

Post a comment

Back to first comment | top

Need more Help?

Search

Ask the Community!

Get help with your questions from our community of like-minded hosting users and InMotion Hosting Staff.

Current Customers

Chat: Click to Chat Now E-mail: support@InMotionHosting.com
Call: 888-321-HOST (4678) Ticket: Submit a Support Ticket

Not a Customer?

Get web hosting from a company that is here to help. Sign up today!