PHP mail function doesn't complete sending of e-mail











up vote
403
down vote

favorite
102












<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: yoursite.com';
$to = 'contact@yoursite.com';
$subject = 'Customer Inquiry';
$body = "From: $namen E-Mail: $emailn Message:n $message";

if ($_POST['submit']) {
if (mail ($to, $subject, $body, $from)) {
echo '<p>Your message has been sent!</p>';
} else {
echo '<p>Something went wrong, go back and try again!</p>';
}
}
?>


I've tried creating a simple mail form. The form itself is on my index.html page, but submits to a separate "thank you for your submission" page, thankyou.php, where the above php code is embedded.
The code submits perfectly, but never sends an email. please help.










share|improve this question




















  • 1




    is your program in local computer or live host
    – Tamil Selvan C
    Jul 9 '14 at 2:20






  • 6




    1. are you sure it isn't going into your spam? 2. do you have sendmail configured on your box?
    – Steven Moseley
    Jul 9 '14 at 2:21










  • its on a live host it is not going into my spam..i'm not sure what you mean by having sendmail configured on my box so i assume not?
    – user3818620
    Jul 9 '14 at 2:22










  • try send without $from
    – Artem Ilchenko
    Nov 8 '17 at 14:31















up vote
403
down vote

favorite
102












<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: yoursite.com';
$to = 'contact@yoursite.com';
$subject = 'Customer Inquiry';
$body = "From: $namen E-Mail: $emailn Message:n $message";

if ($_POST['submit']) {
if (mail ($to, $subject, $body, $from)) {
echo '<p>Your message has been sent!</p>';
} else {
echo '<p>Something went wrong, go back and try again!</p>';
}
}
?>


I've tried creating a simple mail form. The form itself is on my index.html page, but submits to a separate "thank you for your submission" page, thankyou.php, where the above php code is embedded.
The code submits perfectly, but never sends an email. please help.










share|improve this question




















  • 1




    is your program in local computer or live host
    – Tamil Selvan C
    Jul 9 '14 at 2:20






  • 6




    1. are you sure it isn't going into your spam? 2. do you have sendmail configured on your box?
    – Steven Moseley
    Jul 9 '14 at 2:21










  • its on a live host it is not going into my spam..i'm not sure what you mean by having sendmail configured on my box so i assume not?
    – user3818620
    Jul 9 '14 at 2:22










  • try send without $from
    – Artem Ilchenko
    Nov 8 '17 at 14:31













up vote
403
down vote

favorite
102









up vote
403
down vote

favorite
102






102





<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: yoursite.com';
$to = 'contact@yoursite.com';
$subject = 'Customer Inquiry';
$body = "From: $namen E-Mail: $emailn Message:n $message";

if ($_POST['submit']) {
if (mail ($to, $subject, $body, $from)) {
echo '<p>Your message has been sent!</p>';
} else {
echo '<p>Something went wrong, go back and try again!</p>';
}
}
?>


I've tried creating a simple mail form. The form itself is on my index.html page, but submits to a separate "thank you for your submission" page, thankyou.php, where the above php code is embedded.
The code submits perfectly, but never sends an email. please help.










share|improve this question















<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: yoursite.com';
$to = 'contact@yoursite.com';
$subject = 'Customer Inquiry';
$body = "From: $namen E-Mail: $emailn Message:n $message";

if ($_POST['submit']) {
if (mail ($to, $subject, $body, $from)) {
echo '<p>Your message has been sent!</p>';
} else {
echo '<p>Something went wrong, go back and try again!</p>';
}
}
?>


I've tried creating a simple mail form. The form itself is on my index.html page, but submits to a separate "thank you for your submission" page, thankyou.php, where the above php code is embedded.
The code submits perfectly, but never sends an email. please help.







php html email






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Aug 4 '17 at 12:17









Machavity

24.1k135478




24.1k135478










asked Jul 9 '14 at 2:18









user3818620

2,024373




2,024373








  • 1




    is your program in local computer or live host
    – Tamil Selvan C
    Jul 9 '14 at 2:20






  • 6




    1. are you sure it isn't going into your spam? 2. do you have sendmail configured on your box?
    – Steven Moseley
    Jul 9 '14 at 2:21










  • its on a live host it is not going into my spam..i'm not sure what you mean by having sendmail configured on my box so i assume not?
    – user3818620
    Jul 9 '14 at 2:22










  • try send without $from
    – Artem Ilchenko
    Nov 8 '17 at 14:31














  • 1




    is your program in local computer or live host
    – Tamil Selvan C
    Jul 9 '14 at 2:20






  • 6




    1. are you sure it isn't going into your spam? 2. do you have sendmail configured on your box?
    – Steven Moseley
    Jul 9 '14 at 2:21










  • its on a live host it is not going into my spam..i'm not sure what you mean by having sendmail configured on my box so i assume not?
    – user3818620
    Jul 9 '14 at 2:22










  • try send without $from
    – Artem Ilchenko
    Nov 8 '17 at 14:31








1




1




is your program in local computer or live host
– Tamil Selvan C
Jul 9 '14 at 2:20




is your program in local computer or live host
– Tamil Selvan C
Jul 9 '14 at 2:20




6




6




1. are you sure it isn't going into your spam? 2. do you have sendmail configured on your box?
– Steven Moseley
Jul 9 '14 at 2:21




1. are you sure it isn't going into your spam? 2. do you have sendmail configured on your box?
– Steven Moseley
Jul 9 '14 at 2:21












its on a live host it is not going into my spam..i'm not sure what you mean by having sendmail configured on my box so i assume not?
– user3818620
Jul 9 '14 at 2:22




its on a live host it is not going into my spam..i'm not sure what you mean by having sendmail configured on my box so i assume not?
– user3818620
Jul 9 '14 at 2:22












try send without $from
– Artem Ilchenko
Nov 8 '17 at 14:31




try send without $from
– Artem Ilchenko
Nov 8 '17 at 14:31












24 Answers
24






active

oldest

votes

















up vote
419
down vote













Although there are portions of this answer that apply to only to the usage of themail() function itself, many of these troubleshooting steps can be applied to any PHP mailing system.



There are a variety of reasons your script appears to not be sending emails. It's difficult to diagnose these things unless there is an obvious syntax error. Without one you need to run through the checklist below to find any potential pitfalls you may be encountering.



Make sure error reporting is enabled and set to report all errors



Error reporting is essential to rooting out bugs in your code and general errors that PHP encounters. Error reporting needs to be enabled to receive these errors. Placing the following code at the top of your PHP files (or in a master configuration file) will enable error reporting.



error_reporting(-1);
ini_set('display_errors', 'On');
set_error_handler("var_dump");


See this Stack Overflow answer for more details on this.



Make sure the mail() function is called



It may seem silly but a common error is to forget to actually place the mail() function in your code. Make sure it is there and not commented out.



Make sure the mail() function is called correctly




bool mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )




The mail function takes three required parameters and optionally a fourth and fifth one. If your call to mail() does not have at least three parameters it will fail.



If your call to mail() does not have the correct parameters in the correct order it will also fail.



Check the server's mail logs



Your web server should be logging all attempts to send emails through it. The location of these logs will vary (you may need to ask your server administrator where they are located) but they can commonly be found in a user's root directory under logs. Inside will be error messages the server reported, if any, related to your attempts to send emails.



Check for Port connection failure



Port block is a very common problem which most developers face while integrating their code to deliver emails using SMTP. And, this can be easily traced at the server maillogs (the location of server of mail log can vary from server to server, as explained above). In case you are on a shared hosting server, the ports 25 and 587 remain blocked by default. This block is been purposely done by your hosting provider. This is true even for some of the dedicated servers. When these ports are blocked, try to connect using port 2525. If you find that port is also blocked, then the only solution is to contact your hosting provider to unblock these ports.



Most of the hosting providers block these email ports to protect their network from sending any spam emails.



Use ports 25 or 587 for plain/TLS connections and port 465 for SSL connections. For most users, it is suggested to use port 587 to avoid rate limits set by some hosting providers.



Don't use the error suppression operator



When the error suppression operator @ is prepended to an expression in PHP, any error messages that might be generated by that expression will be ignored. There are circumstances where using this operator is necessary but sending mail is not one of them.



If your code contains @mail(...) then you may be hiding important error messages that will help you debug this. Remove the @ and see if any errors are reported.



It's only advisable when you check with error_get_last() right afterwards for concrete failures.



Check the mail() return value



The mail() function:




Returns TRUE if the mail was successfully accepted for delivery, FALSE otherwise. It is important to note that just because the mail was accepted for delivery, it does NOT mean the mail will actually reach the intended destination.




This is important to note because:




  • If you receive a FALSE return value you know the error lies with your server accepting your mail. This probably isn't a coding issue but a server configuration issue. You need to speak to your system administrator to find out why this is happening.

  • If your receive a TRUE return value it does not mean your email will definitely be sent. It just means the email was sent to its respective handler on the server successfully by PHP. There are still more points of failure outside of PHP's control that can cause the email to not be sent.


So FALSE will help point you in the right direction whereas TRUE does not necessarily mean your email was sent successfully. This is important to note!



Make sure your hosting provider allows you to send emails and does not limit mail sending



Many shared webhosts, especially free webhosting providers, either do not allow emails to be sent from their servers or limit the amount that can be sent during any given time period. This is due to their efforts to limit spammers from taking advantage of their cheaper services.



If you think your host has emailing limits or blocks the sending of emails, check their FAQs to see if they list any such limitations. Otherwise, you may need to reach out to their support to verify if there are any restrictions in place around the sending of emails.



Check spam folders; prevent emails from being flagged as spam



Oftentimes, for various reasons, emails sent through PHP (and other server-side programming languages) end up in a recipient's spam folder. Always check there before troubleshooting your code.



To avoid mail sent through PHP from being sent to a recipient's spam folder, there are various things you can do, both in your PHP code and otherwise, to minimize the chances your emails are marked as spam. Good tips from Michiel de Mare include:





  • Use email authentication methods, such as SPF, and DKIM to prove that your emails and your domain name belong together, and to prevent spoofing of your domain name. The SPF website includes a wizard to generate the DNS information for your site.


  • Check your reverse DNS to make sure the IP address of your mail server points to the domain name that you use for sending mail.

  • Make sure that the IP-address that you're using is not on a blacklist

  • Make sure that the reply-to address is a valid, existing address.

  • Use the full, real name of the addressee in the To field, not just the email-address (e.g. "John Smith" <john@blacksmiths-international.com> ).

  • Monitor your abuse accounts, such as abuse@yourdomain.com and postmaster@yourdomain.com. That means - make sure that these accounts exist, read what's sent to them, and act on complaints.

  • Finally, make it really easy to unsubscribe. Otherwise, your users will unsubscribe by pressing the spam button, and that will affect your reputation.




See How do you make sure email you send programmatically is not automatically marked as spam? for more on this topic.



Make sure all mail headers are supplied



Some spam software will reject mail if it is missing common headers such as "From" and "Reply-to":



$headers = array("From: from@example.com",
"Reply-To: replyto@example.com",
"X-Mailer: PHP/" . PHP_VERSION
);
$headers = implode("rn", $headers);
mail($to, $subject, $message, $headers);


Make sure mail headers have no syntax errors



Invalid headers are just as bad as having no headers. One incorrect character could be all it takes to derail your email. Double-check to make sure your syntax is correct as PHP will not catch these errors for you.



$headers = array("From from@example.com", // missing colon
"Reply To: replyto@example.com", // missing hyphen
"X-Mailer: "PHP"/" . PHP_VERSION // bad quotes
);


Make sure the recipient value is correct



Sometimes the problem is as simple as having an incorrect value for the recipient of the email. This can be due to using an incorrect variable.



$to = 'user@example.com';
// other variables ....
mail($recipient, $subject, $message, $headers); // $recipient should be $to


Another way to test this is to hard code the recipient value into the mail() function call:



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


This can apply to all of the mail() parameters.



Send to multiple accounts



To help rule out email account issues, send your email to multiple email accounts at different email providers. If your emails are not arriving at a user's Gmail account, send the same emails to a Yahoo account, a Hotmail account, and a regular POP3 account (like your ISP-provided email account).



If the emails arrive at all or some of the other email accounts, you know your code is sending emails but it is likely that the email account provider is blocking them for some reason. If the email does not arrive at any email account, the problem is more likely to be related to your code.



Make sure the code matches the form method



If you have set your form method to POST, make sure you are using $_POST to look for your form values. If you have set it to GET or didn't set it at all, make sure you using $_GET to look for your form values.



Make sure your form action value points to the correct location



Make sure your form action attribute contains a value that points to your PHP mailing code.



<form action="send_email.php" method="POST">


Make sure the Web host supports sending email



Some Web hosting providers do not allow or enable the sending of emails through their servers. The reasons for this may vary but if they have disabled the sending of mail you will need to use an alternative method that uses a third party to send those emails for you.



An email to their technical support (after a trip to their online support or FAQ) should clarify if email capabilities are available on your server.



Make sure the localhost mail server is configured



If you are developing on your local workstation using WAMP, MAMP, or XAMPP, an email server is probably not installed on your workstation. Without one, PHP cannot send mail by default.



You can overcome this by installing a basic mail server. For Windows you can use the free Mercury Mail.



You can also use SMTP to send your emails. See this great answer from Vikas Dwivedi to learn how to do this.



Enable PHP's custom mail.log



In addition to your MTA's and PHP's log file, you can enable logging for the mail() function specifically. It doesn't record the complete SMTP interaction, but at least function call parameters and invocation script.



ini_set("mail.log", "/tmp/mail.log");
ini_set("mail.add_x_header", TRUE);


See http://php.net/manual/en/mail.configuration.php for details. (It's best to enable these options in the php.ini or .user.ini or .htaccess perhaps.)



Check with a mail testing service



There are various delivery and spamminess checking services you can utilize to test your MTA/webserver setup. Typically you send a mail probe To: their address, then get a delivery report and more concrete failures or analyzations later:





  • mail-tester.com (free/simple)


  • glockapps.com (free/$$$)


  • senforensics.com (signup/$$$)


  • mailtrap.io (pro/$$$)


  • ultratools/…/emailTest (free/MX checks only)

  • Various: http://www.verticalresponse.com/blog/7-email-testing-delivery-tools/


Use a different mailer



PHP's built in mail() function is handy and often gets the job done but it has its shortcomings. Fortunately there are alternatives that offer more power and flexibility including handling a lot of the issues outlined above:




  • Most popular being: PHPMailer

  • Likewise featureful: SwiftMailer

  • Or even the older PEAR::Mail.


All of which can be combined with a professional SMTP server/service provider. (Because typical 08/15 shared webhosting plans are hit or miss when it comes to email setup/configurability.)






share|improve this answer



















  • 8




    Well written, John. I would just add one more thing: Checking the mail server logs of the sending mail server (whether it is locally or remotely hosted), if possible, can often shed light on what is causing the problem. These logs should show 1) whether or not the message was received from the PHP script by the sending mail server and if so, 2) whether or not the receiving MX accepted delivery from the sending mail server.
    – mti2935
    Jul 16 '14 at 20:11










  • @mti2935 That's so helpful I am surprised I missed that. I'll be sure to add it that shortly. :)
    – John Conde
    Jul 16 '14 at 20:12










  • i would start with dont use mail(), it does such a poor job and is harder to debug than any other mail library
    – user557846
    Aug 2 '16 at 21:25






  • 1




    the same website on 000webhost sends emails successfully and fast on netai.net domain but on site88.net i have the same website i get in console that the message sends successfully but i don't receive any messages. Also in comuf.com domain i get error.(The same website on the same host works only on netai.net domain, net16.net works too)
    – Nicolas Papaconstantinou
    Sep 25 '16 at 7:42








  • 1




    my question (stackoverflow.com/questions/44866673/…) was marked as a duplicate of this one, but I still can't figure it out. Any suggestions?
    – Zachary Elkins
    Jul 2 '17 at 2:50




















up vote
32
down vote













Add mail header in mail function



$header = "From: noreply@example.comrn"; 
$header.= "MIME-Version: 1.0rn";
$header.= "Content-Type: text/html; charset=ISO-8859-1rn";
$header.= "X-Priority: 1rn";

$status = mail($to, $subject, $message, $header);

if($status)
{
echo '<p>Your mail has been sent!</p>';
} else {
echo '<p>Something went wrong, Please try again!</p>';
}





share|improve this answer






























    up vote
    21
    down vote














    1. Always try sending headers in mail function.

    2. If you are sending mail through localhost then do the smtp settings for sending mail.

    3. If you are sending mail through server then check the email sending feature is enabled on your server.






    share|improve this answer




























      up vote
      17
      down vote













      are you using SMTP configuration for sending your email?
      try using phpmailer instead. you can download the library from https://github.com/PHPMailer/PHPMailer. i created my email sending this way:



      function send_mail($email, $recipient_name, $message='')
      {
      require("phpmailer/class.phpmailer.php");

      $mail = new PHPMailer();

      $mail->CharSet="utf-8";
      $mail->IsSMTP(); // set mailer to use SMTP
      $mail->Host = "mail.example.com"; // specify main and backup server
      $mail->SMTPAuth = true; // turn on SMTP authentication
      $mail->Username = "myusername"; // SMTP username
      $mail->Password = "p@ssw0rd"; // SMTP password

      $mail->From = "me@walalang.com";
      $mail->FromName = "System-Ad";
      $mail->AddAddress($email, $recipient_name);

      $mail->WordWrap = 50; // set word wrap to 50 characters
      $mail->IsHTML(true); // set email format to HTML (true) or plain text (false)

      $mail->Subject = "This is a Sampleenter code here Email";
      $mail->Body = $message;
      $mail->AltBody = "This is the body in plain text for non-HTML mail clients";
      $mail->AddEmbeddedImage('images/logo.png', 'logo', 'logo.png');
      $mail->addAttachment('files/file.xlsx');

      if(!$mail->Send())
      {
      echo "Message could not be sent. <p>";
      echo "Mailer Error: " . $mail->ErrorInfo;
      exit;
      }

      echo "Message has been sent";
      }





      share|improve this answer

















      • 1




        I also had to use: require("phpmailer/class.smtp.php");
        – Doug
        Oct 10 '15 at 12:29


















      up vote
      13
      down vote













      Just add some headers before sending mail:



      <?php 
      $name = $_POST['name'];
      $email = $_POST['email'];
      $message = $_POST['message'];
      $from = 'From: yoursite.com';
      $to = 'contact@yoursite.com';
      $subject = 'Customer Inquiry';
      $body = "From: $namen E-Mail: $emailn Message:n $message";

      $headers .= "MIME-Version: 1.0rn";
      $headers .= "Content-type: text/htmlrn";
      $headers .= 'From: from@example.com' . "rn" .
      'Reply-To: reply@example.com' . "rn" .
      'X-Mailer: PHP/' . phpversion();

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


      And one more thing. The mail() function is not working in localhost. Upload your code to a server and try.






      share|improve this answer























      • this actually made the form submit-- my question now would be..how do i show the user's typed in email, now that i've replaced it with the header...
        – user3818620
        Jul 9 '14 at 3:38










      • will it prevent mail from spam folder. I have seen if we send mail more than 10 using this method you used in your code gmail send this mail to spam.
        – jewelhuq
        Jan 5 '16 at 12:27










      • The OP has this line: $from = 'From: yoursite.com'; Which doesn't look like a complete valid email address, there is no local part. You have replaced the From header and added additional ones. Or rather the $from above is redundant in your example. And being malformed was probably the OPs original problem.
        – Progrock
        Oct 5 '16 at 10:35




















      up vote
      12
      down vote













      it worked for me on 000webhost by doing the following :



      $headers  = "MIME-Version: 1.0" . "rn";
      $headers .= "Content-type: text/html; charset=iso-8859-1" . "rn";
      $headers .= "From: ". $from. "rn";
      $headers .= "Reply-To: ". $from. "rn";
      $headers .= "X-Mailer: PHP/" . phpversion();
      $headers .= "X-Priority: 1" . "rn";


      Enter directly the email address when sending the email



      mail('email@gmail.com', $subject, $message, $headers)


      Use '' and not ""



      This code works but the email was received with half an hour lag






      share|improve this answer






























        up vote
        9
        down vote













        Try these two thigs separately and together:




        1. remove the if($_POST['submit']){}

        2. remove $from (just my gut)






        share|improve this answer




























          up vote
          9
          down vote













          Mostly the mail() function is disabled in shared hosting.
          A better option is to use SMTP. The best option would be Gmail or SendGrid.





          SMTPconfig.php



          <?php 
          $SmtpServer="smtp.*.*";
          $SmtpPort="2525"; //default
          $SmtpUser="***";
          $SmtpPass="***";
          ?>




          SMTPmail.php



          <?php
          class SMTPClient
          {

          function SMTPClient ($SmtpServer, $SmtpPort, $SmtpUser, $SmtpPass, $from, $to, $subject, $body)
          {

          $this->SmtpServer = $SmtpServer;
          $this->SmtpUser = base64_encode ($SmtpUser);
          $this->SmtpPass = base64_encode ($SmtpPass);
          $this->from = $from;
          $this->to = $to;
          $this->subject = $subject;
          $this->body = $body;

          if ($SmtpPort == "")
          {
          $this->PortSMTP = 25;
          }
          else
          {
          $this->PortSMTP = $SmtpPort;
          }
          }

          function SendMail ()
          {
          $newLine = "rn";
          $headers = "MIME-Version: 1.0" . $newLine;
          $headers .= "Content-type: text/html; charset=iso-8859-1" . $newLine;

          if ($SMTPIN = fsockopen ($this->SmtpServer, $this->PortSMTP))
          {
          fputs ($SMTPIN, "EHLO ".$HTTP_HOST."rn");
          $talk["hello"] = fgets ( $SMTPIN, 1024 );
          fputs($SMTPIN, "auth loginrn");
          $talk["res"]=fgets($SMTPIN,1024);
          fputs($SMTPIN, $this->SmtpUser."rn");
          $talk["user"]=fgets($SMTPIN,1024);
          fputs($SMTPIN, $this->SmtpPass."rn");
          $talk["pass"]=fgets($SMTPIN,256);
          fputs ($SMTPIN, "MAIL FROM: <".$this->from.">rn");
          $talk["From"] = fgets ( $SMTPIN, 1024 );
          fputs ($SMTPIN, "RCPT TO: <".$this->to.">rn");
          $talk["To"] = fgets ($SMTPIN, 1024);
          fputs($SMTPIN, "DATArn");
          $talk["data"]=fgets( $SMTPIN,1024 );
          fputs($SMTPIN, "To: <".$this->to.">rnFrom: <".$this->from.">rn".$headers."nnSubject:".$this->subject."rnrnrn".$this->body."rn.rn");
          $talk["send"]=fgets($SMTPIN,256);
          //CLOSE CONNECTION AND EXIT ...
          fputs ($SMTPIN, "QUITrn");
          fclose($SMTPIN);
          //
          }
          return $talk;
          }
          }
          ?>




          contact_email.php



          <?php 
          include('SMTPconfig.php');
          include('SMTPmail.php');
          if($_SERVER["REQUEST_METHOD"] == "POST")
          {
          $to = "";
          $from = $_POST['email'];
          $subject = "Enquiry";
          $body = $_POST['name'].'</br>'.$_POST['companyName'].'</br>'.$_POST['tel'].'</br>'.'<hr />'.$_POST['message'];
          $SMTPMail = new SMTPClient ($SmtpServer, $SmtpPort, $SmtpUser, $SmtpPass, $from, $to, $subject, $body);
          $SMTPChat = $SMTPMail->SendMail();
          }
          ?>





          share|improve this answer























          • Is it a good idea to save the smtp user and password in plaintext on the server?
            – Juri
            Nov 27 '16 at 10:36










          • @Juri probably not... god i can't figure out how to get the mail function to work :.(
            – user2230470
            Dec 20 '16 at 5:38


















          up vote
          8
          down vote













          If you only use the mail()function, you need to complete the config file.



          You need to open the mail expansion, and set the SMTP smtp_port and so on, and most important, your username and your password. Without that, mail cannot be sent. Also, you can use PHPMail class to send.






          share|improve this answer






























            up vote
            7
            down vote













            You can use config email by codeigniter, example using smtp (simple way) :



            $config = Array(
            'protocol' => 'smtp',
            'smtp_host' => 'mail.domain.com', //your smtp host
            'smtp_port' => 26, //default port smtp
            'smtp_user' => 'name@domain.com',
            'smtp_pass' => 'password',
            'mailtype' => 'html',
            'charset' => 'iso-8859-1',
            'wordwrap' => TRUE
            );
            $message = 'Your msg';
            $this->load->library('email', $config);
            $this->email->from('name@domain.com', 'Title');
            $this->email->to('emaildestination@domain.com');
            $this->email->subject('Header');
            $this->email->message($message);

            if($this->email->send())
            {
            //conditional true
            }


            It's works for me!






            share|improve this answer





















            • fsockopen() [function.fsockopen]: php_network_getaddresses: getaddrinfo failed: No such host is known. - If i encounter this error, then what is the reason and what needs to be done?
              – Choudhury Saadmaan Mahmid
              Aug 24 '15 at 4:18


















            up vote
            7
            down vote













            For anyone who finds this going forward, I would not recommend using mail. There's some answers that touch on this, but not the why of it.



            PHP's mail function is not only opaque, it fully relies on whatever MTA you use (i.e. Sendmail) to do the work. mail will ONLY tell you if the MTA failed to accept it (i.e. Sendmail was down when you tried to send). It cannot tell you if the mail was successful because it's handed it off. As such (as John Conde's answer details), you now get to fiddle with the logs of the MTA and hope that it tells you enough about the failure to fix it. If you're on a shared host or don't have access to the MTA logs, you're out of luck. Sadly, the default for most vanilla installs for Linux handle it this way.



            A mail library (PHPMailer, Zend Framework 2+, etc), does something very different from mail. What they do is they open a socket directly to the receiving mail server and then send the SMTP mail commands directly over that socket. In other words, the class acts as its own MTA (note that you can tell the libraries to use mail to ultimately send the mail, but I would strongly recommend you not do that).



            What this means for you is that you can then directly see the responses from the receiving server (in PHPMailer, for instance, you can turn on debugging output). No more guessing if a mail failed to send or why.




            If you're using SMTP (i.e. you're calling isSMTP()), you can get a detailed transcript of the SMTP conversation using the SMTPDebug property.



            Set this option by including a line like this in your script:



            $mail->SMTPDebug = 2;



            You also get the benefit of a better interface. With mail you have to set up all your headers, attachments, etc. With a library, you have a dedicated function to do that. It also means the function is doing all the tricky parts (like headers).






            share|improve this answer




























              up vote
              7
              down vote













              I think this should do the trick. I just added an if(isset and added concatenation to the variables in the body to separate PHP from HTML.



              <?php
              $name = $_POST['name'];
              $email = $_POST['email'];
              $message = $_POST['message'];
              $from = 'From: yoursite.com';
              $to = 'contact@yoursite.com';
              $subject = 'Customer Inquiry';
              $body = "From:" .$name."rn E-Mail:" .$email."rn Message:rn" .$message;

              if (isset($_POST['submit']))
              {
              if (mail ($to, $subject, $body, $from))
              {
              echo '<p>Your message has been sent!</p>';
              }
              else
              {
              echo '<p>Something went wrong, go back and try again!</p>';
              }
              }

              ?>





              share|improve this answer






























                up vote
                5
                down vote













                $name = $_POST['name'];
                $email = $_POST['email'];
                $reciver = '/* Reciver Email address */';
                if (filter_var($reciver, FILTER_VALIDATE_EMAIL)) {
                $subject = $name;
                // To send HTML mail, the Content-type header must be set.
                $headers = 'MIME-Version: 1.0' . "rn";
                $headers .= 'Content-type: text/html; charset=iso-8859-1' . "rn";
                $headers .= 'From:' . $email. "rn"; // Sender's Email
                //$headers .= 'Cc:' . $email. "rn"; // Carbon copy to Sender
                $template = '<div style="padding:50px; color:white;">Hello ,<br/>'
                . '<br/><br/>'
                . 'Name:' .$name.'<br/>'
                . 'Email:' .$email.'<br/>'
                . '<br/>'
                . '</div>';
                $sendmessage = "<div style="background-color:#7E7E7E; color:white;">" . $template . "</div>";
                // Message lines should not exceed 70 characters (PHP rule), so wrap it.
                $sendmessage = wordwrap($sendmessage, 70);
                // Send mail by PHP Mail Function.
                mail($reciver, $subject, $sendmessage, $headers);
                echo "Your Query has been received, We will contact you soon.";
                } else {
                echo "<span>* invalid email *</span>";
                }





                share|improve this answer






























                  up vote
                  5
                  down vote













                  Try this



                  <?php
                  $to = "somebody@example.com, somebodyelse@example.com";
                  $subject = "HTML email";

                  $message = "
                  <html>
                  <head>
                  <title>HTML email</title>
                  </head>
                  <body>
                  <p>This email contains HTML Tags!</p>
                  <table>
                  <tr>
                  <th>Firstname</th>
                  <th>Lastname</th>
                  </tr>
                  <tr>
                  <td>John</td>
                  <td>Doe</td>
                  </tr>
                  </table>
                  </body>
                  </html>";

                  // Always set content-type when sending HTML email
                  $headers = "MIME-Version: 1.0" . "rn";
                  $headers .= "Content-type:text/html;charset=UTF-8" . "rn";

                  // More headers
                  $headers .= 'From: <webmaster@example.com>' . "rn";
                  $headers .= 'Cc: myboss@example.com' . "rn";

                  mail($to,$subject,$message,$headers);
                  ?>





                  share|improve this answer



















                  • 7




                    Generally, answers are much more helpful if they include an explanation of what the code is intended to do, and why that solves the problem without introducing others.
                    – IKavanagh
                    Sep 15 '15 at 7:58


















                  up vote
                  3
                  down vote













                  Try this



                  if ($_POST['submit']) {
                  $success= mail($to, $subject, $body, $from);
                  if($success)
                  {
                  echo '
                  <p>Your message has been sent!</p>
                  ';
                  } else {
                  echo '
                  <p>Something went wrong, go back and try again!</p>
                  ';
                  }
                  }





                  share|improve this answer






























                    up vote
                    3
                    down vote













                    Make sure you have Sendmail installed in your server.



                    If you have checked your code and verified that there is nothing wrong there, go to /var/mail and check whether that folder is empty.



                    If it is empty, you will need to do a:



                    sudo apt-get install sendmail


                    if you are on an Ubuntu server.






                    share|improve this answer





















                    • That's way too coarse to be useful. Merely installing an MTA will seldomly allow to get to it. And a full sendmail/postfix/qmail setup guide would be off-topic here.
                      – mario
                      Nov 16 '17 at 17:24










                    • Is this comment in reference to my answer? Because this hasn't happened to me and I didn't solve it this same exact way. There are various possible answers to the question, mine being one of them after having tried all of the above in the past, my answer had solved the problem for me, so I offered it here as another potential solution.
                      – Daniel
                      Nov 17 '17 at 15:22










                    • Yes sure. It's one of many possible answers. However, it's still way to superficial IMO. Generalizing it into "you need an MTA" such as … - and here are a few setup guides linked… might be more practical however.
                      – mario
                      Nov 17 '17 at 15:28


















                    up vote
                    2
                    down vote













                    If you're having trouble sending mails with PHP, consider an alternative like PHPMailer or SwiftMailer.



                    I usually use SwiftMailer whenever I need to send mails with PHP.





                    Basic usage :



                    require 'mail/swift_required.php';

                    $message = Swift_Message::newInstance()
                    // The subject of your email
                    ->setSubject('Jane Doe sends you a message')
                    // The from address(es)
                    ->setFrom(array('jane.doe@gmail.com' => 'Jane Doe'))
                    // The to address(es)
                    ->setTo(array('frank.stevens@gmail.com' => 'Frank Stevens'))
                    // Here, you put the content of your email
                    ->setBody('<h3>New message</h3><p>Here goes the rest of my message</p>', 'text/html');

                    if (Swift_Mailer::newInstance(Swift_MailTransport::newInstance())->send($message)) {
                    echo json_encode([
                    "status" => "OK",
                    "message" => 'Your message has been sent!'
                    ], JSON_PRETTY_PRINT);
                    } else {
                    echo json_encode([
                    "status" => "error",
                    "message" => 'Oops! Something went wrong!'
                    ], JSON_PRETTY_PRINT);
                    }


                    See the official documentation for more info on how to use SwiftMailer.






                    share|improve this answer




























                      up vote
                      2
                      down vote













                      Maybe the problem is the configuration of the mail server, to avoid this type of problems or you do not have to worry about the mail server problem, I recommend you use PHPMailer, it is a plugin that has everything necessary to send mail, the only thing you have to take into account is to have the SMTP port (Port: 25 and 465), enabled



                      require_once 'PHPMailer/PHPMailer.php';
                      require_once '/servicios/PHPMailer/SMTP.php';
                      require_once '/servicios/PHPMailer/Exception.php';

                      $mail = new PHPMailerPHPMailerPHPMailer(true);
                      try {
                      //Server settings
                      $mail->SMTPDebug = 0;
                      $mail->isSMTP();
                      $mail->Host = 'smtp.gmail.com';
                      $mail->SMTPAuth = true;
                      $mail->Username = 'correo@gmail.com';
                      $mail->Password = 'contrasenia';
                      $mail->SMTPSecure = 'ssl';
                      $mail->Port = 465;

                      //Recipients
                      $mail->setFrom('correo@gmail.com', 'my name');
                      $mail->addAddress('destination@correo.com');

                      //Attachments
                      $mail->addAttachment('optional file'); // Add files, is optional

                      //Content
                      $mail->isHTML(true);// Set email format to HTML
                      $mail->Subject = utf8_decode("subject");
                      $mail->Body = utf8_decode("mail content");
                      $mail->AltBody = '';
                      $mail->send();
                      }
                      catch (Exception $e){
                      $error = $mail->ErrorInfo;
                      }





                      share|improve this answer




























                        up vote
                        2
                        down vote













                        For those who do not want to use external mailers and want to mail() on a dedicated linux server.



                        The way, how php mails, is described in php.ini in section [mail function].
                        Parameter sendmail-path describes how sendmail is called. Default value is sendmail -t -i, so if you get working sendmail -t -i < message.txt in linux console - you will be done. You could also add mail.log to debug and be sure mail() is really called.



                        Different MTA can implement sendmail, they just make symbolic link to their binaries on that name. For example, in debian default is postfix. Configure your MTA to send mail and test it from console with sendmail -v -t -i < message.txt. File message.txt should contain all headers of a message and a body, destination addres for envelope will be taken from To: header. Example:



                        From: myapp@example.com
                        To: mymail@example.com
                        Subject: Test mail via sendmail.

                        Text body.


                        I prefere to use ssmtp as MTA because it is simple and do not require running daemon with opened ports. ssmtp fits only for sending mail from local host, it also can send authenticated email via your account on a public mail service. Install ssmtp and edit config /etc/ssmtp/ssmtp.conf. To be able also to recive local system mail to unix accounts(alerts to root from cron jobs, for example) configure /etc/ssmtp/revaliases file.



                        Here is my config for my account on Yandex mail:



                        root=mymail@example.com
                        mailhub=smtp.yandex.ru:465
                        FromLineOverride=YES
                        UseTLS=YES
                        AuthUser=abcde@yandex.ru
                        AuthPass=password





                        share|improve this answer






























                          up vote
                          1
                          down vote













                          First of all,



                          You might have to many parameters for the mail() function...
                          You are able to have 5 max. mail(to,subject,message,headers,parameters);
                          As far as the $from variable goes, that should automatically come from your webhost if your using linux cPanel. It automatically comes from your cPanel username and ip address.



                          $name = $_POST['name'];
                          $email = $_POST['email'];
                          $message = $_POST['message'];
                          $from = 'From: yoursite.com';
                          $to = 'contact@yoursite.com';
                          $subject = 'Customer Inquiry';
                          $body = "From: $namen E-Mail: $emailn Message:n $message";


                          Also make sure you have the correct order of variables in your mail() function.
                          the mail($to,$subject,$message,etc.) in that order, or else there is a chance of it not working.
                          Let me know if this helps...






                          share|improve this answer




























                            up vote
                            1
                            down vote













                            This will only affect a small handful of users, but I'd like it documented for that small handful. This member of that small handful spent 6 hours troubleshooting a working PHP mail script because of this issue.



                            If you're going to a university that runs XAMPP from www.AceITLab.com, you should know what our professor didn't tell us: The AceITLab firewall (not the Windows firewall) blocks MercuryMail in XAMPP. You'll have to use an alternative mail client, pear is working for us. You'll have to send to a Gmail account with low security settings.



                            Yes, I know, this is totally useless for real world email. However, from what I've seen, academic settings and the real world often have precious little in common.






                            share|improve this answer























                            • That's too oddly specific, and will indeed help noone else. Use the [delete] link below your answer please.
                              – mario
                              Nov 16 '17 at 17:21


















                            up vote
                            1
                            down vote













                            You can use empty() and isset() functions. If you want to make it work with different files, just modify the action='yourphp.php' to the html I'm giving you, and store the PHP script to that yourphp.php file. Also you need to change your index.html into index.php to activate PHP functionality.



                            PHP



                            <?php

                            error_reporting(0);
                            $name = $_POST['name'];
                            $email = $_POST['email'];
                            $message = $_POST['message'];
                            $from = 'From: yoursite.com';
                            $to = 'contact@yoursite.com';
                            $subject = 'Customer Inquiry';
                            $body = "From: $namen E-Mail: $emailn Message:n $message";


                            if ($_POST['submit']){
                            if (!(empty($_POST['name']))) {
                            if (!(empty($_POST['email']))){
                            if (!(empty($_POST['message']))){
                            mail ($to, $subject, $body, $from);
                            echo '<p>Your message has been sent!</p>';
                            }else{
                            echo '<p>Fill your message please.</p>';}
                            }else {
                            echo '<p>Fill your email please.</p>';}
                            }else{
                            echo '<p>Fill your name please.</p>';}
                            }else{
                            echo '<p>Fill the form.</p>';}
                            ?>


                            HTML



                            <html>
                            <form method="post" action="?">
                            <table>
                            <tr><td>Name</td><td><input type='text' name='name' id='name'/></td></tr>
                            <tr><td>Email</td><td><input type='text' name='email' id='email'/></td></tr>
                            <tr><td>Message</td><td><input type='text' name='message' id='message'/></td></tr>
                            <tr><td></td><td><input type='submit' name='submit' id='submit'/></td></tr>
                            </table>
                            </form>
                            </html>


                            Best Regards!






                            share|improve this answer






























                              up vote
                              0
                              down vote













                              If you are running this code on a local server (i.e your computer for development purposes) it wont send the email to the recipient. What will happen is, it will create a .txt file in a folder named mailoutput.



                              In the case if you are using a free hosing service like 000webhost or hostinger, those service providers disable the mail() function to prevent unintended uses of email spoofing, spamming etc. I prefer you to contact them to see whether they support this feature.



                              If you are sure that the service provider supports the mail() function, you can check this PHP manual for further reference,
                              PHP mail()



                              To check weather your hosting service support the mail() function, try running this code, (Remember to change the recipient email address)



                              <?php
                              $to = 'nobody@example.com';
                              $subject = 'the subject';
                              $message = 'hello';
                              $headers = 'From: webmaster@example.com' . "rn" .
                              'Reply-To: webmaster@example.com' . "rn" .
                              'X-Mailer: PHP/' . phpversion();

                              mail($to, $subject, $message, $headers);
                              ?>


                              Hope this helped.






                              share|improve this answer




























                                up vote
                                0
                                down vote













                                You can use libmail: http://lwest.free.fr/doc/php/lib/index.php3?page=mail&lang=en



                                include "libmail.php";
                                $m = new Mail(); // create the mail
                                $m->From( $_POST['form'] );
                                $m->To( $_POST['to'] );
                                $m->Subject( $_POST['subject'] );
                                $m->Body( $_POST['body'] );
                                $m->Cc( $_POST['cc']);
                                $m->Priority(4);
                                // attach a file of type image/gif to be displayed in the message if possible
                                $m->Attach( "/home/leo/toto.gif", "image/gif", "inline" );
                                $m->Send(); // send the mail
                                echo "Mail was sent:"
                                echo $m->Get(); // show the mail source





                                share|improve this answer

















                                • 4




                                  Important to mention: this is merely a mail() function wrapper. Only adds message construction and some gimmicks.
                                  – mario
                                  Nov 16 '17 at 17:18










                                protected by Samuel Liew Oct 5 '15 at 8:58



                                Thank you for your interest in this question.
                                Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).



                                Would you like to answer one of these unanswered questions instead?














                                24 Answers
                                24






                                active

                                oldest

                                votes








                                24 Answers
                                24






                                active

                                oldest

                                votes









                                active

                                oldest

                                votes






                                active

                                oldest

                                votes








                                up vote
                                419
                                down vote













                                Although there are portions of this answer that apply to only to the usage of themail() function itself, many of these troubleshooting steps can be applied to any PHP mailing system.



                                There are a variety of reasons your script appears to not be sending emails. It's difficult to diagnose these things unless there is an obvious syntax error. Without one you need to run through the checklist below to find any potential pitfalls you may be encountering.



                                Make sure error reporting is enabled and set to report all errors



                                Error reporting is essential to rooting out bugs in your code and general errors that PHP encounters. Error reporting needs to be enabled to receive these errors. Placing the following code at the top of your PHP files (or in a master configuration file) will enable error reporting.



                                error_reporting(-1);
                                ini_set('display_errors', 'On');
                                set_error_handler("var_dump");


                                See this Stack Overflow answer for more details on this.



                                Make sure the mail() function is called



                                It may seem silly but a common error is to forget to actually place the mail() function in your code. Make sure it is there and not commented out.



                                Make sure the mail() function is called correctly




                                bool mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )




                                The mail function takes three required parameters and optionally a fourth and fifth one. If your call to mail() does not have at least three parameters it will fail.



                                If your call to mail() does not have the correct parameters in the correct order it will also fail.



                                Check the server's mail logs



                                Your web server should be logging all attempts to send emails through it. The location of these logs will vary (you may need to ask your server administrator where they are located) but they can commonly be found in a user's root directory under logs. Inside will be error messages the server reported, if any, related to your attempts to send emails.



                                Check for Port connection failure



                                Port block is a very common problem which most developers face while integrating their code to deliver emails using SMTP. And, this can be easily traced at the server maillogs (the location of server of mail log can vary from server to server, as explained above). In case you are on a shared hosting server, the ports 25 and 587 remain blocked by default. This block is been purposely done by your hosting provider. This is true even for some of the dedicated servers. When these ports are blocked, try to connect using port 2525. If you find that port is also blocked, then the only solution is to contact your hosting provider to unblock these ports.



                                Most of the hosting providers block these email ports to protect their network from sending any spam emails.



                                Use ports 25 or 587 for plain/TLS connections and port 465 for SSL connections. For most users, it is suggested to use port 587 to avoid rate limits set by some hosting providers.



                                Don't use the error suppression operator



                                When the error suppression operator @ is prepended to an expression in PHP, any error messages that might be generated by that expression will be ignored. There are circumstances where using this operator is necessary but sending mail is not one of them.



                                If your code contains @mail(...) then you may be hiding important error messages that will help you debug this. Remove the @ and see if any errors are reported.



                                It's only advisable when you check with error_get_last() right afterwards for concrete failures.



                                Check the mail() return value



                                The mail() function:




                                Returns TRUE if the mail was successfully accepted for delivery, FALSE otherwise. It is important to note that just because the mail was accepted for delivery, it does NOT mean the mail will actually reach the intended destination.




                                This is important to note because:




                                • If you receive a FALSE return value you know the error lies with your server accepting your mail. This probably isn't a coding issue but a server configuration issue. You need to speak to your system administrator to find out why this is happening.

                                • If your receive a TRUE return value it does not mean your email will definitely be sent. It just means the email was sent to its respective handler on the server successfully by PHP. There are still more points of failure outside of PHP's control that can cause the email to not be sent.


                                So FALSE will help point you in the right direction whereas TRUE does not necessarily mean your email was sent successfully. This is important to note!



                                Make sure your hosting provider allows you to send emails and does not limit mail sending



                                Many shared webhosts, especially free webhosting providers, either do not allow emails to be sent from their servers or limit the amount that can be sent during any given time period. This is due to their efforts to limit spammers from taking advantage of their cheaper services.



                                If you think your host has emailing limits or blocks the sending of emails, check their FAQs to see if they list any such limitations. Otherwise, you may need to reach out to their support to verify if there are any restrictions in place around the sending of emails.



                                Check spam folders; prevent emails from being flagged as spam



                                Oftentimes, for various reasons, emails sent through PHP (and other server-side programming languages) end up in a recipient's spam folder. Always check there before troubleshooting your code.



                                To avoid mail sent through PHP from being sent to a recipient's spam folder, there are various things you can do, both in your PHP code and otherwise, to minimize the chances your emails are marked as spam. Good tips from Michiel de Mare include:





                                • Use email authentication methods, such as SPF, and DKIM to prove that your emails and your domain name belong together, and to prevent spoofing of your domain name. The SPF website includes a wizard to generate the DNS information for your site.


                                • Check your reverse DNS to make sure the IP address of your mail server points to the domain name that you use for sending mail.

                                • Make sure that the IP-address that you're using is not on a blacklist

                                • Make sure that the reply-to address is a valid, existing address.

                                • Use the full, real name of the addressee in the To field, not just the email-address (e.g. "John Smith" <john@blacksmiths-international.com> ).

                                • Monitor your abuse accounts, such as abuse@yourdomain.com and postmaster@yourdomain.com. That means - make sure that these accounts exist, read what's sent to them, and act on complaints.

                                • Finally, make it really easy to unsubscribe. Otherwise, your users will unsubscribe by pressing the spam button, and that will affect your reputation.




                                See How do you make sure email you send programmatically is not automatically marked as spam? for more on this topic.



                                Make sure all mail headers are supplied



                                Some spam software will reject mail if it is missing common headers such as "From" and "Reply-to":



                                $headers = array("From: from@example.com",
                                "Reply-To: replyto@example.com",
                                "X-Mailer: PHP/" . PHP_VERSION
                                );
                                $headers = implode("rn", $headers);
                                mail($to, $subject, $message, $headers);


                                Make sure mail headers have no syntax errors



                                Invalid headers are just as bad as having no headers. One incorrect character could be all it takes to derail your email. Double-check to make sure your syntax is correct as PHP will not catch these errors for you.



                                $headers = array("From from@example.com", // missing colon
                                "Reply To: replyto@example.com", // missing hyphen
                                "X-Mailer: "PHP"/" . PHP_VERSION // bad quotes
                                );


                                Make sure the recipient value is correct



                                Sometimes the problem is as simple as having an incorrect value for the recipient of the email. This can be due to using an incorrect variable.



                                $to = 'user@example.com';
                                // other variables ....
                                mail($recipient, $subject, $message, $headers); // $recipient should be $to


                                Another way to test this is to hard code the recipient value into the mail() function call:



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


                                This can apply to all of the mail() parameters.



                                Send to multiple accounts



                                To help rule out email account issues, send your email to multiple email accounts at different email providers. If your emails are not arriving at a user's Gmail account, send the same emails to a Yahoo account, a Hotmail account, and a regular POP3 account (like your ISP-provided email account).



                                If the emails arrive at all or some of the other email accounts, you know your code is sending emails but it is likely that the email account provider is blocking them for some reason. If the email does not arrive at any email account, the problem is more likely to be related to your code.



                                Make sure the code matches the form method



                                If you have set your form method to POST, make sure you are using $_POST to look for your form values. If you have set it to GET or didn't set it at all, make sure you using $_GET to look for your form values.



                                Make sure your form action value points to the correct location



                                Make sure your form action attribute contains a value that points to your PHP mailing code.



                                <form action="send_email.php" method="POST">


                                Make sure the Web host supports sending email



                                Some Web hosting providers do not allow or enable the sending of emails through their servers. The reasons for this may vary but if they have disabled the sending of mail you will need to use an alternative method that uses a third party to send those emails for you.



                                An email to their technical support (after a trip to their online support or FAQ) should clarify if email capabilities are available on your server.



                                Make sure the localhost mail server is configured



                                If you are developing on your local workstation using WAMP, MAMP, or XAMPP, an email server is probably not installed on your workstation. Without one, PHP cannot send mail by default.



                                You can overcome this by installing a basic mail server. For Windows you can use the free Mercury Mail.



                                You can also use SMTP to send your emails. See this great answer from Vikas Dwivedi to learn how to do this.



                                Enable PHP's custom mail.log



                                In addition to your MTA's and PHP's log file, you can enable logging for the mail() function specifically. It doesn't record the complete SMTP interaction, but at least function call parameters and invocation script.



                                ini_set("mail.log", "/tmp/mail.log");
                                ini_set("mail.add_x_header", TRUE);


                                See http://php.net/manual/en/mail.configuration.php for details. (It's best to enable these options in the php.ini or .user.ini or .htaccess perhaps.)



                                Check with a mail testing service



                                There are various delivery and spamminess checking services you can utilize to test your MTA/webserver setup. Typically you send a mail probe To: their address, then get a delivery report and more concrete failures or analyzations later:





                                • mail-tester.com (free/simple)


                                • glockapps.com (free/$$$)


                                • senforensics.com (signup/$$$)


                                • mailtrap.io (pro/$$$)


                                • ultratools/…/emailTest (free/MX checks only)

                                • Various: http://www.verticalresponse.com/blog/7-email-testing-delivery-tools/


                                Use a different mailer



                                PHP's built in mail() function is handy and often gets the job done but it has its shortcomings. Fortunately there are alternatives that offer more power and flexibility including handling a lot of the issues outlined above:




                                • Most popular being: PHPMailer

                                • Likewise featureful: SwiftMailer

                                • Or even the older PEAR::Mail.


                                All of which can be combined with a professional SMTP server/service provider. (Because typical 08/15 shared webhosting plans are hit or miss when it comes to email setup/configurability.)






                                share|improve this answer



















                                • 8




                                  Well written, John. I would just add one more thing: Checking the mail server logs of the sending mail server (whether it is locally or remotely hosted), if possible, can often shed light on what is causing the problem. These logs should show 1) whether or not the message was received from the PHP script by the sending mail server and if so, 2) whether or not the receiving MX accepted delivery from the sending mail server.
                                  – mti2935
                                  Jul 16 '14 at 20:11










                                • @mti2935 That's so helpful I am surprised I missed that. I'll be sure to add it that shortly. :)
                                  – John Conde
                                  Jul 16 '14 at 20:12










                                • i would start with dont use mail(), it does such a poor job and is harder to debug than any other mail library
                                  – user557846
                                  Aug 2 '16 at 21:25






                                • 1




                                  the same website on 000webhost sends emails successfully and fast on netai.net domain but on site88.net i have the same website i get in console that the message sends successfully but i don't receive any messages. Also in comuf.com domain i get error.(The same website on the same host works only on netai.net domain, net16.net works too)
                                  – Nicolas Papaconstantinou
                                  Sep 25 '16 at 7:42








                                • 1




                                  my question (stackoverflow.com/questions/44866673/…) was marked as a duplicate of this one, but I still can't figure it out. Any suggestions?
                                  – Zachary Elkins
                                  Jul 2 '17 at 2:50

















                                up vote
                                419
                                down vote













                                Although there are portions of this answer that apply to only to the usage of themail() function itself, many of these troubleshooting steps can be applied to any PHP mailing system.



                                There are a variety of reasons your script appears to not be sending emails. It's difficult to diagnose these things unless there is an obvious syntax error. Without one you need to run through the checklist below to find any potential pitfalls you may be encountering.



                                Make sure error reporting is enabled and set to report all errors



                                Error reporting is essential to rooting out bugs in your code and general errors that PHP encounters. Error reporting needs to be enabled to receive these errors. Placing the following code at the top of your PHP files (or in a master configuration file) will enable error reporting.



                                error_reporting(-1);
                                ini_set('display_errors', 'On');
                                set_error_handler("var_dump");


                                See this Stack Overflow answer for more details on this.



                                Make sure the mail() function is called



                                It may seem silly but a common error is to forget to actually place the mail() function in your code. Make sure it is there and not commented out.



                                Make sure the mail() function is called correctly




                                bool mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )




                                The mail function takes three required parameters and optionally a fourth and fifth one. If your call to mail() does not have at least three parameters it will fail.



                                If your call to mail() does not have the correct parameters in the correct order it will also fail.



                                Check the server's mail logs



                                Your web server should be logging all attempts to send emails through it. The location of these logs will vary (you may need to ask your server administrator where they are located) but they can commonly be found in a user's root directory under logs. Inside will be error messages the server reported, if any, related to your attempts to send emails.



                                Check for Port connection failure



                                Port block is a very common problem which most developers face while integrating their code to deliver emails using SMTP. And, this can be easily traced at the server maillogs (the location of server of mail log can vary from server to server, as explained above). In case you are on a shared hosting server, the ports 25 and 587 remain blocked by default. This block is been purposely done by your hosting provider. This is true even for some of the dedicated servers. When these ports are blocked, try to connect using port 2525. If you find that port is also blocked, then the only solution is to contact your hosting provider to unblock these ports.



                                Most of the hosting providers block these email ports to protect their network from sending any spam emails.



                                Use ports 25 or 587 for plain/TLS connections and port 465 for SSL connections. For most users, it is suggested to use port 587 to avoid rate limits set by some hosting providers.



                                Don't use the error suppression operator



                                When the error suppression operator @ is prepended to an expression in PHP, any error messages that might be generated by that expression will be ignored. There are circumstances where using this operator is necessary but sending mail is not one of them.



                                If your code contains @mail(...) then you may be hiding important error messages that will help you debug this. Remove the @ and see if any errors are reported.



                                It's only advisable when you check with error_get_last() right afterwards for concrete failures.



                                Check the mail() return value



                                The mail() function:




                                Returns TRUE if the mail was successfully accepted for delivery, FALSE otherwise. It is important to note that just because the mail was accepted for delivery, it does NOT mean the mail will actually reach the intended destination.




                                This is important to note because:




                                • If you receive a FALSE return value you know the error lies with your server accepting your mail. This probably isn't a coding issue but a server configuration issue. You need to speak to your system administrator to find out why this is happening.

                                • If your receive a TRUE return value it does not mean your email will definitely be sent. It just means the email was sent to its respective handler on the server successfully by PHP. There are still more points of failure outside of PHP's control that can cause the email to not be sent.


                                So FALSE will help point you in the right direction whereas TRUE does not necessarily mean your email was sent successfully. This is important to note!



                                Make sure your hosting provider allows you to send emails and does not limit mail sending



                                Many shared webhosts, especially free webhosting providers, either do not allow emails to be sent from their servers or limit the amount that can be sent during any given time period. This is due to their efforts to limit spammers from taking advantage of their cheaper services.



                                If you think your host has emailing limits or blocks the sending of emails, check their FAQs to see if they list any such limitations. Otherwise, you may need to reach out to their support to verify if there are any restrictions in place around the sending of emails.



                                Check spam folders; prevent emails from being flagged as spam



                                Oftentimes, for various reasons, emails sent through PHP (and other server-side programming languages) end up in a recipient's spam folder. Always check there before troubleshooting your code.



                                To avoid mail sent through PHP from being sent to a recipient's spam folder, there are various things you can do, both in your PHP code and otherwise, to minimize the chances your emails are marked as spam. Good tips from Michiel de Mare include:





                                • Use email authentication methods, such as SPF, and DKIM to prove that your emails and your domain name belong together, and to prevent spoofing of your domain name. The SPF website includes a wizard to generate the DNS information for your site.


                                • Check your reverse DNS to make sure the IP address of your mail server points to the domain name that you use for sending mail.

                                • Make sure that the IP-address that you're using is not on a blacklist

                                • Make sure that the reply-to address is a valid, existing address.

                                • Use the full, real name of the addressee in the To field, not just the email-address (e.g. "John Smith" <john@blacksmiths-international.com> ).

                                • Monitor your abuse accounts, such as abuse@yourdomain.com and postmaster@yourdomain.com. That means - make sure that these accounts exist, read what's sent to them, and act on complaints.

                                • Finally, make it really easy to unsubscribe. Otherwise, your users will unsubscribe by pressing the spam button, and that will affect your reputation.




                                See How do you make sure email you send programmatically is not automatically marked as spam? for more on this topic.



                                Make sure all mail headers are supplied



                                Some spam software will reject mail if it is missing common headers such as "From" and "Reply-to":



                                $headers = array("From: from@example.com",
                                "Reply-To: replyto@example.com",
                                "X-Mailer: PHP/" . PHP_VERSION
                                );
                                $headers = implode("rn", $headers);
                                mail($to, $subject, $message, $headers);


                                Make sure mail headers have no syntax errors



                                Invalid headers are just as bad as having no headers. One incorrect character could be all it takes to derail your email. Double-check to make sure your syntax is correct as PHP will not catch these errors for you.



                                $headers = array("From from@example.com", // missing colon
                                "Reply To: replyto@example.com", // missing hyphen
                                "X-Mailer: "PHP"/" . PHP_VERSION // bad quotes
                                );


                                Make sure the recipient value is correct



                                Sometimes the problem is as simple as having an incorrect value for the recipient of the email. This can be due to using an incorrect variable.



                                $to = 'user@example.com';
                                // other variables ....
                                mail($recipient, $subject, $message, $headers); // $recipient should be $to


                                Another way to test this is to hard code the recipient value into the mail() function call:



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


                                This can apply to all of the mail() parameters.



                                Send to multiple accounts



                                To help rule out email account issues, send your email to multiple email accounts at different email providers. If your emails are not arriving at a user's Gmail account, send the same emails to a Yahoo account, a Hotmail account, and a regular POP3 account (like your ISP-provided email account).



                                If the emails arrive at all or some of the other email accounts, you know your code is sending emails but it is likely that the email account provider is blocking them for some reason. If the email does not arrive at any email account, the problem is more likely to be related to your code.



                                Make sure the code matches the form method



                                If you have set your form method to POST, make sure you are using $_POST to look for your form values. If you have set it to GET or didn't set it at all, make sure you using $_GET to look for your form values.



                                Make sure your form action value points to the correct location



                                Make sure your form action attribute contains a value that points to your PHP mailing code.



                                <form action="send_email.php" method="POST">


                                Make sure the Web host supports sending email



                                Some Web hosting providers do not allow or enable the sending of emails through their servers. The reasons for this may vary but if they have disabled the sending of mail you will need to use an alternative method that uses a third party to send those emails for you.



                                An email to their technical support (after a trip to their online support or FAQ) should clarify if email capabilities are available on your server.



                                Make sure the localhost mail server is configured



                                If you are developing on your local workstation using WAMP, MAMP, or XAMPP, an email server is probably not installed on your workstation. Without one, PHP cannot send mail by default.



                                You can overcome this by installing a basic mail server. For Windows you can use the free Mercury Mail.



                                You can also use SMTP to send your emails. See this great answer from Vikas Dwivedi to learn how to do this.



                                Enable PHP's custom mail.log



                                In addition to your MTA's and PHP's log file, you can enable logging for the mail() function specifically. It doesn't record the complete SMTP interaction, but at least function call parameters and invocation script.



                                ini_set("mail.log", "/tmp/mail.log");
                                ini_set("mail.add_x_header", TRUE);


                                See http://php.net/manual/en/mail.configuration.php for details. (It's best to enable these options in the php.ini or .user.ini or .htaccess perhaps.)



                                Check with a mail testing service



                                There are various delivery and spamminess checking services you can utilize to test your MTA/webserver setup. Typically you send a mail probe To: their address, then get a delivery report and more concrete failures or analyzations later:





                                • mail-tester.com (free/simple)


                                • glockapps.com (free/$$$)


                                • senforensics.com (signup/$$$)


                                • mailtrap.io (pro/$$$)


                                • ultratools/…/emailTest (free/MX checks only)

                                • Various: http://www.verticalresponse.com/blog/7-email-testing-delivery-tools/


                                Use a different mailer



                                PHP's built in mail() function is handy and often gets the job done but it has its shortcomings. Fortunately there are alternatives that offer more power and flexibility including handling a lot of the issues outlined above:




                                • Most popular being: PHPMailer

                                • Likewise featureful: SwiftMailer

                                • Or even the older PEAR::Mail.


                                All of which can be combined with a professional SMTP server/service provider. (Because typical 08/15 shared webhosting plans are hit or miss when it comes to email setup/configurability.)






                                share|improve this answer



















                                • 8




                                  Well written, John. I would just add one more thing: Checking the mail server logs of the sending mail server (whether it is locally or remotely hosted), if possible, can often shed light on what is causing the problem. These logs should show 1) whether or not the message was received from the PHP script by the sending mail server and if so, 2) whether or not the receiving MX accepted delivery from the sending mail server.
                                  – mti2935
                                  Jul 16 '14 at 20:11










                                • @mti2935 That's so helpful I am surprised I missed that. I'll be sure to add it that shortly. :)
                                  – John Conde
                                  Jul 16 '14 at 20:12










                                • i would start with dont use mail(), it does such a poor job and is harder to debug than any other mail library
                                  – user557846
                                  Aug 2 '16 at 21:25






                                • 1




                                  the same website on 000webhost sends emails successfully and fast on netai.net domain but on site88.net i have the same website i get in console that the message sends successfully but i don't receive any messages. Also in comuf.com domain i get error.(The same website on the same host works only on netai.net domain, net16.net works too)
                                  – Nicolas Papaconstantinou
                                  Sep 25 '16 at 7:42








                                • 1




                                  my question (stackoverflow.com/questions/44866673/…) was marked as a duplicate of this one, but I still can't figure it out. Any suggestions?
                                  – Zachary Elkins
                                  Jul 2 '17 at 2:50















                                up vote
                                419
                                down vote










                                up vote
                                419
                                down vote









                                Although there are portions of this answer that apply to only to the usage of themail() function itself, many of these troubleshooting steps can be applied to any PHP mailing system.



                                There are a variety of reasons your script appears to not be sending emails. It's difficult to diagnose these things unless there is an obvious syntax error. Without one you need to run through the checklist below to find any potential pitfalls you may be encountering.



                                Make sure error reporting is enabled and set to report all errors



                                Error reporting is essential to rooting out bugs in your code and general errors that PHP encounters. Error reporting needs to be enabled to receive these errors. Placing the following code at the top of your PHP files (or in a master configuration file) will enable error reporting.



                                error_reporting(-1);
                                ini_set('display_errors', 'On');
                                set_error_handler("var_dump");


                                See this Stack Overflow answer for more details on this.



                                Make sure the mail() function is called



                                It may seem silly but a common error is to forget to actually place the mail() function in your code. Make sure it is there and not commented out.



                                Make sure the mail() function is called correctly




                                bool mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )




                                The mail function takes three required parameters and optionally a fourth and fifth one. If your call to mail() does not have at least three parameters it will fail.



                                If your call to mail() does not have the correct parameters in the correct order it will also fail.



                                Check the server's mail logs



                                Your web server should be logging all attempts to send emails through it. The location of these logs will vary (you may need to ask your server administrator where they are located) but they can commonly be found in a user's root directory under logs. Inside will be error messages the server reported, if any, related to your attempts to send emails.



                                Check for Port connection failure



                                Port block is a very common problem which most developers face while integrating their code to deliver emails using SMTP. And, this can be easily traced at the server maillogs (the location of server of mail log can vary from server to server, as explained above). In case you are on a shared hosting server, the ports 25 and 587 remain blocked by default. This block is been purposely done by your hosting provider. This is true even for some of the dedicated servers. When these ports are blocked, try to connect using port 2525. If you find that port is also blocked, then the only solution is to contact your hosting provider to unblock these ports.



                                Most of the hosting providers block these email ports to protect their network from sending any spam emails.



                                Use ports 25 or 587 for plain/TLS connections and port 465 for SSL connections. For most users, it is suggested to use port 587 to avoid rate limits set by some hosting providers.



                                Don't use the error suppression operator



                                When the error suppression operator @ is prepended to an expression in PHP, any error messages that might be generated by that expression will be ignored. There are circumstances where using this operator is necessary but sending mail is not one of them.



                                If your code contains @mail(...) then you may be hiding important error messages that will help you debug this. Remove the @ and see if any errors are reported.



                                It's only advisable when you check with error_get_last() right afterwards for concrete failures.



                                Check the mail() return value



                                The mail() function:




                                Returns TRUE if the mail was successfully accepted for delivery, FALSE otherwise. It is important to note that just because the mail was accepted for delivery, it does NOT mean the mail will actually reach the intended destination.




                                This is important to note because:




                                • If you receive a FALSE return value you know the error lies with your server accepting your mail. This probably isn't a coding issue but a server configuration issue. You need to speak to your system administrator to find out why this is happening.

                                • If your receive a TRUE return value it does not mean your email will definitely be sent. It just means the email was sent to its respective handler on the server successfully by PHP. There are still more points of failure outside of PHP's control that can cause the email to not be sent.


                                So FALSE will help point you in the right direction whereas TRUE does not necessarily mean your email was sent successfully. This is important to note!



                                Make sure your hosting provider allows you to send emails and does not limit mail sending



                                Many shared webhosts, especially free webhosting providers, either do not allow emails to be sent from their servers or limit the amount that can be sent during any given time period. This is due to their efforts to limit spammers from taking advantage of their cheaper services.



                                If you think your host has emailing limits or blocks the sending of emails, check their FAQs to see if they list any such limitations. Otherwise, you may need to reach out to their support to verify if there are any restrictions in place around the sending of emails.



                                Check spam folders; prevent emails from being flagged as spam



                                Oftentimes, for various reasons, emails sent through PHP (and other server-side programming languages) end up in a recipient's spam folder. Always check there before troubleshooting your code.



                                To avoid mail sent through PHP from being sent to a recipient's spam folder, there are various things you can do, both in your PHP code and otherwise, to minimize the chances your emails are marked as spam. Good tips from Michiel de Mare include:





                                • Use email authentication methods, such as SPF, and DKIM to prove that your emails and your domain name belong together, and to prevent spoofing of your domain name. The SPF website includes a wizard to generate the DNS information for your site.


                                • Check your reverse DNS to make sure the IP address of your mail server points to the domain name that you use for sending mail.

                                • Make sure that the IP-address that you're using is not on a blacklist

                                • Make sure that the reply-to address is a valid, existing address.

                                • Use the full, real name of the addressee in the To field, not just the email-address (e.g. "John Smith" <john@blacksmiths-international.com> ).

                                • Monitor your abuse accounts, such as abuse@yourdomain.com and postmaster@yourdomain.com. That means - make sure that these accounts exist, read what's sent to them, and act on complaints.

                                • Finally, make it really easy to unsubscribe. Otherwise, your users will unsubscribe by pressing the spam button, and that will affect your reputation.




                                See How do you make sure email you send programmatically is not automatically marked as spam? for more on this topic.



                                Make sure all mail headers are supplied



                                Some spam software will reject mail if it is missing common headers such as "From" and "Reply-to":



                                $headers = array("From: from@example.com",
                                "Reply-To: replyto@example.com",
                                "X-Mailer: PHP/" . PHP_VERSION
                                );
                                $headers = implode("rn", $headers);
                                mail($to, $subject, $message, $headers);


                                Make sure mail headers have no syntax errors



                                Invalid headers are just as bad as having no headers. One incorrect character could be all it takes to derail your email. Double-check to make sure your syntax is correct as PHP will not catch these errors for you.



                                $headers = array("From from@example.com", // missing colon
                                "Reply To: replyto@example.com", // missing hyphen
                                "X-Mailer: "PHP"/" . PHP_VERSION // bad quotes
                                );


                                Make sure the recipient value is correct



                                Sometimes the problem is as simple as having an incorrect value for the recipient of the email. This can be due to using an incorrect variable.



                                $to = 'user@example.com';
                                // other variables ....
                                mail($recipient, $subject, $message, $headers); // $recipient should be $to


                                Another way to test this is to hard code the recipient value into the mail() function call:



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


                                This can apply to all of the mail() parameters.



                                Send to multiple accounts



                                To help rule out email account issues, send your email to multiple email accounts at different email providers. If your emails are not arriving at a user's Gmail account, send the same emails to a Yahoo account, a Hotmail account, and a regular POP3 account (like your ISP-provided email account).



                                If the emails arrive at all or some of the other email accounts, you know your code is sending emails but it is likely that the email account provider is blocking them for some reason. If the email does not arrive at any email account, the problem is more likely to be related to your code.



                                Make sure the code matches the form method



                                If you have set your form method to POST, make sure you are using $_POST to look for your form values. If you have set it to GET or didn't set it at all, make sure you using $_GET to look for your form values.



                                Make sure your form action value points to the correct location



                                Make sure your form action attribute contains a value that points to your PHP mailing code.



                                <form action="send_email.php" method="POST">


                                Make sure the Web host supports sending email



                                Some Web hosting providers do not allow or enable the sending of emails through their servers. The reasons for this may vary but if they have disabled the sending of mail you will need to use an alternative method that uses a third party to send those emails for you.



                                An email to their technical support (after a trip to their online support or FAQ) should clarify if email capabilities are available on your server.



                                Make sure the localhost mail server is configured



                                If you are developing on your local workstation using WAMP, MAMP, or XAMPP, an email server is probably not installed on your workstation. Without one, PHP cannot send mail by default.



                                You can overcome this by installing a basic mail server. For Windows you can use the free Mercury Mail.



                                You can also use SMTP to send your emails. See this great answer from Vikas Dwivedi to learn how to do this.



                                Enable PHP's custom mail.log



                                In addition to your MTA's and PHP's log file, you can enable logging for the mail() function specifically. It doesn't record the complete SMTP interaction, but at least function call parameters and invocation script.



                                ini_set("mail.log", "/tmp/mail.log");
                                ini_set("mail.add_x_header", TRUE);


                                See http://php.net/manual/en/mail.configuration.php for details. (It's best to enable these options in the php.ini or .user.ini or .htaccess perhaps.)



                                Check with a mail testing service



                                There are various delivery and spamminess checking services you can utilize to test your MTA/webserver setup. Typically you send a mail probe To: their address, then get a delivery report and more concrete failures or analyzations later:





                                • mail-tester.com (free/simple)


                                • glockapps.com (free/$$$)


                                • senforensics.com (signup/$$$)


                                • mailtrap.io (pro/$$$)


                                • ultratools/…/emailTest (free/MX checks only)

                                • Various: http://www.verticalresponse.com/blog/7-email-testing-delivery-tools/


                                Use a different mailer



                                PHP's built in mail() function is handy and often gets the job done but it has its shortcomings. Fortunately there are alternatives that offer more power and flexibility including handling a lot of the issues outlined above:




                                • Most popular being: PHPMailer

                                • Likewise featureful: SwiftMailer

                                • Or even the older PEAR::Mail.


                                All of which can be combined with a professional SMTP server/service provider. (Because typical 08/15 shared webhosting plans are hit or miss when it comes to email setup/configurability.)






                                share|improve this answer














                                Although there are portions of this answer that apply to only to the usage of themail() function itself, many of these troubleshooting steps can be applied to any PHP mailing system.



                                There are a variety of reasons your script appears to not be sending emails. It's difficult to diagnose these things unless there is an obvious syntax error. Without one you need to run through the checklist below to find any potential pitfalls you may be encountering.



                                Make sure error reporting is enabled and set to report all errors



                                Error reporting is essential to rooting out bugs in your code and general errors that PHP encounters. Error reporting needs to be enabled to receive these errors. Placing the following code at the top of your PHP files (or in a master configuration file) will enable error reporting.



                                error_reporting(-1);
                                ini_set('display_errors', 'On');
                                set_error_handler("var_dump");


                                See this Stack Overflow answer for more details on this.



                                Make sure the mail() function is called



                                It may seem silly but a common error is to forget to actually place the mail() function in your code. Make sure it is there and not commented out.



                                Make sure the mail() function is called correctly




                                bool mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )




                                The mail function takes three required parameters and optionally a fourth and fifth one. If your call to mail() does not have at least three parameters it will fail.



                                If your call to mail() does not have the correct parameters in the correct order it will also fail.



                                Check the server's mail logs



                                Your web server should be logging all attempts to send emails through it. The location of these logs will vary (you may need to ask your server administrator where they are located) but they can commonly be found in a user's root directory under logs. Inside will be error messages the server reported, if any, related to your attempts to send emails.



                                Check for Port connection failure



                                Port block is a very common problem which most developers face while integrating their code to deliver emails using SMTP. And, this can be easily traced at the server maillogs (the location of server of mail log can vary from server to server, as explained above). In case you are on a shared hosting server, the ports 25 and 587 remain blocked by default. This block is been purposely done by your hosting provider. This is true even for some of the dedicated servers. When these ports are blocked, try to connect using port 2525. If you find that port is also blocked, then the only solution is to contact your hosting provider to unblock these ports.



                                Most of the hosting providers block these email ports to protect their network from sending any spam emails.



                                Use ports 25 or 587 for plain/TLS connections and port 465 for SSL connections. For most users, it is suggested to use port 587 to avoid rate limits set by some hosting providers.



                                Don't use the error suppression operator



                                When the error suppression operator @ is prepended to an expression in PHP, any error messages that might be generated by that expression will be ignored. There are circumstances where using this operator is necessary but sending mail is not one of them.



                                If your code contains @mail(...) then you may be hiding important error messages that will help you debug this. Remove the @ and see if any errors are reported.



                                It's only advisable when you check with error_get_last() right afterwards for concrete failures.



                                Check the mail() return value



                                The mail() function:




                                Returns TRUE if the mail was successfully accepted for delivery, FALSE otherwise. It is important to note that just because the mail was accepted for delivery, it does NOT mean the mail will actually reach the intended destination.




                                This is important to note because:




                                • If you receive a FALSE return value you know the error lies with your server accepting your mail. This probably isn't a coding issue but a server configuration issue. You need to speak to your system administrator to find out why this is happening.

                                • If your receive a TRUE return value it does not mean your email will definitely be sent. It just means the email was sent to its respective handler on the server successfully by PHP. There are still more points of failure outside of PHP's control that can cause the email to not be sent.


                                So FALSE will help point you in the right direction whereas TRUE does not necessarily mean your email was sent successfully. This is important to note!



                                Make sure your hosting provider allows you to send emails and does not limit mail sending



                                Many shared webhosts, especially free webhosting providers, either do not allow emails to be sent from their servers or limit the amount that can be sent during any given time period. This is due to their efforts to limit spammers from taking advantage of their cheaper services.



                                If you think your host has emailing limits or blocks the sending of emails, check their FAQs to see if they list any such limitations. Otherwise, you may need to reach out to their support to verify if there are any restrictions in place around the sending of emails.



                                Check spam folders; prevent emails from being flagged as spam



                                Oftentimes, for various reasons, emails sent through PHP (and other server-side programming languages) end up in a recipient's spam folder. Always check there before troubleshooting your code.



                                To avoid mail sent through PHP from being sent to a recipient's spam folder, there are various things you can do, both in your PHP code and otherwise, to minimize the chances your emails are marked as spam. Good tips from Michiel de Mare include:





                                • Use email authentication methods, such as SPF, and DKIM to prove that your emails and your domain name belong together, and to prevent spoofing of your domain name. The SPF website includes a wizard to generate the DNS information for your site.


                                • Check your reverse DNS to make sure the IP address of your mail server points to the domain name that you use for sending mail.

                                • Make sure that the IP-address that you're using is not on a blacklist

                                • Make sure that the reply-to address is a valid, existing address.

                                • Use the full, real name of the addressee in the To field, not just the email-address (e.g. "John Smith" <john@blacksmiths-international.com> ).

                                • Monitor your abuse accounts, such as abuse@yourdomain.com and postmaster@yourdomain.com. That means - make sure that these accounts exist, read what's sent to them, and act on complaints.

                                • Finally, make it really easy to unsubscribe. Otherwise, your users will unsubscribe by pressing the spam button, and that will affect your reputation.




                                See How do you make sure email you send programmatically is not automatically marked as spam? for more on this topic.



                                Make sure all mail headers are supplied



                                Some spam software will reject mail if it is missing common headers such as "From" and "Reply-to":



                                $headers = array("From: from@example.com",
                                "Reply-To: replyto@example.com",
                                "X-Mailer: PHP/" . PHP_VERSION
                                );
                                $headers = implode("rn", $headers);
                                mail($to, $subject, $message, $headers);


                                Make sure mail headers have no syntax errors



                                Invalid headers are just as bad as having no headers. One incorrect character could be all it takes to derail your email. Double-check to make sure your syntax is correct as PHP will not catch these errors for you.



                                $headers = array("From from@example.com", // missing colon
                                "Reply To: replyto@example.com", // missing hyphen
                                "X-Mailer: "PHP"/" . PHP_VERSION // bad quotes
                                );


                                Make sure the recipient value is correct



                                Sometimes the problem is as simple as having an incorrect value for the recipient of the email. This can be due to using an incorrect variable.



                                $to = 'user@example.com';
                                // other variables ....
                                mail($recipient, $subject, $message, $headers); // $recipient should be $to


                                Another way to test this is to hard code the recipient value into the mail() function call:



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


                                This can apply to all of the mail() parameters.



                                Send to multiple accounts



                                To help rule out email account issues, send your email to multiple email accounts at different email providers. If your emails are not arriving at a user's Gmail account, send the same emails to a Yahoo account, a Hotmail account, and a regular POP3 account (like your ISP-provided email account).



                                If the emails arrive at all or some of the other email accounts, you know your code is sending emails but it is likely that the email account provider is blocking them for some reason. If the email does not arrive at any email account, the problem is more likely to be related to your code.



                                Make sure the code matches the form method



                                If you have set your form method to POST, make sure you are using $_POST to look for your form values. If you have set it to GET or didn't set it at all, make sure you using $_GET to look for your form values.



                                Make sure your form action value points to the correct location



                                Make sure your form action attribute contains a value that points to your PHP mailing code.



                                <form action="send_email.php" method="POST">


                                Make sure the Web host supports sending email



                                Some Web hosting providers do not allow or enable the sending of emails through their servers. The reasons for this may vary but if they have disabled the sending of mail you will need to use an alternative method that uses a third party to send those emails for you.



                                An email to their technical support (after a trip to their online support or FAQ) should clarify if email capabilities are available on your server.



                                Make sure the localhost mail server is configured



                                If you are developing on your local workstation using WAMP, MAMP, or XAMPP, an email server is probably not installed on your workstation. Without one, PHP cannot send mail by default.



                                You can overcome this by installing a basic mail server. For Windows you can use the free Mercury Mail.



                                You can also use SMTP to send your emails. See this great answer from Vikas Dwivedi to learn how to do this.



                                Enable PHP's custom mail.log



                                In addition to your MTA's and PHP's log file, you can enable logging for the mail() function specifically. It doesn't record the complete SMTP interaction, but at least function call parameters and invocation script.



                                ini_set("mail.log", "/tmp/mail.log");
                                ini_set("mail.add_x_header", TRUE);


                                See http://php.net/manual/en/mail.configuration.php for details. (It's best to enable these options in the php.ini or .user.ini or .htaccess perhaps.)



                                Check with a mail testing service



                                There are various delivery and spamminess checking services you can utilize to test your MTA/webserver setup. Typically you send a mail probe To: their address, then get a delivery report and more concrete failures or analyzations later:





                                • mail-tester.com (free/simple)


                                • glockapps.com (free/$$$)


                                • senforensics.com (signup/$$$)


                                • mailtrap.io (pro/$$$)


                                • ultratools/…/emailTest (free/MX checks only)

                                • Various: http://www.verticalresponse.com/blog/7-email-testing-delivery-tools/


                                Use a different mailer



                                PHP's built in mail() function is handy and often gets the job done but it has its shortcomings. Fortunately there are alternatives that offer more power and flexibility including handling a lot of the issues outlined above:




                                • Most popular being: PHPMailer

                                • Likewise featureful: SwiftMailer

                                • Or even the older PEAR::Mail.


                                All of which can be combined with a professional SMTP server/service provider. (Because typical 08/15 shared webhosting plans are hit or miss when it comes to email setup/configurability.)







                                share|improve this answer














                                share|improve this answer



                                share|improve this answer








                                edited Nov 1 at 9:14









                                Synchro

                                17.5k85271




                                17.5k85271










                                answered Jul 9 '14 at 2:21









                                John Conde

                                183k79365415




                                183k79365415








                                • 8




                                  Well written, John. I would just add one more thing: Checking the mail server logs of the sending mail server (whether it is locally or remotely hosted), if possible, can often shed light on what is causing the problem. These logs should show 1) whether or not the message was received from the PHP script by the sending mail server and if so, 2) whether or not the receiving MX accepted delivery from the sending mail server.
                                  – mti2935
                                  Jul 16 '14 at 20:11










                                • @mti2935 That's so helpful I am surprised I missed that. I'll be sure to add it that shortly. :)
                                  – John Conde
                                  Jul 16 '14 at 20:12










                                • i would start with dont use mail(), it does such a poor job and is harder to debug than any other mail library
                                  – user557846
                                  Aug 2 '16 at 21:25






                                • 1




                                  the same website on 000webhost sends emails successfully and fast on netai.net domain but on site88.net i have the same website i get in console that the message sends successfully but i don't receive any messages. Also in comuf.com domain i get error.(The same website on the same host works only on netai.net domain, net16.net works too)
                                  – Nicolas Papaconstantinou
                                  Sep 25 '16 at 7:42








                                • 1




                                  my question (stackoverflow.com/questions/44866673/…) was marked as a duplicate of this one, but I still can't figure it out. Any suggestions?
                                  – Zachary Elkins
                                  Jul 2 '17 at 2:50
















                                • 8




                                  Well written, John. I would just add one more thing: Checking the mail server logs of the sending mail server (whether it is locally or remotely hosted), if possible, can often shed light on what is causing the problem. These logs should show 1) whether or not the message was received from the PHP script by the sending mail server and if so, 2) whether or not the receiving MX accepted delivery from the sending mail server.
                                  – mti2935
                                  Jul 16 '14 at 20:11










                                • @mti2935 That's so helpful I am surprised I missed that. I'll be sure to add it that shortly. :)
                                  – John Conde
                                  Jul 16 '14 at 20:12










                                • i would start with dont use mail(), it does such a poor job and is harder to debug than any other mail library
                                  – user557846
                                  Aug 2 '16 at 21:25






                                • 1




                                  the same website on 000webhost sends emails successfully and fast on netai.net domain but on site88.net i have the same website i get in console that the message sends successfully but i don't receive any messages. Also in comuf.com domain i get error.(The same website on the same host works only on netai.net domain, net16.net works too)
                                  – Nicolas Papaconstantinou
                                  Sep 25 '16 at 7:42








                                • 1




                                  my question (stackoverflow.com/questions/44866673/…) was marked as a duplicate of this one, but I still can't figure it out. Any suggestions?
                                  – Zachary Elkins
                                  Jul 2 '17 at 2:50










                                8




                                8




                                Well written, John. I would just add one more thing: Checking the mail server logs of the sending mail server (whether it is locally or remotely hosted), if possible, can often shed light on what is causing the problem. These logs should show 1) whether or not the message was received from the PHP script by the sending mail server and if so, 2) whether or not the receiving MX accepted delivery from the sending mail server.
                                – mti2935
                                Jul 16 '14 at 20:11




                                Well written, John. I would just add one more thing: Checking the mail server logs of the sending mail server (whether it is locally or remotely hosted), if possible, can often shed light on what is causing the problem. These logs should show 1) whether or not the message was received from the PHP script by the sending mail server and if so, 2) whether or not the receiving MX accepted delivery from the sending mail server.
                                – mti2935
                                Jul 16 '14 at 20:11












                                @mti2935 That's so helpful I am surprised I missed that. I'll be sure to add it that shortly. :)
                                – John Conde
                                Jul 16 '14 at 20:12




                                @mti2935 That's so helpful I am surprised I missed that. I'll be sure to add it that shortly. :)
                                – John Conde
                                Jul 16 '14 at 20:12












                                i would start with dont use mail(), it does such a poor job and is harder to debug than any other mail library
                                – user557846
                                Aug 2 '16 at 21:25




                                i would start with dont use mail(), it does such a poor job and is harder to debug than any other mail library
                                – user557846
                                Aug 2 '16 at 21:25




                                1




                                1




                                the same website on 000webhost sends emails successfully and fast on netai.net domain but on site88.net i have the same website i get in console that the message sends successfully but i don't receive any messages. Also in comuf.com domain i get error.(The same website on the same host works only on netai.net domain, net16.net works too)
                                – Nicolas Papaconstantinou
                                Sep 25 '16 at 7:42






                                the same website on 000webhost sends emails successfully and fast on netai.net domain but on site88.net i have the same website i get in console that the message sends successfully but i don't receive any messages. Also in comuf.com domain i get error.(The same website on the same host works only on netai.net domain, net16.net works too)
                                – Nicolas Papaconstantinou
                                Sep 25 '16 at 7:42






                                1




                                1




                                my question (stackoverflow.com/questions/44866673/…) was marked as a duplicate of this one, but I still can't figure it out. Any suggestions?
                                – Zachary Elkins
                                Jul 2 '17 at 2:50






                                my question (stackoverflow.com/questions/44866673/…) was marked as a duplicate of this one, but I still can't figure it out. Any suggestions?
                                – Zachary Elkins
                                Jul 2 '17 at 2:50














                                up vote
                                32
                                down vote













                                Add mail header in mail function



                                $header = "From: noreply@example.comrn"; 
                                $header.= "MIME-Version: 1.0rn";
                                $header.= "Content-Type: text/html; charset=ISO-8859-1rn";
                                $header.= "X-Priority: 1rn";

                                $status = mail($to, $subject, $message, $header);

                                if($status)
                                {
                                echo '<p>Your mail has been sent!</p>';
                                } else {
                                echo '<p>Something went wrong, Please try again!</p>';
                                }





                                share|improve this answer



























                                  up vote
                                  32
                                  down vote













                                  Add mail header in mail function



                                  $header = "From: noreply@example.comrn"; 
                                  $header.= "MIME-Version: 1.0rn";
                                  $header.= "Content-Type: text/html; charset=ISO-8859-1rn";
                                  $header.= "X-Priority: 1rn";

                                  $status = mail($to, $subject, $message, $header);

                                  if($status)
                                  {
                                  echo '<p>Your mail has been sent!</p>';
                                  } else {
                                  echo '<p>Something went wrong, Please try again!</p>';
                                  }





                                  share|improve this answer

























                                    up vote
                                    32
                                    down vote










                                    up vote
                                    32
                                    down vote









                                    Add mail header in mail function



                                    $header = "From: noreply@example.comrn"; 
                                    $header.= "MIME-Version: 1.0rn";
                                    $header.= "Content-Type: text/html; charset=ISO-8859-1rn";
                                    $header.= "X-Priority: 1rn";

                                    $status = mail($to, $subject, $message, $header);

                                    if($status)
                                    {
                                    echo '<p>Your mail has been sent!</p>';
                                    } else {
                                    echo '<p>Something went wrong, Please try again!</p>';
                                    }





                                    share|improve this answer














                                    Add mail header in mail function



                                    $header = "From: noreply@example.comrn"; 
                                    $header.= "MIME-Version: 1.0rn";
                                    $header.= "Content-Type: text/html; charset=ISO-8859-1rn";
                                    $header.= "X-Priority: 1rn";

                                    $status = mail($to, $subject, $message, $header);

                                    if($status)
                                    {
                                    echo '<p>Your mail has been sent!</p>';
                                    } else {
                                    echo '<p>Something went wrong, Please try again!</p>';
                                    }






                                    share|improve this answer














                                    share|improve this answer



                                    share|improve this answer








                                    edited Sep 29 '16 at 6:49

























                                    answered Jul 27 '15 at 14:01









                                    er.irfankhan11

                                    1,09321425




                                    1,09321425






















                                        up vote
                                        21
                                        down vote














                                        1. Always try sending headers in mail function.

                                        2. If you are sending mail through localhost then do the smtp settings for sending mail.

                                        3. If you are sending mail through server then check the email sending feature is enabled on your server.






                                        share|improve this answer

























                                          up vote
                                          21
                                          down vote














                                          1. Always try sending headers in mail function.

                                          2. If you are sending mail through localhost then do the smtp settings for sending mail.

                                          3. If you are sending mail through server then check the email sending feature is enabled on your server.






                                          share|improve this answer























                                            up vote
                                            21
                                            down vote










                                            up vote
                                            21
                                            down vote










                                            1. Always try sending headers in mail function.

                                            2. If you are sending mail through localhost then do the smtp settings for sending mail.

                                            3. If you are sending mail through server then check the email sending feature is enabled on your server.






                                            share|improve this answer













                                            1. Always try sending headers in mail function.

                                            2. If you are sending mail through localhost then do the smtp settings for sending mail.

                                            3. If you are sending mail through server then check the email sending feature is enabled on your server.







                                            share|improve this answer












                                            share|improve this answer



                                            share|improve this answer










                                            answered May 20 '15 at 13:23









                                            sonam gupta

                                            709416




                                            709416






















                                                up vote
                                                17
                                                down vote













                                                are you using SMTP configuration for sending your email?
                                                try using phpmailer instead. you can download the library from https://github.com/PHPMailer/PHPMailer. i created my email sending this way:



                                                function send_mail($email, $recipient_name, $message='')
                                                {
                                                require("phpmailer/class.phpmailer.php");

                                                $mail = new PHPMailer();

                                                $mail->CharSet="utf-8";
                                                $mail->IsSMTP(); // set mailer to use SMTP
                                                $mail->Host = "mail.example.com"; // specify main and backup server
                                                $mail->SMTPAuth = true; // turn on SMTP authentication
                                                $mail->Username = "myusername"; // SMTP username
                                                $mail->Password = "p@ssw0rd"; // SMTP password

                                                $mail->From = "me@walalang.com";
                                                $mail->FromName = "System-Ad";
                                                $mail->AddAddress($email, $recipient_name);

                                                $mail->WordWrap = 50; // set word wrap to 50 characters
                                                $mail->IsHTML(true); // set email format to HTML (true) or plain text (false)

                                                $mail->Subject = "This is a Sampleenter code here Email";
                                                $mail->Body = $message;
                                                $mail->AltBody = "This is the body in plain text for non-HTML mail clients";
                                                $mail->AddEmbeddedImage('images/logo.png', 'logo', 'logo.png');
                                                $mail->addAttachment('files/file.xlsx');

                                                if(!$mail->Send())
                                                {
                                                echo "Message could not be sent. <p>";
                                                echo "Mailer Error: " . $mail->ErrorInfo;
                                                exit;
                                                }

                                                echo "Message has been sent";
                                                }





                                                share|improve this answer

















                                                • 1




                                                  I also had to use: require("phpmailer/class.smtp.php");
                                                  – Doug
                                                  Oct 10 '15 at 12:29















                                                up vote
                                                17
                                                down vote













                                                are you using SMTP configuration for sending your email?
                                                try using phpmailer instead. you can download the library from https://github.com/PHPMailer/PHPMailer. i created my email sending this way:



                                                function send_mail($email, $recipient_name, $message='')
                                                {
                                                require("phpmailer/class.phpmailer.php");

                                                $mail = new PHPMailer();

                                                $mail->CharSet="utf-8";
                                                $mail->IsSMTP(); // set mailer to use SMTP
                                                $mail->Host = "mail.example.com"; // specify main and backup server
                                                $mail->SMTPAuth = true; // turn on SMTP authentication
                                                $mail->Username = "myusername"; // SMTP username
                                                $mail->Password = "p@ssw0rd"; // SMTP password

                                                $mail->From = "me@walalang.com";
                                                $mail->FromName = "System-Ad";
                                                $mail->AddAddress($email, $recipient_name);

                                                $mail->WordWrap = 50; // set word wrap to 50 characters
                                                $mail->IsHTML(true); // set email format to HTML (true) or plain text (false)

                                                $mail->Subject = "This is a Sampleenter code here Email";
                                                $mail->Body = $message;
                                                $mail->AltBody = "This is the body in plain text for non-HTML mail clients";
                                                $mail->AddEmbeddedImage('images/logo.png', 'logo', 'logo.png');
                                                $mail->addAttachment('files/file.xlsx');

                                                if(!$mail->Send())
                                                {
                                                echo "Message could not be sent. <p>";
                                                echo "Mailer Error: " . $mail->ErrorInfo;
                                                exit;
                                                }

                                                echo "Message has been sent";
                                                }





                                                share|improve this answer

















                                                • 1




                                                  I also had to use: require("phpmailer/class.smtp.php");
                                                  – Doug
                                                  Oct 10 '15 at 12:29













                                                up vote
                                                17
                                                down vote










                                                up vote
                                                17
                                                down vote









                                                are you using SMTP configuration for sending your email?
                                                try using phpmailer instead. you can download the library from https://github.com/PHPMailer/PHPMailer. i created my email sending this way:



                                                function send_mail($email, $recipient_name, $message='')
                                                {
                                                require("phpmailer/class.phpmailer.php");

                                                $mail = new PHPMailer();

                                                $mail->CharSet="utf-8";
                                                $mail->IsSMTP(); // set mailer to use SMTP
                                                $mail->Host = "mail.example.com"; // specify main and backup server
                                                $mail->SMTPAuth = true; // turn on SMTP authentication
                                                $mail->Username = "myusername"; // SMTP username
                                                $mail->Password = "p@ssw0rd"; // SMTP password

                                                $mail->From = "me@walalang.com";
                                                $mail->FromName = "System-Ad";
                                                $mail->AddAddress($email, $recipient_name);

                                                $mail->WordWrap = 50; // set word wrap to 50 characters
                                                $mail->IsHTML(true); // set email format to HTML (true) or plain text (false)

                                                $mail->Subject = "This is a Sampleenter code here Email";
                                                $mail->Body = $message;
                                                $mail->AltBody = "This is the body in plain text for non-HTML mail clients";
                                                $mail->AddEmbeddedImage('images/logo.png', 'logo', 'logo.png');
                                                $mail->addAttachment('files/file.xlsx');

                                                if(!$mail->Send())
                                                {
                                                echo "Message could not be sent. <p>";
                                                echo "Mailer Error: " . $mail->ErrorInfo;
                                                exit;
                                                }

                                                echo "Message has been sent";
                                                }





                                                share|improve this answer












                                                are you using SMTP configuration for sending your email?
                                                try using phpmailer instead. you can download the library from https://github.com/PHPMailer/PHPMailer. i created my email sending this way:



                                                function send_mail($email, $recipient_name, $message='')
                                                {
                                                require("phpmailer/class.phpmailer.php");

                                                $mail = new PHPMailer();

                                                $mail->CharSet="utf-8";
                                                $mail->IsSMTP(); // set mailer to use SMTP
                                                $mail->Host = "mail.example.com"; // specify main and backup server
                                                $mail->SMTPAuth = true; // turn on SMTP authentication
                                                $mail->Username = "myusername"; // SMTP username
                                                $mail->Password = "p@ssw0rd"; // SMTP password

                                                $mail->From = "me@walalang.com";
                                                $mail->FromName = "System-Ad";
                                                $mail->AddAddress($email, $recipient_name);

                                                $mail->WordWrap = 50; // set word wrap to 50 characters
                                                $mail->IsHTML(true); // set email format to HTML (true) or plain text (false)

                                                $mail->Subject = "This is a Sampleenter code here Email";
                                                $mail->Body = $message;
                                                $mail->AltBody = "This is the body in plain text for non-HTML mail clients";
                                                $mail->AddEmbeddedImage('images/logo.png', 'logo', 'logo.png');
                                                $mail->addAttachment('files/file.xlsx');

                                                if(!$mail->Send())
                                                {
                                                echo "Message could not be sent. <p>";
                                                echo "Mailer Error: " . $mail->ErrorInfo;
                                                exit;
                                                }

                                                echo "Message has been sent";
                                                }






                                                share|improve this answer












                                                share|improve this answer



                                                share|improve this answer










                                                answered Jul 9 '14 at 2:55









                                                oneDerer

                                                1887




                                                1887








                                                • 1




                                                  I also had to use: require("phpmailer/class.smtp.php");
                                                  – Doug
                                                  Oct 10 '15 at 12:29














                                                • 1




                                                  I also had to use: require("phpmailer/class.smtp.php");
                                                  – Doug
                                                  Oct 10 '15 at 12:29








                                                1




                                                1




                                                I also had to use: require("phpmailer/class.smtp.php");
                                                – Doug
                                                Oct 10 '15 at 12:29




                                                I also had to use: require("phpmailer/class.smtp.php");
                                                – Doug
                                                Oct 10 '15 at 12:29










                                                up vote
                                                13
                                                down vote













                                                Just add some headers before sending mail:



                                                <?php 
                                                $name = $_POST['name'];
                                                $email = $_POST['email'];
                                                $message = $_POST['message'];
                                                $from = 'From: yoursite.com';
                                                $to = 'contact@yoursite.com';
                                                $subject = 'Customer Inquiry';
                                                $body = "From: $namen E-Mail: $emailn Message:n $message";

                                                $headers .= "MIME-Version: 1.0rn";
                                                $headers .= "Content-type: text/htmlrn";
                                                $headers .= 'From: from@example.com' . "rn" .
                                                'Reply-To: reply@example.com' . "rn" .
                                                'X-Mailer: PHP/' . phpversion();

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


                                                And one more thing. The mail() function is not working in localhost. Upload your code to a server and try.






                                                share|improve this answer























                                                • this actually made the form submit-- my question now would be..how do i show the user's typed in email, now that i've replaced it with the header...
                                                  – user3818620
                                                  Jul 9 '14 at 3:38










                                                • will it prevent mail from spam folder. I have seen if we send mail more than 10 using this method you used in your code gmail send this mail to spam.
                                                  – jewelhuq
                                                  Jan 5 '16 at 12:27










                                                • The OP has this line: $from = 'From: yoursite.com'; Which doesn't look like a complete valid email address, there is no local part. You have replaced the From header and added additional ones. Or rather the $from above is redundant in your example. And being malformed was probably the OPs original problem.
                                                  – Progrock
                                                  Oct 5 '16 at 10:35

















                                                up vote
                                                13
                                                down vote













                                                Just add some headers before sending mail:



                                                <?php 
                                                $name = $_POST['name'];
                                                $email = $_POST['email'];
                                                $message = $_POST['message'];
                                                $from = 'From: yoursite.com';
                                                $to = 'contact@yoursite.com';
                                                $subject = 'Customer Inquiry';
                                                $body = "From: $namen E-Mail: $emailn Message:n $message";

                                                $headers .= "MIME-Version: 1.0rn";
                                                $headers .= "Content-type: text/htmlrn";
                                                $headers .= 'From: from@example.com' . "rn" .
                                                'Reply-To: reply@example.com' . "rn" .
                                                'X-Mailer: PHP/' . phpversion();

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


                                                And one more thing. The mail() function is not working in localhost. Upload your code to a server and try.






                                                share|improve this answer























                                                • this actually made the form submit-- my question now would be..how do i show the user's typed in email, now that i've replaced it with the header...
                                                  – user3818620
                                                  Jul 9 '14 at 3:38










                                                • will it prevent mail from spam folder. I have seen if we send mail more than 10 using this method you used in your code gmail send this mail to spam.
                                                  – jewelhuq
                                                  Jan 5 '16 at 12:27










                                                • The OP has this line: $from = 'From: yoursite.com'; Which doesn't look like a complete valid email address, there is no local part. You have replaced the From header and added additional ones. Or rather the $from above is redundant in your example. And being malformed was probably the OPs original problem.
                                                  – Progrock
                                                  Oct 5 '16 at 10:35















                                                up vote
                                                13
                                                down vote










                                                up vote
                                                13
                                                down vote









                                                Just add some headers before sending mail:



                                                <?php 
                                                $name = $_POST['name'];
                                                $email = $_POST['email'];
                                                $message = $_POST['message'];
                                                $from = 'From: yoursite.com';
                                                $to = 'contact@yoursite.com';
                                                $subject = 'Customer Inquiry';
                                                $body = "From: $namen E-Mail: $emailn Message:n $message";

                                                $headers .= "MIME-Version: 1.0rn";
                                                $headers .= "Content-type: text/htmlrn";
                                                $headers .= 'From: from@example.com' . "rn" .
                                                'Reply-To: reply@example.com' . "rn" .
                                                'X-Mailer: PHP/' . phpversion();

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


                                                And one more thing. The mail() function is not working in localhost. Upload your code to a server and try.






                                                share|improve this answer














                                                Just add some headers before sending mail:



                                                <?php 
                                                $name = $_POST['name'];
                                                $email = $_POST['email'];
                                                $message = $_POST['message'];
                                                $from = 'From: yoursite.com';
                                                $to = 'contact@yoursite.com';
                                                $subject = 'Customer Inquiry';
                                                $body = "From: $namen E-Mail: $emailn Message:n $message";

                                                $headers .= "MIME-Version: 1.0rn";
                                                $headers .= "Content-type: text/htmlrn";
                                                $headers .= 'From: from@example.com' . "rn" .
                                                'Reply-To: reply@example.com' . "rn" .
                                                'X-Mailer: PHP/' . phpversion();

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


                                                And one more thing. The mail() function is not working in localhost. Upload your code to a server and try.







                                                share|improve this answer














                                                share|improve this answer



                                                share|improve this answer








                                                edited Jan 12 '17 at 21:55









                                                Nisse Engström

                                                4,09292034




                                                4,09292034










                                                answered Jul 9 '14 at 3:22









                                                user1258544

                                                158111




                                                158111












                                                • this actually made the form submit-- my question now would be..how do i show the user's typed in email, now that i've replaced it with the header...
                                                  – user3818620
                                                  Jul 9 '14 at 3:38










                                                • will it prevent mail from spam folder. I have seen if we send mail more than 10 using this method you used in your code gmail send this mail to spam.
                                                  – jewelhuq
                                                  Jan 5 '16 at 12:27










                                                • The OP has this line: $from = 'From: yoursite.com'; Which doesn't look like a complete valid email address, there is no local part. You have replaced the From header and added additional ones. Or rather the $from above is redundant in your example. And being malformed was probably the OPs original problem.
                                                  – Progrock
                                                  Oct 5 '16 at 10:35




















                                                • this actually made the form submit-- my question now would be..how do i show the user's typed in email, now that i've replaced it with the header...
                                                  – user3818620
                                                  Jul 9 '14 at 3:38










                                                • will it prevent mail from spam folder. I have seen if we send mail more than 10 using this method you used in your code gmail send this mail to spam.
                                                  – jewelhuq
                                                  Jan 5 '16 at 12:27










                                                • The OP has this line: $from = 'From: yoursite.com'; Which doesn't look like a complete valid email address, there is no local part. You have replaced the From header and added additional ones. Or rather the $from above is redundant in your example. And being malformed was probably the OPs original problem.
                                                  – Progrock
                                                  Oct 5 '16 at 10:35


















                                                this actually made the form submit-- my question now would be..how do i show the user's typed in email, now that i've replaced it with the header...
                                                – user3818620
                                                Jul 9 '14 at 3:38




                                                this actually made the form submit-- my question now would be..how do i show the user's typed in email, now that i've replaced it with the header...
                                                – user3818620
                                                Jul 9 '14 at 3:38












                                                will it prevent mail from spam folder. I have seen if we send mail more than 10 using this method you used in your code gmail send this mail to spam.
                                                – jewelhuq
                                                Jan 5 '16 at 12:27




                                                will it prevent mail from spam folder. I have seen if we send mail more than 10 using this method you used in your code gmail send this mail to spam.
                                                – jewelhuq
                                                Jan 5 '16 at 12:27












                                                The OP has this line: $from = 'From: yoursite.com'; Which doesn't look like a complete valid email address, there is no local part. You have replaced the From header and added additional ones. Or rather the $from above is redundant in your example. And being malformed was probably the OPs original problem.
                                                – Progrock
                                                Oct 5 '16 at 10:35






                                                The OP has this line: $from = 'From: yoursite.com'; Which doesn't look like a complete valid email address, there is no local part. You have replaced the From header and added additional ones. Or rather the $from above is redundant in your example. And being malformed was probably the OPs original problem.
                                                – Progrock
                                                Oct 5 '16 at 10:35












                                                up vote
                                                12
                                                down vote













                                                it worked for me on 000webhost by doing the following :



                                                $headers  = "MIME-Version: 1.0" . "rn";
                                                $headers .= "Content-type: text/html; charset=iso-8859-1" . "rn";
                                                $headers .= "From: ". $from. "rn";
                                                $headers .= "Reply-To: ". $from. "rn";
                                                $headers .= "X-Mailer: PHP/" . phpversion();
                                                $headers .= "X-Priority: 1" . "rn";


                                                Enter directly the email address when sending the email



                                                mail('email@gmail.com', $subject, $message, $headers)


                                                Use '' and not ""



                                                This code works but the email was received with half an hour lag






                                                share|improve this answer



























                                                  up vote
                                                  12
                                                  down vote













                                                  it worked for me on 000webhost by doing the following :



                                                  $headers  = "MIME-Version: 1.0" . "rn";
                                                  $headers .= "Content-type: text/html; charset=iso-8859-1" . "rn";
                                                  $headers .= "From: ". $from. "rn";
                                                  $headers .= "Reply-To: ". $from. "rn";
                                                  $headers .= "X-Mailer: PHP/" . phpversion();
                                                  $headers .= "X-Priority: 1" . "rn";


                                                  Enter directly the email address when sending the email



                                                  mail('email@gmail.com', $subject, $message, $headers)


                                                  Use '' and not ""



                                                  This code works but the email was received with half an hour lag






                                                  share|improve this answer

























                                                    up vote
                                                    12
                                                    down vote










                                                    up vote
                                                    12
                                                    down vote









                                                    it worked for me on 000webhost by doing the following :



                                                    $headers  = "MIME-Version: 1.0" . "rn";
                                                    $headers .= "Content-type: text/html; charset=iso-8859-1" . "rn";
                                                    $headers .= "From: ". $from. "rn";
                                                    $headers .= "Reply-To: ". $from. "rn";
                                                    $headers .= "X-Mailer: PHP/" . phpversion();
                                                    $headers .= "X-Priority: 1" . "rn";


                                                    Enter directly the email address when sending the email



                                                    mail('email@gmail.com', $subject, $message, $headers)


                                                    Use '' and not ""



                                                    This code works but the email was received with half an hour lag






                                                    share|improve this answer














                                                    it worked for me on 000webhost by doing the following :



                                                    $headers  = "MIME-Version: 1.0" . "rn";
                                                    $headers .= "Content-type: text/html; charset=iso-8859-1" . "rn";
                                                    $headers .= "From: ". $from. "rn";
                                                    $headers .= "Reply-To: ". $from. "rn";
                                                    $headers .= "X-Mailer: PHP/" . phpversion();
                                                    $headers .= "X-Priority: 1" . "rn";


                                                    Enter directly the email address when sending the email



                                                    mail('email@gmail.com', $subject, $message, $headers)


                                                    Use '' and not ""



                                                    This code works but the email was received with half an hour lag







                                                    share|improve this answer














                                                    share|improve this answer



                                                    share|improve this answer








                                                    edited Sep 29 '15 at 14:44

























                                                    answered Sep 29 '15 at 14:25









                                                    bat .t

                                                    2011410




                                                    2011410






















                                                        up vote
                                                        9
                                                        down vote













                                                        Try these two thigs separately and together:




                                                        1. remove the if($_POST['submit']){}

                                                        2. remove $from (just my gut)






                                                        share|improve this answer

























                                                          up vote
                                                          9
                                                          down vote













                                                          Try these two thigs separately and together:




                                                          1. remove the if($_POST['submit']){}

                                                          2. remove $from (just my gut)






                                                          share|improve this answer























                                                            up vote
                                                            9
                                                            down vote










                                                            up vote
                                                            9
                                                            down vote









                                                            Try these two thigs separately and together:




                                                            1. remove the if($_POST['submit']){}

                                                            2. remove $from (just my gut)






                                                            share|improve this answer












                                                            Try these two thigs separately and together:




                                                            1. remove the if($_POST['submit']){}

                                                            2. remove $from (just my gut)







                                                            share|improve this answer












                                                            share|improve this answer



                                                            share|improve this answer










                                                            answered Feb 11 '15 at 15:20









                                                            vldfr

                                                            16418




                                                            16418






















                                                                up vote
                                                                9
                                                                down vote













                                                                Mostly the mail() function is disabled in shared hosting.
                                                                A better option is to use SMTP. The best option would be Gmail or SendGrid.





                                                                SMTPconfig.php



                                                                <?php 
                                                                $SmtpServer="smtp.*.*";
                                                                $SmtpPort="2525"; //default
                                                                $SmtpUser="***";
                                                                $SmtpPass="***";
                                                                ?>




                                                                SMTPmail.php



                                                                <?php
                                                                class SMTPClient
                                                                {

                                                                function SMTPClient ($SmtpServer, $SmtpPort, $SmtpUser, $SmtpPass, $from, $to, $subject, $body)
                                                                {

                                                                $this->SmtpServer = $SmtpServer;
                                                                $this->SmtpUser = base64_encode ($SmtpUser);
                                                                $this->SmtpPass = base64_encode ($SmtpPass);
                                                                $this->from = $from;
                                                                $this->to = $to;
                                                                $this->subject = $subject;
                                                                $this->body = $body;

                                                                if ($SmtpPort == "")
                                                                {
                                                                $this->PortSMTP = 25;
                                                                }
                                                                else
                                                                {
                                                                $this->PortSMTP = $SmtpPort;
                                                                }
                                                                }

                                                                function SendMail ()
                                                                {
                                                                $newLine = "rn";
                                                                $headers = "MIME-Version: 1.0" . $newLine;
                                                                $headers .= "Content-type: text/html; charset=iso-8859-1" . $newLine;

                                                                if ($SMTPIN = fsockopen ($this->SmtpServer, $this->PortSMTP))
                                                                {
                                                                fputs ($SMTPIN, "EHLO ".$HTTP_HOST."rn");
                                                                $talk["hello"] = fgets ( $SMTPIN, 1024 );
                                                                fputs($SMTPIN, "auth loginrn");
                                                                $talk["res"]=fgets($SMTPIN,1024);
                                                                fputs($SMTPIN, $this->SmtpUser."rn");
                                                                $talk["user"]=fgets($SMTPIN,1024);
                                                                fputs($SMTPIN, $this->SmtpPass."rn");
                                                                $talk["pass"]=fgets($SMTPIN,256);
                                                                fputs ($SMTPIN, "MAIL FROM: <".$this->from.">rn");
                                                                $talk["From"] = fgets ( $SMTPIN, 1024 );
                                                                fputs ($SMTPIN, "RCPT TO: <".$this->to.">rn");
                                                                $talk["To"] = fgets ($SMTPIN, 1024);
                                                                fputs($SMTPIN, "DATArn");
                                                                $talk["data"]=fgets( $SMTPIN,1024 );
                                                                fputs($SMTPIN, "To: <".$this->to.">rnFrom: <".$this->from.">rn".$headers."nnSubject:".$this->subject."rnrnrn".$this->body."rn.rn");
                                                                $talk["send"]=fgets($SMTPIN,256);
                                                                //CLOSE CONNECTION AND EXIT ...
                                                                fputs ($SMTPIN, "QUITrn");
                                                                fclose($SMTPIN);
                                                                //
                                                                }
                                                                return $talk;
                                                                }
                                                                }
                                                                ?>




                                                                contact_email.php



                                                                <?php 
                                                                include('SMTPconfig.php');
                                                                include('SMTPmail.php');
                                                                if($_SERVER["REQUEST_METHOD"] == "POST")
                                                                {
                                                                $to = "";
                                                                $from = $_POST['email'];
                                                                $subject = "Enquiry";
                                                                $body = $_POST['name'].'</br>'.$_POST['companyName'].'</br>'.$_POST['tel'].'</br>'.'<hr />'.$_POST['message'];
                                                                $SMTPMail = new SMTPClient ($SmtpServer, $SmtpPort, $SmtpUser, $SmtpPass, $from, $to, $subject, $body);
                                                                $SMTPChat = $SMTPMail->SendMail();
                                                                }
                                                                ?>





                                                                share|improve this answer























                                                                • Is it a good idea to save the smtp user and password in plaintext on the server?
                                                                  – Juri
                                                                  Nov 27 '16 at 10:36










                                                                • @Juri probably not... god i can't figure out how to get the mail function to work :.(
                                                                  – user2230470
                                                                  Dec 20 '16 at 5:38















                                                                up vote
                                                                9
                                                                down vote













                                                                Mostly the mail() function is disabled in shared hosting.
                                                                A better option is to use SMTP. The best option would be Gmail or SendGrid.





                                                                SMTPconfig.php



                                                                <?php 
                                                                $SmtpServer="smtp.*.*";
                                                                $SmtpPort="2525"; //default
                                                                $SmtpUser="***";
                                                                $SmtpPass="***";
                                                                ?>




                                                                SMTPmail.php



                                                                <?php
                                                                class SMTPClient
                                                                {

                                                                function SMTPClient ($SmtpServer, $SmtpPort, $SmtpUser, $SmtpPass, $from, $to, $subject, $body)
                                                                {

                                                                $this->SmtpServer = $SmtpServer;
                                                                $this->SmtpUser = base64_encode ($SmtpUser);
                                                                $this->SmtpPass = base64_encode ($SmtpPass);
                                                                $this->from = $from;
                                                                $this->to = $to;
                                                                $this->subject = $subject;
                                                                $this->body = $body;

                                                                if ($SmtpPort == "")
                                                                {
                                                                $this->PortSMTP = 25;
                                                                }
                                                                else
                                                                {
                                                                $this->PortSMTP = $SmtpPort;
                                                                }
                                                                }

                                                                function SendMail ()
                                                                {
                                                                $newLine = "rn";
                                                                $headers = "MIME-Version: 1.0" . $newLine;
                                                                $headers .= "Content-type: text/html; charset=iso-8859-1" . $newLine;

                                                                if ($SMTPIN = fsockopen ($this->SmtpServer, $this->PortSMTP))
                                                                {
                                                                fputs ($SMTPIN, "EHLO ".$HTTP_HOST."rn");
                                                                $talk["hello"] = fgets ( $SMTPIN, 1024 );
                                                                fputs($SMTPIN, "auth loginrn");
                                                                $talk["res"]=fgets($SMTPIN,1024);
                                                                fputs($SMTPIN, $this->SmtpUser."rn");
                                                                $talk["user"]=fgets($SMTPIN,1024);
                                                                fputs($SMTPIN, $this->SmtpPass."rn");
                                                                $talk["pass"]=fgets($SMTPIN,256);
                                                                fputs ($SMTPIN, "MAIL FROM: <".$this->from.">rn");
                                                                $talk["From"] = fgets ( $SMTPIN, 1024 );
                                                                fputs ($SMTPIN, "RCPT TO: <".$this->to.">rn");
                                                                $talk["To"] = fgets ($SMTPIN, 1024);
                                                                fputs($SMTPIN, "DATArn");
                                                                $talk["data"]=fgets( $SMTPIN,1024 );
                                                                fputs($SMTPIN, "To: <".$this->to.">rnFrom: <".$this->from.">rn".$headers."nnSubject:".$this->subject."rnrnrn".$this->body."rn.rn");
                                                                $talk["send"]=fgets($SMTPIN,256);
                                                                //CLOSE CONNECTION AND EXIT ...
                                                                fputs ($SMTPIN, "QUITrn");
                                                                fclose($SMTPIN);
                                                                //
                                                                }
                                                                return $talk;
                                                                }
                                                                }
                                                                ?>




                                                                contact_email.php



                                                                <?php 
                                                                include('SMTPconfig.php');
                                                                include('SMTPmail.php');
                                                                if($_SERVER["REQUEST_METHOD"] == "POST")
                                                                {
                                                                $to = "";
                                                                $from = $_POST['email'];
                                                                $subject = "Enquiry";
                                                                $body = $_POST['name'].'</br>'.$_POST['companyName'].'</br>'.$_POST['tel'].'</br>'.'<hr />'.$_POST['message'];
                                                                $SMTPMail = new SMTPClient ($SmtpServer, $SmtpPort, $SmtpUser, $SmtpPass, $from, $to, $subject, $body);
                                                                $SMTPChat = $SMTPMail->SendMail();
                                                                }
                                                                ?>





                                                                share|improve this answer























                                                                • Is it a good idea to save the smtp user and password in plaintext on the server?
                                                                  – Juri
                                                                  Nov 27 '16 at 10:36










                                                                • @Juri probably not... god i can't figure out how to get the mail function to work :.(
                                                                  – user2230470
                                                                  Dec 20 '16 at 5:38













                                                                up vote
                                                                9
                                                                down vote










                                                                up vote
                                                                9
                                                                down vote









                                                                Mostly the mail() function is disabled in shared hosting.
                                                                A better option is to use SMTP. The best option would be Gmail or SendGrid.





                                                                SMTPconfig.php



                                                                <?php 
                                                                $SmtpServer="smtp.*.*";
                                                                $SmtpPort="2525"; //default
                                                                $SmtpUser="***";
                                                                $SmtpPass="***";
                                                                ?>




                                                                SMTPmail.php



                                                                <?php
                                                                class SMTPClient
                                                                {

                                                                function SMTPClient ($SmtpServer, $SmtpPort, $SmtpUser, $SmtpPass, $from, $to, $subject, $body)
                                                                {

                                                                $this->SmtpServer = $SmtpServer;
                                                                $this->SmtpUser = base64_encode ($SmtpUser);
                                                                $this->SmtpPass = base64_encode ($SmtpPass);
                                                                $this->from = $from;
                                                                $this->to = $to;
                                                                $this->subject = $subject;
                                                                $this->body = $body;

                                                                if ($SmtpPort == "")
                                                                {
                                                                $this->PortSMTP = 25;
                                                                }
                                                                else
                                                                {
                                                                $this->PortSMTP = $SmtpPort;
                                                                }
                                                                }

                                                                function SendMail ()
                                                                {
                                                                $newLine = "rn";
                                                                $headers = "MIME-Version: 1.0" . $newLine;
                                                                $headers .= "Content-type: text/html; charset=iso-8859-1" . $newLine;

                                                                if ($SMTPIN = fsockopen ($this->SmtpServer, $this->PortSMTP))
                                                                {
                                                                fputs ($SMTPIN, "EHLO ".$HTTP_HOST."rn");
                                                                $talk["hello"] = fgets ( $SMTPIN, 1024 );
                                                                fputs($SMTPIN, "auth loginrn");
                                                                $talk["res"]=fgets($SMTPIN,1024);
                                                                fputs($SMTPIN, $this->SmtpUser."rn");
                                                                $talk["user"]=fgets($SMTPIN,1024);
                                                                fputs($SMTPIN, $this->SmtpPass."rn");
                                                                $talk["pass"]=fgets($SMTPIN,256);
                                                                fputs ($SMTPIN, "MAIL FROM: <".$this->from.">rn");
                                                                $talk["From"] = fgets ( $SMTPIN, 1024 );
                                                                fputs ($SMTPIN, "RCPT TO: <".$this->to.">rn");
                                                                $talk["To"] = fgets ($SMTPIN, 1024);
                                                                fputs($SMTPIN, "DATArn");
                                                                $talk["data"]=fgets( $SMTPIN,1024 );
                                                                fputs($SMTPIN, "To: <".$this->to.">rnFrom: <".$this->from.">rn".$headers."nnSubject:".$this->subject."rnrnrn".$this->body."rn.rn");
                                                                $talk["send"]=fgets($SMTPIN,256);
                                                                //CLOSE CONNECTION AND EXIT ...
                                                                fputs ($SMTPIN, "QUITrn");
                                                                fclose($SMTPIN);
                                                                //
                                                                }
                                                                return $talk;
                                                                }
                                                                }
                                                                ?>




                                                                contact_email.php



                                                                <?php 
                                                                include('SMTPconfig.php');
                                                                include('SMTPmail.php');
                                                                if($_SERVER["REQUEST_METHOD"] == "POST")
                                                                {
                                                                $to = "";
                                                                $from = $_POST['email'];
                                                                $subject = "Enquiry";
                                                                $body = $_POST['name'].'</br>'.$_POST['companyName'].'</br>'.$_POST['tel'].'</br>'.'<hr />'.$_POST['message'];
                                                                $SMTPMail = new SMTPClient ($SmtpServer, $SmtpPort, $SmtpUser, $SmtpPass, $from, $to, $subject, $body);
                                                                $SMTPChat = $SMTPMail->SendMail();
                                                                }
                                                                ?>





                                                                share|improve this answer














                                                                Mostly the mail() function is disabled in shared hosting.
                                                                A better option is to use SMTP. The best option would be Gmail or SendGrid.





                                                                SMTPconfig.php



                                                                <?php 
                                                                $SmtpServer="smtp.*.*";
                                                                $SmtpPort="2525"; //default
                                                                $SmtpUser="***";
                                                                $SmtpPass="***";
                                                                ?>




                                                                SMTPmail.php



                                                                <?php
                                                                class SMTPClient
                                                                {

                                                                function SMTPClient ($SmtpServer, $SmtpPort, $SmtpUser, $SmtpPass, $from, $to, $subject, $body)
                                                                {

                                                                $this->SmtpServer = $SmtpServer;
                                                                $this->SmtpUser = base64_encode ($SmtpUser);
                                                                $this->SmtpPass = base64_encode ($SmtpPass);
                                                                $this->from = $from;
                                                                $this->to = $to;
                                                                $this->subject = $subject;
                                                                $this->body = $body;

                                                                if ($SmtpPort == "")
                                                                {
                                                                $this->PortSMTP = 25;
                                                                }
                                                                else
                                                                {
                                                                $this->PortSMTP = $SmtpPort;
                                                                }
                                                                }

                                                                function SendMail ()
                                                                {
                                                                $newLine = "rn";
                                                                $headers = "MIME-Version: 1.0" . $newLine;
                                                                $headers .= "Content-type: text/html; charset=iso-8859-1" . $newLine;

                                                                if ($SMTPIN = fsockopen ($this->SmtpServer, $this->PortSMTP))
                                                                {
                                                                fputs ($SMTPIN, "EHLO ".$HTTP_HOST."rn");
                                                                $talk["hello"] = fgets ( $SMTPIN, 1024 );
                                                                fputs($SMTPIN, "auth loginrn");
                                                                $talk["res"]=fgets($SMTPIN,1024);
                                                                fputs($SMTPIN, $this->SmtpUser."rn");
                                                                $talk["user"]=fgets($SMTPIN,1024);
                                                                fputs($SMTPIN, $this->SmtpPass."rn");
                                                                $talk["pass"]=fgets($SMTPIN,256);
                                                                fputs ($SMTPIN, "MAIL FROM: <".$this->from.">rn");
                                                                $talk["From"] = fgets ( $SMTPIN, 1024 );
                                                                fputs ($SMTPIN, "RCPT TO: <".$this->to.">rn");
                                                                $talk["To"] = fgets ($SMTPIN, 1024);
                                                                fputs($SMTPIN, "DATArn");
                                                                $talk["data"]=fgets( $SMTPIN,1024 );
                                                                fputs($SMTPIN, "To: <".$this->to.">rnFrom: <".$this->from.">rn".$headers."nnSubject:".$this->subject."rnrnrn".$this->body."rn.rn");
                                                                $talk["send"]=fgets($SMTPIN,256);
                                                                //CLOSE CONNECTION AND EXIT ...
                                                                fputs ($SMTPIN, "QUITrn");
                                                                fclose($SMTPIN);
                                                                //
                                                                }
                                                                return $talk;
                                                                }
                                                                }
                                                                ?>




                                                                contact_email.php



                                                                <?php 
                                                                include('SMTPconfig.php');
                                                                include('SMTPmail.php');
                                                                if($_SERVER["REQUEST_METHOD"] == "POST")
                                                                {
                                                                $to = "";
                                                                $from = $_POST['email'];
                                                                $subject = "Enquiry";
                                                                $body = $_POST['name'].'</br>'.$_POST['companyName'].'</br>'.$_POST['tel'].'</br>'.'<hr />'.$_POST['message'];
                                                                $SMTPMail = new SMTPClient ($SmtpServer, $SmtpPort, $SmtpUser, $SmtpPass, $from, $to, $subject, $body);
                                                                $SMTPChat = $SMTPMail->SendMail();
                                                                }
                                                                ?>






                                                                share|improve this answer














                                                                share|improve this answer



                                                                share|improve this answer








                                                                edited Jan 13 '17 at 4:37

























                                                                answered Jun 23 '16 at 9:02









                                                                Karthik

                                                                132215




                                                                132215












                                                                • Is it a good idea to save the smtp user and password in plaintext on the server?
                                                                  – Juri
                                                                  Nov 27 '16 at 10:36










                                                                • @Juri probably not... god i can't figure out how to get the mail function to work :.(
                                                                  – user2230470
                                                                  Dec 20 '16 at 5:38


















                                                                • Is it a good idea to save the smtp user and password in plaintext on the server?
                                                                  – Juri
                                                                  Nov 27 '16 at 10:36










                                                                • @Juri probably not... god i can't figure out how to get the mail function to work :.(
                                                                  – user2230470
                                                                  Dec 20 '16 at 5:38
















                                                                Is it a good idea to save the smtp user and password in plaintext on the server?
                                                                – Juri
                                                                Nov 27 '16 at 10:36




                                                                Is it a good idea to save the smtp user and password in plaintext on the server?
                                                                – Juri
                                                                Nov 27 '16 at 10:36












                                                                @Juri probably not... god i can't figure out how to get the mail function to work :.(
                                                                – user2230470
                                                                Dec 20 '16 at 5:38




                                                                @Juri probably not... god i can't figure out how to get the mail function to work :.(
                                                                – user2230470
                                                                Dec 20 '16 at 5:38










                                                                up vote
                                                                8
                                                                down vote













                                                                If you only use the mail()function, you need to complete the config file.



                                                                You need to open the mail expansion, and set the SMTP smtp_port and so on, and most important, your username and your password. Without that, mail cannot be sent. Also, you can use PHPMail class to send.






                                                                share|improve this answer



























                                                                  up vote
                                                                  8
                                                                  down vote













                                                                  If you only use the mail()function, you need to complete the config file.



                                                                  You need to open the mail expansion, and set the SMTP smtp_port and so on, and most important, your username and your password. Without that, mail cannot be sent. Also, you can use PHPMail class to send.






                                                                  share|improve this answer

























                                                                    up vote
                                                                    8
                                                                    down vote










                                                                    up vote
                                                                    8
                                                                    down vote









                                                                    If you only use the mail()function, you need to complete the config file.



                                                                    You need to open the mail expansion, and set the SMTP smtp_port and so on, and most important, your username and your password. Without that, mail cannot be sent. Also, you can use PHPMail class to send.






                                                                    share|improve this answer














                                                                    If you only use the mail()function, you need to complete the config file.



                                                                    You need to open the mail expansion, and set the SMTP smtp_port and so on, and most important, your username and your password. Without that, mail cannot be sent. Also, you can use PHPMail class to send.







                                                                    share|improve this answer














                                                                    share|improve this answer



                                                                    share|improve this answer








                                                                    edited Sep 8 '14 at 7:46









                                                                    Peter O.

                                                                    20.6k95869




                                                                    20.6k95869










                                                                    answered Aug 5 '14 at 3:49









                                                                    iatboy

                                                                    7951815




                                                                    7951815






















                                                                        up vote
                                                                        7
                                                                        down vote













                                                                        You can use config email by codeigniter, example using smtp (simple way) :



                                                                        $config = Array(
                                                                        'protocol' => 'smtp',
                                                                        'smtp_host' => 'mail.domain.com', //your smtp host
                                                                        'smtp_port' => 26, //default port smtp
                                                                        'smtp_user' => 'name@domain.com',
                                                                        'smtp_pass' => 'password',
                                                                        'mailtype' => 'html',
                                                                        'charset' => 'iso-8859-1',
                                                                        'wordwrap' => TRUE
                                                                        );
                                                                        $message = 'Your msg';
                                                                        $this->load->library('email', $config);
                                                                        $this->email->from('name@domain.com', 'Title');
                                                                        $this->email->to('emaildestination@domain.com');
                                                                        $this->email->subject('Header');
                                                                        $this->email->message($message);

                                                                        if($this->email->send())
                                                                        {
                                                                        //conditional true
                                                                        }


                                                                        It's works for me!






                                                                        share|improve this answer





















                                                                        • fsockopen() [function.fsockopen]: php_network_getaddresses: getaddrinfo failed: No such host is known. - If i encounter this error, then what is the reason and what needs to be done?
                                                                          – Choudhury Saadmaan Mahmid
                                                                          Aug 24 '15 at 4:18















                                                                        up vote
                                                                        7
                                                                        down vote













                                                                        You can use config email by codeigniter, example using smtp (simple way) :



                                                                        $config = Array(
                                                                        'protocol' => 'smtp',
                                                                        'smtp_host' => 'mail.domain.com', //your smtp host
                                                                        'smtp_port' => 26, //default port smtp
                                                                        'smtp_user' => 'name@domain.com',
                                                                        'smtp_pass' => 'password',
                                                                        'mailtype' => 'html',
                                                                        'charset' => 'iso-8859-1',
                                                                        'wordwrap' => TRUE
                                                                        );
                                                                        $message = 'Your msg';
                                                                        $this->load->library('email', $config);
                                                                        $this->email->from('name@domain.com', 'Title');
                                                                        $this->email->to('emaildestination@domain.com');
                                                                        $this->email->subject('Header');
                                                                        $this->email->message($message);

                                                                        if($this->email->send())
                                                                        {
                                                                        //conditional true
                                                                        }


                                                                        It's works for me!






                                                                        share|improve this answer





















                                                                        • fsockopen() [function.fsockopen]: php_network_getaddresses: getaddrinfo failed: No such host is known. - If i encounter this error, then what is the reason and what needs to be done?
                                                                          – Choudhury Saadmaan Mahmid
                                                                          Aug 24 '15 at 4:18













                                                                        up vote
                                                                        7
                                                                        down vote










                                                                        up vote
                                                                        7
                                                                        down vote









                                                                        You can use config email by codeigniter, example using smtp (simple way) :



                                                                        $config = Array(
                                                                        'protocol' => 'smtp',
                                                                        'smtp_host' => 'mail.domain.com', //your smtp host
                                                                        'smtp_port' => 26, //default port smtp
                                                                        'smtp_user' => 'name@domain.com',
                                                                        'smtp_pass' => 'password',
                                                                        'mailtype' => 'html',
                                                                        'charset' => 'iso-8859-1',
                                                                        'wordwrap' => TRUE
                                                                        );
                                                                        $message = 'Your msg';
                                                                        $this->load->library('email', $config);
                                                                        $this->email->from('name@domain.com', 'Title');
                                                                        $this->email->to('emaildestination@domain.com');
                                                                        $this->email->subject('Header');
                                                                        $this->email->message($message);

                                                                        if($this->email->send())
                                                                        {
                                                                        //conditional true
                                                                        }


                                                                        It's works for me!






                                                                        share|improve this answer












                                                                        You can use config email by codeigniter, example using smtp (simple way) :



                                                                        $config = Array(
                                                                        'protocol' => 'smtp',
                                                                        'smtp_host' => 'mail.domain.com', //your smtp host
                                                                        'smtp_port' => 26, //default port smtp
                                                                        'smtp_user' => 'name@domain.com',
                                                                        'smtp_pass' => 'password',
                                                                        'mailtype' => 'html',
                                                                        'charset' => 'iso-8859-1',
                                                                        'wordwrap' => TRUE
                                                                        );
                                                                        $message = 'Your msg';
                                                                        $this->load->library('email', $config);
                                                                        $this->email->from('name@domain.com', 'Title');
                                                                        $this->email->to('emaildestination@domain.com');
                                                                        $this->email->subject('Header');
                                                                        $this->email->message($message);

                                                                        if($this->email->send())
                                                                        {
                                                                        //conditional true
                                                                        }


                                                                        It's works for me!







                                                                        share|improve this answer












                                                                        share|improve this answer



                                                                        share|improve this answer










                                                                        answered Oct 6 '14 at 22:33









                                                                        MD Creative Indonesia

                                                                        8514




                                                                        8514












                                                                        • fsockopen() [function.fsockopen]: php_network_getaddresses: getaddrinfo failed: No such host is known. - If i encounter this error, then what is the reason and what needs to be done?
                                                                          – Choudhury Saadmaan Mahmid
                                                                          Aug 24 '15 at 4:18


















                                                                        • fsockopen() [function.fsockopen]: php_network_getaddresses: getaddrinfo failed: No such host is known. - If i encounter this error, then what is the reason and what needs to be done?
                                                                          – Choudhury Saadmaan Mahmid
                                                                          Aug 24 '15 at 4:18
















                                                                        fsockopen() [function.fsockopen]: php_network_getaddresses: getaddrinfo failed: No such host is known. - If i encounter this error, then what is the reason and what needs to be done?
                                                                        – Choudhury Saadmaan Mahmid
                                                                        Aug 24 '15 at 4:18




                                                                        fsockopen() [function.fsockopen]: php_network_getaddresses: getaddrinfo failed: No such host is known. - If i encounter this error, then what is the reason and what needs to be done?
                                                                        – Choudhury Saadmaan Mahmid
                                                                        Aug 24 '15 at 4:18










                                                                        up vote
                                                                        7
                                                                        down vote













                                                                        For anyone who finds this going forward, I would not recommend using mail. There's some answers that touch on this, but not the why of it.



                                                                        PHP's mail function is not only opaque, it fully relies on whatever MTA you use (i.e. Sendmail) to do the work. mail will ONLY tell you if the MTA failed to accept it (i.e. Sendmail was down when you tried to send). It cannot tell you if the mail was successful because it's handed it off. As such (as John Conde's answer details), you now get to fiddle with the logs of the MTA and hope that it tells you enough about the failure to fix it. If you're on a shared host or don't have access to the MTA logs, you're out of luck. Sadly, the default for most vanilla installs for Linux handle it this way.



                                                                        A mail library (PHPMailer, Zend Framework 2+, etc), does something very different from mail. What they do is they open a socket directly to the receiving mail server and then send the SMTP mail commands directly over that socket. In other words, the class acts as its own MTA (note that you can tell the libraries to use mail to ultimately send the mail, but I would strongly recommend you not do that).



                                                                        What this means for you is that you can then directly see the responses from the receiving server (in PHPMailer, for instance, you can turn on debugging output). No more guessing if a mail failed to send or why.




                                                                        If you're using SMTP (i.e. you're calling isSMTP()), you can get a detailed transcript of the SMTP conversation using the SMTPDebug property.



                                                                        Set this option by including a line like this in your script:



                                                                        $mail->SMTPDebug = 2;



                                                                        You also get the benefit of a better interface. With mail you have to set up all your headers, attachments, etc. With a library, you have a dedicated function to do that. It also means the function is doing all the tricky parts (like headers).






                                                                        share|improve this answer

























                                                                          up vote
                                                                          7
                                                                          down vote













                                                                          For anyone who finds this going forward, I would not recommend using mail. There's some answers that touch on this, but not the why of it.



                                                                          PHP's mail function is not only opaque, it fully relies on whatever MTA you use (i.e. Sendmail) to do the work. mail will ONLY tell you if the MTA failed to accept it (i.e. Sendmail was down when you tried to send). It cannot tell you if the mail was successful because it's handed it off. As such (as John Conde's answer details), you now get to fiddle with the logs of the MTA and hope that it tells you enough about the failure to fix it. If you're on a shared host or don't have access to the MTA logs, you're out of luck. Sadly, the default for most vanilla installs for Linux handle it this way.



                                                                          A mail library (PHPMailer, Zend Framework 2+, etc), does something very different from mail. What they do is they open a socket directly to the receiving mail server and then send the SMTP mail commands directly over that socket. In other words, the class acts as its own MTA (note that you can tell the libraries to use mail to ultimately send the mail, but I would strongly recommend you not do that).



                                                                          What this means for you is that you can then directly see the responses from the receiving server (in PHPMailer, for instance, you can turn on debugging output). No more guessing if a mail failed to send or why.




                                                                          If you're using SMTP (i.e. you're calling isSMTP()), you can get a detailed transcript of the SMTP conversation using the SMTPDebug property.



                                                                          Set this option by including a line like this in your script:



                                                                          $mail->SMTPDebug = 2;



                                                                          You also get the benefit of a better interface. With mail you have to set up all your headers, attachments, etc. With a library, you have a dedicated function to do that. It also means the function is doing all the tricky parts (like headers).






                                                                          share|improve this answer























                                                                            up vote
                                                                            7
                                                                            down vote










                                                                            up vote
                                                                            7
                                                                            down vote









                                                                            For anyone who finds this going forward, I would not recommend using mail. There's some answers that touch on this, but not the why of it.



                                                                            PHP's mail function is not only opaque, it fully relies on whatever MTA you use (i.e. Sendmail) to do the work. mail will ONLY tell you if the MTA failed to accept it (i.e. Sendmail was down when you tried to send). It cannot tell you if the mail was successful because it's handed it off. As such (as John Conde's answer details), you now get to fiddle with the logs of the MTA and hope that it tells you enough about the failure to fix it. If you're on a shared host or don't have access to the MTA logs, you're out of luck. Sadly, the default for most vanilla installs for Linux handle it this way.



                                                                            A mail library (PHPMailer, Zend Framework 2+, etc), does something very different from mail. What they do is they open a socket directly to the receiving mail server and then send the SMTP mail commands directly over that socket. In other words, the class acts as its own MTA (note that you can tell the libraries to use mail to ultimately send the mail, but I would strongly recommend you not do that).



                                                                            What this means for you is that you can then directly see the responses from the receiving server (in PHPMailer, for instance, you can turn on debugging output). No more guessing if a mail failed to send or why.




                                                                            If you're using SMTP (i.e. you're calling isSMTP()), you can get a detailed transcript of the SMTP conversation using the SMTPDebug property.



                                                                            Set this option by including a line like this in your script:



                                                                            $mail->SMTPDebug = 2;



                                                                            You also get the benefit of a better interface. With mail you have to set up all your headers, attachments, etc. With a library, you have a dedicated function to do that. It also means the function is doing all the tricky parts (like headers).






                                                                            share|improve this answer












                                                                            For anyone who finds this going forward, I would not recommend using mail. There's some answers that touch on this, but not the why of it.



                                                                            PHP's mail function is not only opaque, it fully relies on whatever MTA you use (i.e. Sendmail) to do the work. mail will ONLY tell you if the MTA failed to accept it (i.e. Sendmail was down when you tried to send). It cannot tell you if the mail was successful because it's handed it off. As such (as John Conde's answer details), you now get to fiddle with the logs of the MTA and hope that it tells you enough about the failure to fix it. If you're on a shared host or don't have access to the MTA logs, you're out of luck. Sadly, the default for most vanilla installs for Linux handle it this way.



                                                                            A mail library (PHPMailer, Zend Framework 2+, etc), does something very different from mail. What they do is they open a socket directly to the receiving mail server and then send the SMTP mail commands directly over that socket. In other words, the class acts as its own MTA (note that you can tell the libraries to use mail to ultimately send the mail, but I would strongly recommend you not do that).



                                                                            What this means for you is that you can then directly see the responses from the receiving server (in PHPMailer, for instance, you can turn on debugging output). No more guessing if a mail failed to send or why.




                                                                            If you're using SMTP (i.e. you're calling isSMTP()), you can get a detailed transcript of the SMTP conversation using the SMTPDebug property.



                                                                            Set this option by including a line like this in your script:



                                                                            $mail->SMTPDebug = 2;



                                                                            You also get the benefit of a better interface. With mail you have to set up all your headers, attachments, etc. With a library, you have a dedicated function to do that. It also means the function is doing all the tricky parts (like headers).







                                                                            share|improve this answer












                                                                            share|improve this answer



                                                                            share|improve this answer










                                                                            answered May 20 '16 at 12:45









                                                                            Machavity

                                                                            24.1k135478




                                                                            24.1k135478






















                                                                                up vote
                                                                                7
                                                                                down vote













                                                                                I think this should do the trick. I just added an if(isset and added concatenation to the variables in the body to separate PHP from HTML.



                                                                                <?php
                                                                                $name = $_POST['name'];
                                                                                $email = $_POST['email'];
                                                                                $message = $_POST['message'];
                                                                                $from = 'From: yoursite.com';
                                                                                $to = 'contact@yoursite.com';
                                                                                $subject = 'Customer Inquiry';
                                                                                $body = "From:" .$name."rn E-Mail:" .$email."rn Message:rn" .$message;

                                                                                if (isset($_POST['submit']))
                                                                                {
                                                                                if (mail ($to, $subject, $body, $from))
                                                                                {
                                                                                echo '<p>Your message has been sent!</p>';
                                                                                }
                                                                                else
                                                                                {
                                                                                echo '<p>Something went wrong, go back and try again!</p>';
                                                                                }
                                                                                }

                                                                                ?>





                                                                                share|improve this answer



























                                                                                  up vote
                                                                                  7
                                                                                  down vote













                                                                                  I think this should do the trick. I just added an if(isset and added concatenation to the variables in the body to separate PHP from HTML.



                                                                                  <?php
                                                                                  $name = $_POST['name'];
                                                                                  $email = $_POST['email'];
                                                                                  $message = $_POST['message'];
                                                                                  $from = 'From: yoursite.com';
                                                                                  $to = 'contact@yoursite.com';
                                                                                  $subject = 'Customer Inquiry';
                                                                                  $body = "From:" .$name."rn E-Mail:" .$email."rn Message:rn" .$message;

                                                                                  if (isset($_POST['submit']))
                                                                                  {
                                                                                  if (mail ($to, $subject, $body, $from))
                                                                                  {
                                                                                  echo '<p>Your message has been sent!</p>';
                                                                                  }
                                                                                  else
                                                                                  {
                                                                                  echo '<p>Something went wrong, go back and try again!</p>';
                                                                                  }
                                                                                  }

                                                                                  ?>





                                                                                  share|improve this answer

























                                                                                    up vote
                                                                                    7
                                                                                    down vote










                                                                                    up vote
                                                                                    7
                                                                                    down vote









                                                                                    I think this should do the trick. I just added an if(isset and added concatenation to the variables in the body to separate PHP from HTML.



                                                                                    <?php
                                                                                    $name = $_POST['name'];
                                                                                    $email = $_POST['email'];
                                                                                    $message = $_POST['message'];
                                                                                    $from = 'From: yoursite.com';
                                                                                    $to = 'contact@yoursite.com';
                                                                                    $subject = 'Customer Inquiry';
                                                                                    $body = "From:" .$name."rn E-Mail:" .$email."rn Message:rn" .$message;

                                                                                    if (isset($_POST['submit']))
                                                                                    {
                                                                                    if (mail ($to, $subject, $body, $from))
                                                                                    {
                                                                                    echo '<p>Your message has been sent!</p>';
                                                                                    }
                                                                                    else
                                                                                    {
                                                                                    echo '<p>Something went wrong, go back and try again!</p>';
                                                                                    }
                                                                                    }

                                                                                    ?>





                                                                                    share|improve this answer














                                                                                    I think this should do the trick. I just added an if(isset and added concatenation to the variables in the body to separate PHP from HTML.



                                                                                    <?php
                                                                                    $name = $_POST['name'];
                                                                                    $email = $_POST['email'];
                                                                                    $message = $_POST['message'];
                                                                                    $from = 'From: yoursite.com';
                                                                                    $to = 'contact@yoursite.com';
                                                                                    $subject = 'Customer Inquiry';
                                                                                    $body = "From:" .$name."rn E-Mail:" .$email."rn Message:rn" .$message;

                                                                                    if (isset($_POST['submit']))
                                                                                    {
                                                                                    if (mail ($to, $subject, $body, $from))
                                                                                    {
                                                                                    echo '<p>Your message has been sent!</p>';
                                                                                    }
                                                                                    else
                                                                                    {
                                                                                    echo '<p>Something went wrong, go back and try again!</p>';
                                                                                    }
                                                                                    }

                                                                                    ?>






                                                                                    share|improve this answer














                                                                                    share|improve this answer



                                                                                    share|improve this answer








                                                                                    edited Jan 12 '17 at 22:00









                                                                                    Nisse Engström

                                                                                    4,09292034




                                                                                    4,09292034










                                                                                    answered Jan 3 '15 at 2:32









                                                                                    Stephen M

                                                                                    7111




                                                                                    7111






















                                                                                        up vote
                                                                                        5
                                                                                        down vote













                                                                                        $name = $_POST['name'];
                                                                                        $email = $_POST['email'];
                                                                                        $reciver = '/* Reciver Email address */';
                                                                                        if (filter_var($reciver, FILTER_VALIDATE_EMAIL)) {
                                                                                        $subject = $name;
                                                                                        // To send HTML mail, the Content-type header must be set.
                                                                                        $headers = 'MIME-Version: 1.0' . "rn";
                                                                                        $headers .= 'Content-type: text/html; charset=iso-8859-1' . "rn";
                                                                                        $headers .= 'From:' . $email. "rn"; // Sender's Email
                                                                                        //$headers .= 'Cc:' . $email. "rn"; // Carbon copy to Sender
                                                                                        $template = '<div style="padding:50px; color:white;">Hello ,<br/>'
                                                                                        . '<br/><br/>'
                                                                                        . 'Name:' .$name.'<br/>'
                                                                                        . 'Email:' .$email.'<br/>'
                                                                                        . '<br/>'
                                                                                        . '</div>';
                                                                                        $sendmessage = "<div style="background-color:#7E7E7E; color:white;">" . $template . "</div>";
                                                                                        // Message lines should not exceed 70 characters (PHP rule), so wrap it.
                                                                                        $sendmessage = wordwrap($sendmessage, 70);
                                                                                        // Send mail by PHP Mail Function.
                                                                                        mail($reciver, $subject, $sendmessage, $headers);
                                                                                        echo "Your Query has been received, We will contact you soon.";
                                                                                        } else {
                                                                                        echo "<span>* invalid email *</span>";
                                                                                        }





                                                                                        share|improve this answer



























                                                                                          up vote
                                                                                          5
                                                                                          down vote













                                                                                          $name = $_POST['name'];
                                                                                          $email = $_POST['email'];
                                                                                          $reciver = '/* Reciver Email address */';
                                                                                          if (filter_var($reciver, FILTER_VALIDATE_EMAIL)) {
                                                                                          $subject = $name;
                                                                                          // To send HTML mail, the Content-type header must be set.
                                                                                          $headers = 'MIME-Version: 1.0' . "rn";
                                                                                          $headers .= 'Content-type: text/html; charset=iso-8859-1' . "rn";
                                                                                          $headers .= 'From:' . $email. "rn"; // Sender's Email
                                                                                          //$headers .= 'Cc:' . $email. "rn"; // Carbon copy to Sender
                                                                                          $template = '<div style="padding:50px; color:white;">Hello ,<br/>'
                                                                                          . '<br/><br/>'
                                                                                          . 'Name:' .$name.'<br/>'
                                                                                          . 'Email:' .$email.'<br/>'
                                                                                          . '<br/>'
                                                                                          . '</div>';
                                                                                          $sendmessage = "<div style="background-color:#7E7E7E; color:white;">" . $template . "</div>";
                                                                                          // Message lines should not exceed 70 characters (PHP rule), so wrap it.
                                                                                          $sendmessage = wordwrap($sendmessage, 70);
                                                                                          // Send mail by PHP Mail Function.
                                                                                          mail($reciver, $subject, $sendmessage, $headers);
                                                                                          echo "Your Query has been received, We will contact you soon.";
                                                                                          } else {
                                                                                          echo "<span>* invalid email *</span>";
                                                                                          }





                                                                                          share|improve this answer

























                                                                                            up vote
                                                                                            5
                                                                                            down vote










                                                                                            up vote
                                                                                            5
                                                                                            down vote









                                                                                            $name = $_POST['name'];
                                                                                            $email = $_POST['email'];
                                                                                            $reciver = '/* Reciver Email address */';
                                                                                            if (filter_var($reciver, FILTER_VALIDATE_EMAIL)) {
                                                                                            $subject = $name;
                                                                                            // To send HTML mail, the Content-type header must be set.
                                                                                            $headers = 'MIME-Version: 1.0' . "rn";
                                                                                            $headers .= 'Content-type: text/html; charset=iso-8859-1' . "rn";
                                                                                            $headers .= 'From:' . $email. "rn"; // Sender's Email
                                                                                            //$headers .= 'Cc:' . $email. "rn"; // Carbon copy to Sender
                                                                                            $template = '<div style="padding:50px; color:white;">Hello ,<br/>'
                                                                                            . '<br/><br/>'
                                                                                            . 'Name:' .$name.'<br/>'
                                                                                            . 'Email:' .$email.'<br/>'
                                                                                            . '<br/>'
                                                                                            . '</div>';
                                                                                            $sendmessage = "<div style="background-color:#7E7E7E; color:white;">" . $template . "</div>";
                                                                                            // Message lines should not exceed 70 characters (PHP rule), so wrap it.
                                                                                            $sendmessage = wordwrap($sendmessage, 70);
                                                                                            // Send mail by PHP Mail Function.
                                                                                            mail($reciver, $subject, $sendmessage, $headers);
                                                                                            echo "Your Query has been received, We will contact you soon.";
                                                                                            } else {
                                                                                            echo "<span>* invalid email *</span>";
                                                                                            }





                                                                                            share|improve this answer














                                                                                            $name = $_POST['name'];
                                                                                            $email = $_POST['email'];
                                                                                            $reciver = '/* Reciver Email address */';
                                                                                            if (filter_var($reciver, FILTER_VALIDATE_EMAIL)) {
                                                                                            $subject = $name;
                                                                                            // To send HTML mail, the Content-type header must be set.
                                                                                            $headers = 'MIME-Version: 1.0' . "rn";
                                                                                            $headers .= 'Content-type: text/html; charset=iso-8859-1' . "rn";
                                                                                            $headers .= 'From:' . $email. "rn"; // Sender's Email
                                                                                            //$headers .= 'Cc:' . $email. "rn"; // Carbon copy to Sender
                                                                                            $template = '<div style="padding:50px; color:white;">Hello ,<br/>'
                                                                                            . '<br/><br/>'
                                                                                            . 'Name:' .$name.'<br/>'
                                                                                            . 'Email:' .$email.'<br/>'
                                                                                            . '<br/>'
                                                                                            . '</div>';
                                                                                            $sendmessage = "<div style="background-color:#7E7E7E; color:white;">" . $template . "</div>";
                                                                                            // Message lines should not exceed 70 characters (PHP rule), so wrap it.
                                                                                            $sendmessage = wordwrap($sendmessage, 70);
                                                                                            // Send mail by PHP Mail Function.
                                                                                            mail($reciver, $subject, $sendmessage, $headers);
                                                                                            echo "Your Query has been received, We will contact you soon.";
                                                                                            } else {
                                                                                            echo "<span>* invalid email *</span>";
                                                                                            }






                                                                                            share|improve this answer














                                                                                            share|improve this answer



                                                                                            share|improve this answer








                                                                                            edited Jul 13 '15 at 7:32









                                                                                            MathieuF

                                                                                            2,95552634




                                                                                            2,95552634










                                                                                            answered Jul 13 '15 at 7:27









                                                                                            Chirag patel

                                                                                            5912




                                                                                            5912






















                                                                                                up vote
                                                                                                5
                                                                                                down vote













                                                                                                Try this



                                                                                                <?php
                                                                                                $to = "somebody@example.com, somebodyelse@example.com";
                                                                                                $subject = "HTML email";

                                                                                                $message = "
                                                                                                <html>
                                                                                                <head>
                                                                                                <title>HTML email</title>
                                                                                                </head>
                                                                                                <body>
                                                                                                <p>This email contains HTML Tags!</p>
                                                                                                <table>
                                                                                                <tr>
                                                                                                <th>Firstname</th>
                                                                                                <th>Lastname</th>
                                                                                                </tr>
                                                                                                <tr>
                                                                                                <td>John</td>
                                                                                                <td>Doe</td>
                                                                                                </tr>
                                                                                                </table>
                                                                                                </body>
                                                                                                </html>";

                                                                                                // Always set content-type when sending HTML email
                                                                                                $headers = "MIME-Version: 1.0" . "rn";
                                                                                                $headers .= "Content-type:text/html;charset=UTF-8" . "rn";

                                                                                                // More headers
                                                                                                $headers .= 'From: <webmaster@example.com>' . "rn";
                                                                                                $headers .= 'Cc: myboss@example.com' . "rn";

                                                                                                mail($to,$subject,$message,$headers);
                                                                                                ?>





                                                                                                share|improve this answer



















                                                                                                • 7




                                                                                                  Generally, answers are much more helpful if they include an explanation of what the code is intended to do, and why that solves the problem without introducing others.
                                                                                                  – IKavanagh
                                                                                                  Sep 15 '15 at 7:58















                                                                                                up vote
                                                                                                5
                                                                                                down vote













                                                                                                Try this



                                                                                                <?php
                                                                                                $to = "somebody@example.com, somebodyelse@example.com";
                                                                                                $subject = "HTML email";

                                                                                                $message = "
                                                                                                <html>
                                                                                                <head>
                                                                                                <title>HTML email</title>
                                                                                                </head>
                                                                                                <body>
                                                                                                <p>This email contains HTML Tags!</p>
                                                                                                <table>
                                                                                                <tr>
                                                                                                <th>Firstname</th>
                                                                                                <th>Lastname</th>
                                                                                                </tr>
                                                                                                <tr>
                                                                                                <td>John</td>
                                                                                                <td>Doe</td>
                                                                                                </tr>
                                                                                                </table>
                                                                                                </body>
                                                                                                </html>";

                                                                                                // Always set content-type when sending HTML email
                                                                                                $headers = "MIME-Version: 1.0" . "rn";
                                                                                                $headers .= "Content-type:text/html;charset=UTF-8" . "rn";

                                                                                                // More headers
                                                                                                $headers .= 'From: <webmaster@example.com>' . "rn";
                                                                                                $headers .= 'Cc: myboss@example.com' . "rn";

                                                                                                mail($to,$subject,$message,$headers);
                                                                                                ?>





                                                                                                share|improve this answer



















                                                                                                • 7




                                                                                                  Generally, answers are much more helpful if they include an explanation of what the code is intended to do, and why that solves the problem without introducing others.
                                                                                                  – IKavanagh
                                                                                                  Sep 15 '15 at 7:58













                                                                                                up vote
                                                                                                5
                                                                                                down vote










                                                                                                up vote
                                                                                                5
                                                                                                down vote









                                                                                                Try this



                                                                                                <?php
                                                                                                $to = "somebody@example.com, somebodyelse@example.com";
                                                                                                $subject = "HTML email";

                                                                                                $message = "
                                                                                                <html>
                                                                                                <head>
                                                                                                <title>HTML email</title>
                                                                                                </head>
                                                                                                <body>
                                                                                                <p>This email contains HTML Tags!</p>
                                                                                                <table>
                                                                                                <tr>
                                                                                                <th>Firstname</th>
                                                                                                <th>Lastname</th>
                                                                                                </tr>
                                                                                                <tr>
                                                                                                <td>John</td>
                                                                                                <td>Doe</td>
                                                                                                </tr>
                                                                                                </table>
                                                                                                </body>
                                                                                                </html>";

                                                                                                // Always set content-type when sending HTML email
                                                                                                $headers = "MIME-Version: 1.0" . "rn";
                                                                                                $headers .= "Content-type:text/html;charset=UTF-8" . "rn";

                                                                                                // More headers
                                                                                                $headers .= 'From: <webmaster@example.com>' . "rn";
                                                                                                $headers .= 'Cc: myboss@example.com' . "rn";

                                                                                                mail($to,$subject,$message,$headers);
                                                                                                ?>





                                                                                                share|improve this answer














                                                                                                Try this



                                                                                                <?php
                                                                                                $to = "somebody@example.com, somebodyelse@example.com";
                                                                                                $subject = "HTML email";

                                                                                                $message = "
                                                                                                <html>
                                                                                                <head>
                                                                                                <title>HTML email</title>
                                                                                                </head>
                                                                                                <body>
                                                                                                <p>This email contains HTML Tags!</p>
                                                                                                <table>
                                                                                                <tr>
                                                                                                <th>Firstname</th>
                                                                                                <th>Lastname</th>
                                                                                                </tr>
                                                                                                <tr>
                                                                                                <td>John</td>
                                                                                                <td>Doe</td>
                                                                                                </tr>
                                                                                                </table>
                                                                                                </body>
                                                                                                </html>";

                                                                                                // Always set content-type when sending HTML email
                                                                                                $headers = "MIME-Version: 1.0" . "rn";
                                                                                                $headers .= "Content-type:text/html;charset=UTF-8" . "rn";

                                                                                                // More headers
                                                                                                $headers .= 'From: <webmaster@example.com>' . "rn";
                                                                                                $headers .= 'Cc: myboss@example.com' . "rn";

                                                                                                mail($to,$subject,$message,$headers);
                                                                                                ?>






                                                                                                share|improve this answer














                                                                                                share|improve this answer



                                                                                                share|improve this answer








                                                                                                edited Feb 21 '17 at 7:39









                                                                                                JH_

                                                                                                397213




                                                                                                397213










                                                                                                answered Sep 15 '15 at 7:41









                                                                                                Ganga Reddy

                                                                                                8315




                                                                                                8315








                                                                                                • 7




                                                                                                  Generally, answers are much more helpful if they include an explanation of what the code is intended to do, and why that solves the problem without introducing others.
                                                                                                  – IKavanagh
                                                                                                  Sep 15 '15 at 7:58














                                                                                                • 7




                                                                                                  Generally, answers are much more helpful if they include an explanation of what the code is intended to do, and why that solves the problem without introducing others.
                                                                                                  – IKavanagh
                                                                                                  Sep 15 '15 at 7:58








                                                                                                7




                                                                                                7




                                                                                                Generally, answers are much more helpful if they include an explanation of what the code is intended to do, and why that solves the problem without introducing others.
                                                                                                – IKavanagh
                                                                                                Sep 15 '15 at 7:58




                                                                                                Generally, answers are much more helpful if they include an explanation of what the code is intended to do, and why that solves the problem without introducing others.
                                                                                                – IKavanagh
                                                                                                Sep 15 '15 at 7:58










                                                                                                up vote
                                                                                                3
                                                                                                down vote













                                                                                                Try this



                                                                                                if ($_POST['submit']) {
                                                                                                $success= mail($to, $subject, $body, $from);
                                                                                                if($success)
                                                                                                {
                                                                                                echo '
                                                                                                <p>Your message has been sent!</p>
                                                                                                ';
                                                                                                } else {
                                                                                                echo '
                                                                                                <p>Something went wrong, go back and try again!</p>
                                                                                                ';
                                                                                                }
                                                                                                }





                                                                                                share|improve this answer



























                                                                                                  up vote
                                                                                                  3
                                                                                                  down vote













                                                                                                  Try this



                                                                                                  if ($_POST['submit']) {
                                                                                                  $success= mail($to, $subject, $body, $from);
                                                                                                  if($success)
                                                                                                  {
                                                                                                  echo '
                                                                                                  <p>Your message has been sent!</p>
                                                                                                  ';
                                                                                                  } else {
                                                                                                  echo '
                                                                                                  <p>Something went wrong, go back and try again!</p>
                                                                                                  ';
                                                                                                  }
                                                                                                  }





                                                                                                  share|improve this answer

























                                                                                                    up vote
                                                                                                    3
                                                                                                    down vote










                                                                                                    up vote
                                                                                                    3
                                                                                                    down vote









                                                                                                    Try this



                                                                                                    if ($_POST['submit']) {
                                                                                                    $success= mail($to, $subject, $body, $from);
                                                                                                    if($success)
                                                                                                    {
                                                                                                    echo '
                                                                                                    <p>Your message has been sent!</p>
                                                                                                    ';
                                                                                                    } else {
                                                                                                    echo '
                                                                                                    <p>Something went wrong, go back and try again!</p>
                                                                                                    ';
                                                                                                    }
                                                                                                    }





                                                                                                    share|improve this answer














                                                                                                    Try this



                                                                                                    if ($_POST['submit']) {
                                                                                                    $success= mail($to, $subject, $body, $from);
                                                                                                    if($success)
                                                                                                    {
                                                                                                    echo '
                                                                                                    <p>Your message has been sent!</p>
                                                                                                    ';
                                                                                                    } else {
                                                                                                    echo '
                                                                                                    <p>Something went wrong, go back and try again!</p>
                                                                                                    ';
                                                                                                    }
                                                                                                    }






                                                                                                    share|improve this answer














                                                                                                    share|improve this answer



                                                                                                    share|improve this answer








                                                                                                    edited Jun 23 '15 at 11:39









                                                                                                    Ryan Vincent

                                                                                                    3,87231224




                                                                                                    3,87231224










                                                                                                    answered Jun 23 '15 at 10:58









                                                                                                    Gaurav Sirauthiya

                                                                                                    491




                                                                                                    491






















                                                                                                        up vote
                                                                                                        3
                                                                                                        down vote













                                                                                                        Make sure you have Sendmail installed in your server.



                                                                                                        If you have checked your code and verified that there is nothing wrong there, go to /var/mail and check whether that folder is empty.



                                                                                                        If it is empty, you will need to do a:



                                                                                                        sudo apt-get install sendmail


                                                                                                        if you are on an Ubuntu server.






                                                                                                        share|improve this answer





















                                                                                                        • That's way too coarse to be useful. Merely installing an MTA will seldomly allow to get to it. And a full sendmail/postfix/qmail setup guide would be off-topic here.
                                                                                                          – mario
                                                                                                          Nov 16 '17 at 17:24










                                                                                                        • Is this comment in reference to my answer? Because this hasn't happened to me and I didn't solve it this same exact way. There are various possible answers to the question, mine being one of them after having tried all of the above in the past, my answer had solved the problem for me, so I offered it here as another potential solution.
                                                                                                          – Daniel
                                                                                                          Nov 17 '17 at 15:22










                                                                                                        • Yes sure. It's one of many possible answers. However, it's still way to superficial IMO. Generalizing it into "you need an MTA" such as … - and here are a few setup guides linked… might be more practical however.
                                                                                                          – mario
                                                                                                          Nov 17 '17 at 15:28















                                                                                                        up vote
                                                                                                        3
                                                                                                        down vote













                                                                                                        Make sure you have Sendmail installed in your server.



                                                                                                        If you have checked your code and verified that there is nothing wrong there, go to /var/mail and check whether that folder is empty.



                                                                                                        If it is empty, you will need to do a:



                                                                                                        sudo apt-get install sendmail


                                                                                                        if you are on an Ubuntu server.






                                                                                                        share|improve this answer





















                                                                                                        • That's way too coarse to be useful. Merely installing an MTA will seldomly allow to get to it. And a full sendmail/postfix/qmail setup guide would be off-topic here.
                                                                                                          – mario
                                                                                                          Nov 16 '17 at 17:24










                                                                                                        • Is this comment in reference to my answer? Because this hasn't happened to me and I didn't solve it this same exact way. There are various possible answers to the question, mine being one of them after having tried all of the above in the past, my answer had solved the problem for me, so I offered it here as another potential solution.
                                                                                                          – Daniel
                                                                                                          Nov 17 '17 at 15:22










                                                                                                        • Yes sure. It's one of many possible answers. However, it's still way to superficial IMO. Generalizing it into "you need an MTA" such as … - and here are a few setup guides linked… might be more practical however.
                                                                                                          – mario
                                                                                                          Nov 17 '17 at 15:28













                                                                                                        up vote
                                                                                                        3
                                                                                                        down vote










                                                                                                        up vote
                                                                                                        3
                                                                                                        down vote









                                                                                                        Make sure you have Sendmail installed in your server.



                                                                                                        If you have checked your code and verified that there is nothing wrong there, go to /var/mail and check whether that folder is empty.



                                                                                                        If it is empty, you will need to do a:



                                                                                                        sudo apt-get install sendmail


                                                                                                        if you are on an Ubuntu server.






                                                                                                        share|improve this answer












                                                                                                        Make sure you have Sendmail installed in your server.



                                                                                                        If you have checked your code and verified that there is nothing wrong there, go to /var/mail and check whether that folder is empty.



                                                                                                        If it is empty, you will need to do a:



                                                                                                        sudo apt-get install sendmail


                                                                                                        if you are on an Ubuntu server.







                                                                                                        share|improve this answer












                                                                                                        share|improve this answer



                                                                                                        share|improve this answer










                                                                                                        answered Sep 24 '17 at 3:50









                                                                                                        Daniel

                                                                                                        1,59511228




                                                                                                        1,59511228












                                                                                                        • That's way too coarse to be useful. Merely installing an MTA will seldomly allow to get to it. And a full sendmail/postfix/qmail setup guide would be off-topic here.
                                                                                                          – mario
                                                                                                          Nov 16 '17 at 17:24










                                                                                                        • Is this comment in reference to my answer? Because this hasn't happened to me and I didn't solve it this same exact way. There are various possible answers to the question, mine being one of them after having tried all of the above in the past, my answer had solved the problem for me, so I offered it here as another potential solution.
                                                                                                          – Daniel
                                                                                                          Nov 17 '17 at 15:22










                                                                                                        • Yes sure. It's one of many possible answers. However, it's still way to superficial IMO. Generalizing it into "you need an MTA" such as … - and here are a few setup guides linked… might be more practical however.
                                                                                                          – mario
                                                                                                          Nov 17 '17 at 15:28


















                                                                                                        • That's way too coarse to be useful. Merely installing an MTA will seldomly allow to get to it. And a full sendmail/postfix/qmail setup guide would be off-topic here.
                                                                                                          – mario
                                                                                                          Nov 16 '17 at 17:24










                                                                                                        • Is this comment in reference to my answer? Because this hasn't happened to me and I didn't solve it this same exact way. There are various possible answers to the question, mine being one of them after having tried all of the above in the past, my answer had solved the problem for me, so I offered it here as another potential solution.
                                                                                                          – Daniel
                                                                                                          Nov 17 '17 at 15:22










                                                                                                        • Yes sure. It's one of many possible answers. However, it's still way to superficial IMO. Generalizing it into "you need an MTA" such as … - and here are a few setup guides linked… might be more practical however.
                                                                                                          – mario
                                                                                                          Nov 17 '17 at 15:28
















                                                                                                        That's way too coarse to be useful. Merely installing an MTA will seldomly allow to get to it. And a full sendmail/postfix/qmail setup guide would be off-topic here.
                                                                                                        – mario
                                                                                                        Nov 16 '17 at 17:24




                                                                                                        That's way too coarse to be useful. Merely installing an MTA will seldomly allow to get to it. And a full sendmail/postfix/qmail setup guide would be off-topic here.
                                                                                                        – mario
                                                                                                        Nov 16 '17 at 17:24












                                                                                                        Is this comment in reference to my answer? Because this hasn't happened to me and I didn't solve it this same exact way. There are various possible answers to the question, mine being one of them after having tried all of the above in the past, my answer had solved the problem for me, so I offered it here as another potential solution.
                                                                                                        – Daniel
                                                                                                        Nov 17 '17 at 15:22




                                                                                                        Is this comment in reference to my answer? Because this hasn't happened to me and I didn't solve it this same exact way. There are various possible answers to the question, mine being one of them after having tried all of the above in the past, my answer had solved the problem for me, so I offered it here as another potential solution.
                                                                                                        – Daniel
                                                                                                        Nov 17 '17 at 15:22












                                                                                                        Yes sure. It's one of many possible answers. However, it's still way to superficial IMO. Generalizing it into "you need an MTA" such as … - and here are a few setup guides linked… might be more practical however.
                                                                                                        – mario
                                                                                                        Nov 17 '17 at 15:28




                                                                                                        Yes sure. It's one of many possible answers. However, it's still way to superficial IMO. Generalizing it into "you need an MTA" such as … - and here are a few setup guides linked… might be more practical however.
                                                                                                        – mario
                                                                                                        Nov 17 '17 at 15:28










                                                                                                        up vote
                                                                                                        2
                                                                                                        down vote













                                                                                                        If you're having trouble sending mails with PHP, consider an alternative like PHPMailer or SwiftMailer.



                                                                                                        I usually use SwiftMailer whenever I need to send mails with PHP.





                                                                                                        Basic usage :



                                                                                                        require 'mail/swift_required.php';

                                                                                                        $message = Swift_Message::newInstance()
                                                                                                        // The subject of your email
                                                                                                        ->setSubject('Jane Doe sends you a message')
                                                                                                        // The from address(es)
                                                                                                        ->setFrom(array('jane.doe@gmail.com' => 'Jane Doe'))
                                                                                                        // The to address(es)
                                                                                                        ->setTo(array('frank.stevens@gmail.com' => 'Frank Stevens'))
                                                                                                        // Here, you put the content of your email
                                                                                                        ->setBody('<h3>New message</h3><p>Here goes the rest of my message</p>', 'text/html');

                                                                                                        if (Swift_Mailer::newInstance(Swift_MailTransport::newInstance())->send($message)) {
                                                                                                        echo json_encode([
                                                                                                        "status" => "OK",
                                                                                                        "message" => 'Your message has been sent!'
                                                                                                        ], JSON_PRETTY_PRINT);
                                                                                                        } else {
                                                                                                        echo json_encode([
                                                                                                        "status" => "error",
                                                                                                        "message" => 'Oops! Something went wrong!'
                                                                                                        ], JSON_PRETTY_PRINT);
                                                                                                        }


                                                                                                        See the official documentation for more info on how to use SwiftMailer.






                                                                                                        share|improve this answer

























                                                                                                          up vote
                                                                                                          2
                                                                                                          down vote













                                                                                                          If you're having trouble sending mails with PHP, consider an alternative like PHPMailer or SwiftMailer.



                                                                                                          I usually use SwiftMailer whenever I need to send mails with PHP.





                                                                                                          Basic usage :



                                                                                                          require 'mail/swift_required.php';

                                                                                                          $message = Swift_Message::newInstance()
                                                                                                          // The subject of your email
                                                                                                          ->setSubject('Jane Doe sends you a message')
                                                                                                          // The from address(es)
                                                                                                          ->setFrom(array('jane.doe@gmail.com' => 'Jane Doe'))
                                                                                                          // The to address(es)
                                                                                                          ->setTo(array('frank.stevens@gmail.com' => 'Frank Stevens'))
                                                                                                          // Here, you put the content of your email
                                                                                                          ->setBody('<h3>New message</h3><p>Here goes the rest of my message</p>', 'text/html');

                                                                                                          if (Swift_Mailer::newInstance(Swift_MailTransport::newInstance())->send($message)) {
                                                                                                          echo json_encode([
                                                                                                          "status" => "OK",
                                                                                                          "message" => 'Your message has been sent!'
                                                                                                          ], JSON_PRETTY_PRINT);
                                                                                                          } else {
                                                                                                          echo json_encode([
                                                                                                          "status" => "error",
                                                                                                          "message" => 'Oops! Something went wrong!'
                                                                                                          ], JSON_PRETTY_PRINT);
                                                                                                          }


                                                                                                          See the official documentation for more info on how to use SwiftMailer.






                                                                                                          share|improve this answer























                                                                                                            up vote
                                                                                                            2
                                                                                                            down vote










                                                                                                            up vote
                                                                                                            2
                                                                                                            down vote









                                                                                                            If you're having trouble sending mails with PHP, consider an alternative like PHPMailer or SwiftMailer.



                                                                                                            I usually use SwiftMailer whenever I need to send mails with PHP.





                                                                                                            Basic usage :



                                                                                                            require 'mail/swift_required.php';

                                                                                                            $message = Swift_Message::newInstance()
                                                                                                            // The subject of your email
                                                                                                            ->setSubject('Jane Doe sends you a message')
                                                                                                            // The from address(es)
                                                                                                            ->setFrom(array('jane.doe@gmail.com' => 'Jane Doe'))
                                                                                                            // The to address(es)
                                                                                                            ->setTo(array('frank.stevens@gmail.com' => 'Frank Stevens'))
                                                                                                            // Here, you put the content of your email
                                                                                                            ->setBody('<h3>New message</h3><p>Here goes the rest of my message</p>', 'text/html');

                                                                                                            if (Swift_Mailer::newInstance(Swift_MailTransport::newInstance())->send($message)) {
                                                                                                            echo json_encode([
                                                                                                            "status" => "OK",
                                                                                                            "message" => 'Your message has been sent!'
                                                                                                            ], JSON_PRETTY_PRINT);
                                                                                                            } else {
                                                                                                            echo json_encode([
                                                                                                            "status" => "error",
                                                                                                            "message" => 'Oops! Something went wrong!'
                                                                                                            ], JSON_PRETTY_PRINT);
                                                                                                            }


                                                                                                            See the official documentation for more info on how to use SwiftMailer.






                                                                                                            share|improve this answer












                                                                                                            If you're having trouble sending mails with PHP, consider an alternative like PHPMailer or SwiftMailer.



                                                                                                            I usually use SwiftMailer whenever I need to send mails with PHP.





                                                                                                            Basic usage :



                                                                                                            require 'mail/swift_required.php';

                                                                                                            $message = Swift_Message::newInstance()
                                                                                                            // The subject of your email
                                                                                                            ->setSubject('Jane Doe sends you a message')
                                                                                                            // The from address(es)
                                                                                                            ->setFrom(array('jane.doe@gmail.com' => 'Jane Doe'))
                                                                                                            // The to address(es)
                                                                                                            ->setTo(array('frank.stevens@gmail.com' => 'Frank Stevens'))
                                                                                                            // Here, you put the content of your email
                                                                                                            ->setBody('<h3>New message</h3><p>Here goes the rest of my message</p>', 'text/html');

                                                                                                            if (Swift_Mailer::newInstance(Swift_MailTransport::newInstance())->send($message)) {
                                                                                                            echo json_encode([
                                                                                                            "status" => "OK",
                                                                                                            "message" => 'Your message has been sent!'
                                                                                                            ], JSON_PRETTY_PRINT);
                                                                                                            } else {
                                                                                                            echo json_encode([
                                                                                                            "status" => "error",
                                                                                                            "message" => 'Oops! Something went wrong!'
                                                                                                            ], JSON_PRETTY_PRINT);
                                                                                                            }


                                                                                                            See the official documentation for more info on how to use SwiftMailer.







                                                                                                            share|improve this answer












                                                                                                            share|improve this answer



                                                                                                            share|improve this answer










                                                                                                            answered Feb 10 '16 at 13:23









                                                                                                            John Slegers

                                                                                                            27.3k13143127




                                                                                                            27.3k13143127






















                                                                                                                up vote
                                                                                                                2
                                                                                                                down vote













                                                                                                                Maybe the problem is the configuration of the mail server, to avoid this type of problems or you do not have to worry about the mail server problem, I recommend you use PHPMailer, it is a plugin that has everything necessary to send mail, the only thing you have to take into account is to have the SMTP port (Port: 25 and 465), enabled



                                                                                                                require_once 'PHPMailer/PHPMailer.php';
                                                                                                                require_once '/servicios/PHPMailer/SMTP.php';
                                                                                                                require_once '/servicios/PHPMailer/Exception.php';

                                                                                                                $mail = new PHPMailerPHPMailerPHPMailer(true);
                                                                                                                try {
                                                                                                                //Server settings
                                                                                                                $mail->SMTPDebug = 0;
                                                                                                                $mail->isSMTP();
                                                                                                                $mail->Host = 'smtp.gmail.com';
                                                                                                                $mail->SMTPAuth = true;
                                                                                                                $mail->Username = 'correo@gmail.com';
                                                                                                                $mail->Password = 'contrasenia';
                                                                                                                $mail->SMTPSecure = 'ssl';
                                                                                                                $mail->Port = 465;

                                                                                                                //Recipients
                                                                                                                $mail->setFrom('correo@gmail.com', 'my name');
                                                                                                                $mail->addAddress('destination@correo.com');

                                                                                                                //Attachments
                                                                                                                $mail->addAttachment('optional file'); // Add files, is optional

                                                                                                                //Content
                                                                                                                $mail->isHTML(true);// Set email format to HTML
                                                                                                                $mail->Subject = utf8_decode("subject");
                                                                                                                $mail->Body = utf8_decode("mail content");
                                                                                                                $mail->AltBody = '';
                                                                                                                $mail->send();
                                                                                                                }
                                                                                                                catch (Exception $e){
                                                                                                                $error = $mail->ErrorInfo;
                                                                                                                }





                                                                                                                share|improve this answer

























                                                                                                                  up vote
                                                                                                                  2
                                                                                                                  down vote













                                                                                                                  Maybe the problem is the configuration of the mail server, to avoid this type of problems or you do not have to worry about the mail server problem, I recommend you use PHPMailer, it is a plugin that has everything necessary to send mail, the only thing you have to take into account is to have the SMTP port (Port: 25 and 465), enabled



                                                                                                                  require_once 'PHPMailer/PHPMailer.php';
                                                                                                                  require_once '/servicios/PHPMailer/SMTP.php';
                                                                                                                  require_once '/servicios/PHPMailer/Exception.php';

                                                                                                                  $mail = new PHPMailerPHPMailerPHPMailer(true);
                                                                                                                  try {
                                                                                                                  //Server settings
                                                                                                                  $mail->SMTPDebug = 0;
                                                                                                                  $mail->isSMTP();
                                                                                                                  $mail->Host = 'smtp.gmail.com';
                                                                                                                  $mail->SMTPAuth = true;
                                                                                                                  $mail->Username = 'correo@gmail.com';
                                                                                                                  $mail->Password = 'contrasenia';
                                                                                                                  $mail->SMTPSecure = 'ssl';
                                                                                                                  $mail->Port = 465;

                                                                                                                  //Recipients
                                                                                                                  $mail->setFrom('correo@gmail.com', 'my name');
                                                                                                                  $mail->addAddress('destination@correo.com');

                                                                                                                  //Attachments
                                                                                                                  $mail->addAttachment('optional file'); // Add files, is optional

                                                                                                                  //Content
                                                                                                                  $mail->isHTML(true);// Set email format to HTML
                                                                                                                  $mail->Subject = utf8_decode("subject");
                                                                                                                  $mail->Body = utf8_decode("mail content");
                                                                                                                  $mail->AltBody = '';
                                                                                                                  $mail->send();
                                                                                                                  }
                                                                                                                  catch (Exception $e){
                                                                                                                  $error = $mail->ErrorInfo;
                                                                                                                  }





                                                                                                                  share|improve this answer























                                                                                                                    up vote
                                                                                                                    2
                                                                                                                    down vote










                                                                                                                    up vote
                                                                                                                    2
                                                                                                                    down vote









                                                                                                                    Maybe the problem is the configuration of the mail server, to avoid this type of problems or you do not have to worry about the mail server problem, I recommend you use PHPMailer, it is a plugin that has everything necessary to send mail, the only thing you have to take into account is to have the SMTP port (Port: 25 and 465), enabled



                                                                                                                    require_once 'PHPMailer/PHPMailer.php';
                                                                                                                    require_once '/servicios/PHPMailer/SMTP.php';
                                                                                                                    require_once '/servicios/PHPMailer/Exception.php';

                                                                                                                    $mail = new PHPMailerPHPMailerPHPMailer(true);
                                                                                                                    try {
                                                                                                                    //Server settings
                                                                                                                    $mail->SMTPDebug = 0;
                                                                                                                    $mail->isSMTP();
                                                                                                                    $mail->Host = 'smtp.gmail.com';
                                                                                                                    $mail->SMTPAuth = true;
                                                                                                                    $mail->Username = 'correo@gmail.com';
                                                                                                                    $mail->Password = 'contrasenia';
                                                                                                                    $mail->SMTPSecure = 'ssl';
                                                                                                                    $mail->Port = 465;

                                                                                                                    //Recipients
                                                                                                                    $mail->setFrom('correo@gmail.com', 'my name');
                                                                                                                    $mail->addAddress('destination@correo.com');

                                                                                                                    //Attachments
                                                                                                                    $mail->addAttachment('optional file'); // Add files, is optional

                                                                                                                    //Content
                                                                                                                    $mail->isHTML(true);// Set email format to HTML
                                                                                                                    $mail->Subject = utf8_decode("subject");
                                                                                                                    $mail->Body = utf8_decode("mail content");
                                                                                                                    $mail->AltBody = '';
                                                                                                                    $mail->send();
                                                                                                                    }
                                                                                                                    catch (Exception $e){
                                                                                                                    $error = $mail->ErrorInfo;
                                                                                                                    }





                                                                                                                    share|improve this answer












                                                                                                                    Maybe the problem is the configuration of the mail server, to avoid this type of problems or you do not have to worry about the mail server problem, I recommend you use PHPMailer, it is a plugin that has everything necessary to send mail, the only thing you have to take into account is to have the SMTP port (Port: 25 and 465), enabled



                                                                                                                    require_once 'PHPMailer/PHPMailer.php';
                                                                                                                    require_once '/servicios/PHPMailer/SMTP.php';
                                                                                                                    require_once '/servicios/PHPMailer/Exception.php';

                                                                                                                    $mail = new PHPMailerPHPMailerPHPMailer(true);
                                                                                                                    try {
                                                                                                                    //Server settings
                                                                                                                    $mail->SMTPDebug = 0;
                                                                                                                    $mail->isSMTP();
                                                                                                                    $mail->Host = 'smtp.gmail.com';
                                                                                                                    $mail->SMTPAuth = true;
                                                                                                                    $mail->Username = 'correo@gmail.com';
                                                                                                                    $mail->Password = 'contrasenia';
                                                                                                                    $mail->SMTPSecure = 'ssl';
                                                                                                                    $mail->Port = 465;

                                                                                                                    //Recipients
                                                                                                                    $mail->setFrom('correo@gmail.com', 'my name');
                                                                                                                    $mail->addAddress('destination@correo.com');

                                                                                                                    //Attachments
                                                                                                                    $mail->addAttachment('optional file'); // Add files, is optional

                                                                                                                    //Content
                                                                                                                    $mail->isHTML(true);// Set email format to HTML
                                                                                                                    $mail->Subject = utf8_decode("subject");
                                                                                                                    $mail->Body = utf8_decode("mail content");
                                                                                                                    $mail->AltBody = '';
                                                                                                                    $mail->send();
                                                                                                                    }
                                                                                                                    catch (Exception $e){
                                                                                                                    $error = $mail->ErrorInfo;
                                                                                                                    }






                                                                                                                    share|improve this answer












                                                                                                                    share|improve this answer



                                                                                                                    share|improve this answer










                                                                                                                    answered Mar 19 at 19:09









                                                                                                                    Jack Ch

                                                                                                                    311




                                                                                                                    311






















                                                                                                                        up vote
                                                                                                                        2
                                                                                                                        down vote













                                                                                                                        For those who do not want to use external mailers and want to mail() on a dedicated linux server.



                                                                                                                        The way, how php mails, is described in php.ini in section [mail function].
                                                                                                                        Parameter sendmail-path describes how sendmail is called. Default value is sendmail -t -i, so if you get working sendmail -t -i < message.txt in linux console - you will be done. You could also add mail.log to debug and be sure mail() is really called.



                                                                                                                        Different MTA can implement sendmail, they just make symbolic link to their binaries on that name. For example, in debian default is postfix. Configure your MTA to send mail and test it from console with sendmail -v -t -i < message.txt. File message.txt should contain all headers of a message and a body, destination addres for envelope will be taken from To: header. Example:



                                                                                                                        From: myapp@example.com
                                                                                                                        To: mymail@example.com
                                                                                                                        Subject: Test mail via sendmail.

                                                                                                                        Text body.


                                                                                                                        I prefere to use ssmtp as MTA because it is simple and do not require running daemon with opened ports. ssmtp fits only for sending mail from local host, it also can send authenticated email via your account on a public mail service. Install ssmtp and edit config /etc/ssmtp/ssmtp.conf. To be able also to recive local system mail to unix accounts(alerts to root from cron jobs, for example) configure /etc/ssmtp/revaliases file.



                                                                                                                        Here is my config for my account on Yandex mail:



                                                                                                                        root=mymail@example.com
                                                                                                                        mailhub=smtp.yandex.ru:465
                                                                                                                        FromLineOverride=YES
                                                                                                                        UseTLS=YES
                                                                                                                        AuthUser=abcde@yandex.ru
                                                                                                                        AuthPass=password





                                                                                                                        share|improve this answer



























                                                                                                                          up vote
                                                                                                                          2
                                                                                                                          down vote













                                                                                                                          For those who do not want to use external mailers and want to mail() on a dedicated linux server.



                                                                                                                          The way, how php mails, is described in php.ini in section [mail function].
                                                                                                                          Parameter sendmail-path describes how sendmail is called. Default value is sendmail -t -i, so if you get working sendmail -t -i < message.txt in linux console - you will be done. You could also add mail.log to debug and be sure mail() is really called.



                                                                                                                          Different MTA can implement sendmail, they just make symbolic link to their binaries on that name. For example, in debian default is postfix. Configure your MTA to send mail and test it from console with sendmail -v -t -i < message.txt. File message.txt should contain all headers of a message and a body, destination addres for envelope will be taken from To: header. Example:



                                                                                                                          From: myapp@example.com
                                                                                                                          To: mymail@example.com
                                                                                                                          Subject: Test mail via sendmail.

                                                                                                                          Text body.


                                                                                                                          I prefere to use ssmtp as MTA because it is simple and do not require running daemon with opened ports. ssmtp fits only for sending mail from local host, it also can send authenticated email via your account on a public mail service. Install ssmtp and edit config /etc/ssmtp/ssmtp.conf. To be able also to recive local system mail to unix accounts(alerts to root from cron jobs, for example) configure /etc/ssmtp/revaliases file.



                                                                                                                          Here is my config for my account on Yandex mail:



                                                                                                                          root=mymail@example.com
                                                                                                                          mailhub=smtp.yandex.ru:465
                                                                                                                          FromLineOverride=YES
                                                                                                                          UseTLS=YES
                                                                                                                          AuthUser=abcde@yandex.ru
                                                                                                                          AuthPass=password





                                                                                                                          share|improve this answer

























                                                                                                                            up vote
                                                                                                                            2
                                                                                                                            down vote










                                                                                                                            up vote
                                                                                                                            2
                                                                                                                            down vote









                                                                                                                            For those who do not want to use external mailers and want to mail() on a dedicated linux server.



                                                                                                                            The way, how php mails, is described in php.ini in section [mail function].
                                                                                                                            Parameter sendmail-path describes how sendmail is called. Default value is sendmail -t -i, so if you get working sendmail -t -i < message.txt in linux console - you will be done. You could also add mail.log to debug and be sure mail() is really called.



                                                                                                                            Different MTA can implement sendmail, they just make symbolic link to their binaries on that name. For example, in debian default is postfix. Configure your MTA to send mail and test it from console with sendmail -v -t -i < message.txt. File message.txt should contain all headers of a message and a body, destination addres for envelope will be taken from To: header. Example:



                                                                                                                            From: myapp@example.com
                                                                                                                            To: mymail@example.com
                                                                                                                            Subject: Test mail via sendmail.

                                                                                                                            Text body.


                                                                                                                            I prefere to use ssmtp as MTA because it is simple and do not require running daemon with opened ports. ssmtp fits only for sending mail from local host, it also can send authenticated email via your account on a public mail service. Install ssmtp and edit config /etc/ssmtp/ssmtp.conf. To be able also to recive local system mail to unix accounts(alerts to root from cron jobs, for example) configure /etc/ssmtp/revaliases file.



                                                                                                                            Here is my config for my account on Yandex mail:



                                                                                                                            root=mymail@example.com
                                                                                                                            mailhub=smtp.yandex.ru:465
                                                                                                                            FromLineOverride=YES
                                                                                                                            UseTLS=YES
                                                                                                                            AuthUser=abcde@yandex.ru
                                                                                                                            AuthPass=password





                                                                                                                            share|improve this answer














                                                                                                                            For those who do not want to use external mailers and want to mail() on a dedicated linux server.



                                                                                                                            The way, how php mails, is described in php.ini in section [mail function].
                                                                                                                            Parameter sendmail-path describes how sendmail is called. Default value is sendmail -t -i, so if you get working sendmail -t -i < message.txt in linux console - you will be done. You could also add mail.log to debug and be sure mail() is really called.



                                                                                                                            Different MTA can implement sendmail, they just make symbolic link to their binaries on that name. For example, in debian default is postfix. Configure your MTA to send mail and test it from console with sendmail -v -t -i < message.txt. File message.txt should contain all headers of a message and a body, destination addres for envelope will be taken from To: header. Example:



                                                                                                                            From: myapp@example.com
                                                                                                                            To: mymail@example.com
                                                                                                                            Subject: Test mail via sendmail.

                                                                                                                            Text body.


                                                                                                                            I prefere to use ssmtp as MTA because it is simple and do not require running daemon with opened ports. ssmtp fits only for sending mail from local host, it also can send authenticated email via your account on a public mail service. Install ssmtp and edit config /etc/ssmtp/ssmtp.conf. To be able also to recive local system mail to unix accounts(alerts to root from cron jobs, for example) configure /etc/ssmtp/revaliases file.



                                                                                                                            Here is my config for my account on Yandex mail:



                                                                                                                            root=mymail@example.com
                                                                                                                            mailhub=smtp.yandex.ru:465
                                                                                                                            FromLineOverride=YES
                                                                                                                            UseTLS=YES
                                                                                                                            AuthUser=abcde@yandex.ru
                                                                                                                            AuthPass=password






                                                                                                                            share|improve this answer














                                                                                                                            share|improve this answer



                                                                                                                            share|improve this answer








                                                                                                                            edited Jul 24 at 3:55

























                                                                                                                            answered Jun 21 '17 at 12:07









                                                                                                                            user3132194

                                                                                                                            924913




                                                                                                                            924913






















                                                                                                                                up vote
                                                                                                                                1
                                                                                                                                down vote













                                                                                                                                First of all,



                                                                                                                                You might have to many parameters for the mail() function...
                                                                                                                                You are able to have 5 max. mail(to,subject,message,headers,parameters);
                                                                                                                                As far as the $from variable goes, that should automatically come from your webhost if your using linux cPanel. It automatically comes from your cPanel username and ip address.



                                                                                                                                $name = $_POST['name'];
                                                                                                                                $email = $_POST['email'];
                                                                                                                                $message = $_POST['message'];
                                                                                                                                $from = 'From: yoursite.com';
                                                                                                                                $to = 'contact@yoursite.com';
                                                                                                                                $subject = 'Customer Inquiry';
                                                                                                                                $body = "From: $namen E-Mail: $emailn Message:n $message";


                                                                                                                                Also make sure you have the correct order of variables in your mail() function.
                                                                                                                                the mail($to,$subject,$message,etc.) in that order, or else there is a chance of it not working.
                                                                                                                                Let me know if this helps...






                                                                                                                                share|improve this answer

























                                                                                                                                  up vote
                                                                                                                                  1
                                                                                                                                  down vote













                                                                                                                                  First of all,



                                                                                                                                  You might have to many parameters for the mail() function...
                                                                                                                                  You are able to have 5 max. mail(to,subject,message,headers,parameters);
                                                                                                                                  As far as the $from variable goes, that should automatically come from your webhost if your using linux cPanel. It automatically comes from your cPanel username and ip address.



                                                                                                                                  $name = $_POST['name'];
                                                                                                                                  $email = $_POST['email'];
                                                                                                                                  $message = $_POST['message'];
                                                                                                                                  $from = 'From: yoursite.com';
                                                                                                                                  $to = 'contact@yoursite.com';
                                                                                                                                  $subject = 'Customer Inquiry';
                                                                                                                                  $body = "From: $namen E-Mail: $emailn Message:n $message";


                                                                                                                                  Also make sure you have the correct order of variables in your mail() function.
                                                                                                                                  the mail($to,$subject,$message,etc.) in that order, or else there is a chance of it not working.
                                                                                                                                  Let me know if this helps...






                                                                                                                                  share|improve this answer























                                                                                                                                    up vote
                                                                                                                                    1
                                                                                                                                    down vote










                                                                                                                                    up vote
                                                                                                                                    1
                                                                                                                                    down vote









                                                                                                                                    First of all,



                                                                                                                                    You might have to many parameters for the mail() function...
                                                                                                                                    You are able to have 5 max. mail(to,subject,message,headers,parameters);
                                                                                                                                    As far as the $from variable goes, that should automatically come from your webhost if your using linux cPanel. It automatically comes from your cPanel username and ip address.



                                                                                                                                    $name = $_POST['name'];
                                                                                                                                    $email = $_POST['email'];
                                                                                                                                    $message = $_POST['message'];
                                                                                                                                    $from = 'From: yoursite.com';
                                                                                                                                    $to = 'contact@yoursite.com';
                                                                                                                                    $subject = 'Customer Inquiry';
                                                                                                                                    $body = "From: $namen E-Mail: $emailn Message:n $message";


                                                                                                                                    Also make sure you have the correct order of variables in your mail() function.
                                                                                                                                    the mail($to,$subject,$message,etc.) in that order, or else there is a chance of it not working.
                                                                                                                                    Let me know if this helps...






                                                                                                                                    share|improve this answer












                                                                                                                                    First of all,



                                                                                                                                    You might have to many parameters for the mail() function...
                                                                                                                                    You are able to have 5 max. mail(to,subject,message,headers,parameters);
                                                                                                                                    As far as the $from variable goes, that should automatically come from your webhost if your using linux cPanel. It automatically comes from your cPanel username and ip address.



                                                                                                                                    $name = $_POST['name'];
                                                                                                                                    $email = $_POST['email'];
                                                                                                                                    $message = $_POST['message'];
                                                                                                                                    $from = 'From: yoursite.com';
                                                                                                                                    $to = 'contact@yoursite.com';
                                                                                                                                    $subject = 'Customer Inquiry';
                                                                                                                                    $body = "From: $namen E-Mail: $emailn Message:n $message";


                                                                                                                                    Also make sure you have the correct order of variables in your mail() function.
                                                                                                                                    the mail($to,$subject,$message,etc.) in that order, or else there is a chance of it not working.
                                                                                                                                    Let me know if this helps...







                                                                                                                                    share|improve this answer












                                                                                                                                    share|improve this answer



                                                                                                                                    share|improve this answer










                                                                                                                                    answered Jan 31 '16 at 14:15









                                                                                                                                    aidangig

                                                                                                                                    977




                                                                                                                                    977






















                                                                                                                                        up vote
                                                                                                                                        1
                                                                                                                                        down vote













                                                                                                                                        This will only affect a small handful of users, but I'd like it documented for that small handful. This member of that small handful spent 6 hours troubleshooting a working PHP mail script because of this issue.



                                                                                                                                        If you're going to a university that runs XAMPP from www.AceITLab.com, you should know what our professor didn't tell us: The AceITLab firewall (not the Windows firewall) blocks MercuryMail in XAMPP. You'll have to use an alternative mail client, pear is working for us. You'll have to send to a Gmail account with low security settings.



                                                                                                                                        Yes, I know, this is totally useless for real world email. However, from what I've seen, academic settings and the real world often have precious little in common.






                                                                                                                                        share|improve this answer























                                                                                                                                        • That's too oddly specific, and will indeed help noone else. Use the [delete] link below your answer please.
                                                                                                                                          – mario
                                                                                                                                          Nov 16 '17 at 17:21















                                                                                                                                        up vote
                                                                                                                                        1
                                                                                                                                        down vote













                                                                                                                                        This will only affect a small handful of users, but I'd like it documented for that small handful. This member of that small handful spent 6 hours troubleshooting a working PHP mail script because of this issue.



                                                                                                                                        If you're going to a university that runs XAMPP from www.AceITLab.com, you should know what our professor didn't tell us: The AceITLab firewall (not the Windows firewall) blocks MercuryMail in XAMPP. You'll have to use an alternative mail client, pear is working for us. You'll have to send to a Gmail account with low security settings.



                                                                                                                                        Yes, I know, this is totally useless for real world email. However, from what I've seen, academic settings and the real world often have precious little in common.






                                                                                                                                        share|improve this answer























                                                                                                                                        • That's too oddly specific, and will indeed help noone else. Use the [delete] link below your answer please.
                                                                                                                                          – mario
                                                                                                                                          Nov 16 '17 at 17:21













                                                                                                                                        up vote
                                                                                                                                        1
                                                                                                                                        down vote










                                                                                                                                        up vote
                                                                                                                                        1
                                                                                                                                        down vote









                                                                                                                                        This will only affect a small handful of users, but I'd like it documented for that small handful. This member of that small handful spent 6 hours troubleshooting a working PHP mail script because of this issue.



                                                                                                                                        If you're going to a university that runs XAMPP from www.AceITLab.com, you should know what our professor didn't tell us: The AceITLab firewall (not the Windows firewall) blocks MercuryMail in XAMPP. You'll have to use an alternative mail client, pear is working for us. You'll have to send to a Gmail account with low security settings.



                                                                                                                                        Yes, I know, this is totally useless for real world email. However, from what I've seen, academic settings and the real world often have precious little in common.






                                                                                                                                        share|improve this answer














                                                                                                                                        This will only affect a small handful of users, but I'd like it documented for that small handful. This member of that small handful spent 6 hours troubleshooting a working PHP mail script because of this issue.



                                                                                                                                        If you're going to a university that runs XAMPP from www.AceITLab.com, you should know what our professor didn't tell us: The AceITLab firewall (not the Windows firewall) blocks MercuryMail in XAMPP. You'll have to use an alternative mail client, pear is working for us. You'll have to send to a Gmail account with low security settings.



                                                                                                                                        Yes, I know, this is totally useless for real world email. However, from what I've seen, academic settings and the real world often have precious little in common.







                                                                                                                                        share|improve this answer














                                                                                                                                        share|improve this answer



                                                                                                                                        share|improve this answer








                                                                                                                                        edited Apr 5 '17 at 1:30









                                                                                                                                        Nathan Tuggy

                                                                                                                                        2,18992533




                                                                                                                                        2,18992533










                                                                                                                                        answered Apr 5 '17 at 1:09









                                                                                                                                        TonyLuigiC

                                                                                                                                        3219




                                                                                                                                        3219












                                                                                                                                        • That's too oddly specific, and will indeed help noone else. Use the [delete] link below your answer please.
                                                                                                                                          – mario
                                                                                                                                          Nov 16 '17 at 17:21


















                                                                                                                                        • That's too oddly specific, and will indeed help noone else. Use the [delete] link below your answer please.
                                                                                                                                          – mario
                                                                                                                                          Nov 16 '17 at 17:21
















                                                                                                                                        That's too oddly specific, and will indeed help noone else. Use the [delete] link below your answer please.
                                                                                                                                        – mario
                                                                                                                                        Nov 16 '17 at 17:21




                                                                                                                                        That's too oddly specific, and will indeed help noone else. Use the [delete] link below your answer please.
                                                                                                                                        – mario
                                                                                                                                        Nov 16 '17 at 17:21










                                                                                                                                        up vote
                                                                                                                                        1
                                                                                                                                        down vote













                                                                                                                                        You can use empty() and isset() functions. If you want to make it work with different files, just modify the action='yourphp.php' to the html I'm giving you, and store the PHP script to that yourphp.php file. Also you need to change your index.html into index.php to activate PHP functionality.



                                                                                                                                        PHP



                                                                                                                                        <?php

                                                                                                                                        error_reporting(0);
                                                                                                                                        $name = $_POST['name'];
                                                                                                                                        $email = $_POST['email'];
                                                                                                                                        $message = $_POST['message'];
                                                                                                                                        $from = 'From: yoursite.com';
                                                                                                                                        $to = 'contact@yoursite.com';
                                                                                                                                        $subject = 'Customer Inquiry';
                                                                                                                                        $body = "From: $namen E-Mail: $emailn Message:n $message";


                                                                                                                                        if ($_POST['submit']){
                                                                                                                                        if (!(empty($_POST['name']))) {
                                                                                                                                        if (!(empty($_POST['email']))){
                                                                                                                                        if (!(empty($_POST['message']))){
                                                                                                                                        mail ($to, $subject, $body, $from);
                                                                                                                                        echo '<p>Your message has been sent!</p>';
                                                                                                                                        }else{
                                                                                                                                        echo '<p>Fill your message please.</p>';}
                                                                                                                                        }else {
                                                                                                                                        echo '<p>Fill your email please.</p>';}
                                                                                                                                        }else{
                                                                                                                                        echo '<p>Fill your name please.</p>';}
                                                                                                                                        }else{
                                                                                                                                        echo '<p>Fill the form.</p>';}
                                                                                                                                        ?>


                                                                                                                                        HTML



                                                                                                                                        <html>
                                                                                                                                        <form method="post" action="?">
                                                                                                                                        <table>
                                                                                                                                        <tr><td>Name</td><td><input type='text' name='name' id='name'/></td></tr>
                                                                                                                                        <tr><td>Email</td><td><input type='text' name='email' id='email'/></td></tr>
                                                                                                                                        <tr><td>Message</td><td><input type='text' name='message' id='message'/></td></tr>
                                                                                                                                        <tr><td></td><td><input type='submit' name='submit' id='submit'/></td></tr>
                                                                                                                                        </table>
                                                                                                                                        </form>
                                                                                                                                        </html>


                                                                                                                                        Best Regards!






                                                                                                                                        share|improve this answer



























                                                                                                                                          up vote
                                                                                                                                          1
                                                                                                                                          down vote













                                                                                                                                          You can use empty() and isset() functions. If you want to make it work with different files, just modify the action='yourphp.php' to the html I'm giving you, and store the PHP script to that yourphp.php file. Also you need to change your index.html into index.php to activate PHP functionality.



                                                                                                                                          PHP



                                                                                                                                          <?php

                                                                                                                                          error_reporting(0);
                                                                                                                                          $name = $_POST['name'];
                                                                                                                                          $email = $_POST['email'];
                                                                                                                                          $message = $_POST['message'];
                                                                                                                                          $from = 'From: yoursite.com';
                                                                                                                                          $to = 'contact@yoursite.com';
                                                                                                                                          $subject = 'Customer Inquiry';
                                                                                                                                          $body = "From: $namen E-Mail: $emailn Message:n $message";


                                                                                                                                          if ($_POST['submit']){
                                                                                                                                          if (!(empty($_POST['name']))) {
                                                                                                                                          if (!(empty($_POST['email']))){
                                                                                                                                          if (!(empty($_POST['message']))){
                                                                                                                                          mail ($to, $subject, $body, $from);
                                                                                                                                          echo '<p>Your message has been sent!</p>';
                                                                                                                                          }else{
                                                                                                                                          echo '<p>Fill your message please.</p>';}
                                                                                                                                          }else {
                                                                                                                                          echo '<p>Fill your email please.</p>';}
                                                                                                                                          }else{
                                                                                                                                          echo '<p>Fill your name please.</p>';}
                                                                                                                                          }else{
                                                                                                                                          echo '<p>Fill the form.</p>';}
                                                                                                                                          ?>


                                                                                                                                          HTML



                                                                                                                                          <html>
                                                                                                                                          <form method="post" action="?">
                                                                                                                                          <table>
                                                                                                                                          <tr><td>Name</td><td><input type='text' name='name' id='name'/></td></tr>
                                                                                                                                          <tr><td>Email</td><td><input type='text' name='email' id='email'/></td></tr>
                                                                                                                                          <tr><td>Message</td><td><input type='text' name='message' id='message'/></td></tr>
                                                                                                                                          <tr><td></td><td><input type='submit' name='submit' id='submit'/></td></tr>
                                                                                                                                          </table>
                                                                                                                                          </form>
                                                                                                                                          </html>


                                                                                                                                          Best Regards!






                                                                                                                                          share|improve this answer

























                                                                                                                                            up vote
                                                                                                                                            1
                                                                                                                                            down vote










                                                                                                                                            up vote
                                                                                                                                            1
                                                                                                                                            down vote









                                                                                                                                            You can use empty() and isset() functions. If you want to make it work with different files, just modify the action='yourphp.php' to the html I'm giving you, and store the PHP script to that yourphp.php file. Also you need to change your index.html into index.php to activate PHP functionality.



                                                                                                                                            PHP



                                                                                                                                            <?php

                                                                                                                                            error_reporting(0);
                                                                                                                                            $name = $_POST['name'];
                                                                                                                                            $email = $_POST['email'];
                                                                                                                                            $message = $_POST['message'];
                                                                                                                                            $from = 'From: yoursite.com';
                                                                                                                                            $to = 'contact@yoursite.com';
                                                                                                                                            $subject = 'Customer Inquiry';
                                                                                                                                            $body = "From: $namen E-Mail: $emailn Message:n $message";


                                                                                                                                            if ($_POST['submit']){
                                                                                                                                            if (!(empty($_POST['name']))) {
                                                                                                                                            if (!(empty($_POST['email']))){
                                                                                                                                            if (!(empty($_POST['message']))){
                                                                                                                                            mail ($to, $subject, $body, $from);
                                                                                                                                            echo '<p>Your message has been sent!</p>';
                                                                                                                                            }else{
                                                                                                                                            echo '<p>Fill your message please.</p>';}
                                                                                                                                            }else {
                                                                                                                                            echo '<p>Fill your email please.</p>';}
                                                                                                                                            }else{
                                                                                                                                            echo '<p>Fill your name please.</p>';}
                                                                                                                                            }else{
                                                                                                                                            echo '<p>Fill the form.</p>';}
                                                                                                                                            ?>


                                                                                                                                            HTML



                                                                                                                                            <html>
                                                                                                                                            <form method="post" action="?">
                                                                                                                                            <table>
                                                                                                                                            <tr><td>Name</td><td><input type='text' name='name' id='name'/></td></tr>
                                                                                                                                            <tr><td>Email</td><td><input type='text' name='email' id='email'/></td></tr>
                                                                                                                                            <tr><td>Message</td><td><input type='text' name='message' id='message'/></td></tr>
                                                                                                                                            <tr><td></td><td><input type='submit' name='submit' id='submit'/></td></tr>
                                                                                                                                            </table>
                                                                                                                                            </form>
                                                                                                                                            </html>


                                                                                                                                            Best Regards!






                                                                                                                                            share|improve this answer














                                                                                                                                            You can use empty() and isset() functions. If you want to make it work with different files, just modify the action='yourphp.php' to the html I'm giving you, and store the PHP script to that yourphp.php file. Also you need to change your index.html into index.php to activate PHP functionality.



                                                                                                                                            PHP



                                                                                                                                            <?php

                                                                                                                                            error_reporting(0);
                                                                                                                                            $name = $_POST['name'];
                                                                                                                                            $email = $_POST['email'];
                                                                                                                                            $message = $_POST['message'];
                                                                                                                                            $from = 'From: yoursite.com';
                                                                                                                                            $to = 'contact@yoursite.com';
                                                                                                                                            $subject = 'Customer Inquiry';
                                                                                                                                            $body = "From: $namen E-Mail: $emailn Message:n $message";


                                                                                                                                            if ($_POST['submit']){
                                                                                                                                            if (!(empty($_POST['name']))) {
                                                                                                                                            if (!(empty($_POST['email']))){
                                                                                                                                            if (!(empty($_POST['message']))){
                                                                                                                                            mail ($to, $subject, $body, $from);
                                                                                                                                            echo '<p>Your message has been sent!</p>';
                                                                                                                                            }else{
                                                                                                                                            echo '<p>Fill your message please.</p>';}
                                                                                                                                            }else {
                                                                                                                                            echo '<p>Fill your email please.</p>';}
                                                                                                                                            }else{
                                                                                                                                            echo '<p>Fill your name please.</p>';}
                                                                                                                                            }else{
                                                                                                                                            echo '<p>Fill the form.</p>';}
                                                                                                                                            ?>


                                                                                                                                            HTML



                                                                                                                                            <html>
                                                                                                                                            <form method="post" action="?">
                                                                                                                                            <table>
                                                                                                                                            <tr><td>Name</td><td><input type='text' name='name' id='name'/></td></tr>
                                                                                                                                            <tr><td>Email</td><td><input type='text' name='email' id='email'/></td></tr>
                                                                                                                                            <tr><td>Message</td><td><input type='text' name='message' id='message'/></td></tr>
                                                                                                                                            <tr><td></td><td><input type='submit' name='submit' id='submit'/></td></tr>
                                                                                                                                            </table>
                                                                                                                                            </form>
                                                                                                                                            </html>


                                                                                                                                            Best Regards!







                                                                                                                                            share|improve this answer














                                                                                                                                            share|improve this answer



                                                                                                                                            share|improve this answer








                                                                                                                                            edited Jun 2 at 9:44

























                                                                                                                                            answered Jan 30 '16 at 13:25









                                                                                                                                            Bishop

                                                                                                                                            2,25811423




                                                                                                                                            2,25811423






















                                                                                                                                                up vote
                                                                                                                                                0
                                                                                                                                                down vote













                                                                                                                                                If you are running this code on a local server (i.e your computer for development purposes) it wont send the email to the recipient. What will happen is, it will create a .txt file in a folder named mailoutput.



                                                                                                                                                In the case if you are using a free hosing service like 000webhost or hostinger, those service providers disable the mail() function to prevent unintended uses of email spoofing, spamming etc. I prefer you to contact them to see whether they support this feature.



                                                                                                                                                If you are sure that the service provider supports the mail() function, you can check this PHP manual for further reference,
                                                                                                                                                PHP mail()



                                                                                                                                                To check weather your hosting service support the mail() function, try running this code, (Remember to change the recipient email address)



                                                                                                                                                <?php
                                                                                                                                                $to = 'nobody@example.com';
                                                                                                                                                $subject = 'the subject';
                                                                                                                                                $message = 'hello';
                                                                                                                                                $headers = 'From: webmaster@example.com' . "rn" .
                                                                                                                                                'Reply-To: webmaster@example.com' . "rn" .
                                                                                                                                                'X-Mailer: PHP/' . phpversion();

                                                                                                                                                mail($to, $subject, $message, $headers);
                                                                                                                                                ?>


                                                                                                                                                Hope this helped.






                                                                                                                                                share|improve this answer

























                                                                                                                                                  up vote
                                                                                                                                                  0
                                                                                                                                                  down vote













                                                                                                                                                  If you are running this code on a local server (i.e your computer for development purposes) it wont send the email to the recipient. What will happen is, it will create a .txt file in a folder named mailoutput.



                                                                                                                                                  In the case if you are using a free hosing service like 000webhost or hostinger, those service providers disable the mail() function to prevent unintended uses of email spoofing, spamming etc. I prefer you to contact them to see whether they support this feature.



                                                                                                                                                  If you are sure that the service provider supports the mail() function, you can check this PHP manual for further reference,
                                                                                                                                                  PHP mail()



                                                                                                                                                  To check weather your hosting service support the mail() function, try running this code, (Remember to change the recipient email address)



                                                                                                                                                  <?php
                                                                                                                                                  $to = 'nobody@example.com';
                                                                                                                                                  $subject = 'the subject';
                                                                                                                                                  $message = 'hello';
                                                                                                                                                  $headers = 'From: webmaster@example.com' . "rn" .
                                                                                                                                                  'Reply-To: webmaster@example.com' . "rn" .
                                                                                                                                                  'X-Mailer: PHP/' . phpversion();

                                                                                                                                                  mail($to, $subject, $message, $headers);
                                                                                                                                                  ?>


                                                                                                                                                  Hope this helped.






                                                                                                                                                  share|improve this answer























                                                                                                                                                    up vote
                                                                                                                                                    0
                                                                                                                                                    down vote










                                                                                                                                                    up vote
                                                                                                                                                    0
                                                                                                                                                    down vote









                                                                                                                                                    If you are running this code on a local server (i.e your computer for development purposes) it wont send the email to the recipient. What will happen is, it will create a .txt file in a folder named mailoutput.



                                                                                                                                                    In the case if you are using a free hosing service like 000webhost or hostinger, those service providers disable the mail() function to prevent unintended uses of email spoofing, spamming etc. I prefer you to contact them to see whether they support this feature.



                                                                                                                                                    If you are sure that the service provider supports the mail() function, you can check this PHP manual for further reference,
                                                                                                                                                    PHP mail()



                                                                                                                                                    To check weather your hosting service support the mail() function, try running this code, (Remember to change the recipient email address)



                                                                                                                                                    <?php
                                                                                                                                                    $to = 'nobody@example.com';
                                                                                                                                                    $subject = 'the subject';
                                                                                                                                                    $message = 'hello';
                                                                                                                                                    $headers = 'From: webmaster@example.com' . "rn" .
                                                                                                                                                    'Reply-To: webmaster@example.com' . "rn" .
                                                                                                                                                    'X-Mailer: PHP/' . phpversion();

                                                                                                                                                    mail($to, $subject, $message, $headers);
                                                                                                                                                    ?>


                                                                                                                                                    Hope this helped.






                                                                                                                                                    share|improve this answer












                                                                                                                                                    If you are running this code on a local server (i.e your computer for development purposes) it wont send the email to the recipient. What will happen is, it will create a .txt file in a folder named mailoutput.



                                                                                                                                                    In the case if you are using a free hosing service like 000webhost or hostinger, those service providers disable the mail() function to prevent unintended uses of email spoofing, spamming etc. I prefer you to contact them to see whether they support this feature.



                                                                                                                                                    If you are sure that the service provider supports the mail() function, you can check this PHP manual for further reference,
                                                                                                                                                    PHP mail()



                                                                                                                                                    To check weather your hosting service support the mail() function, try running this code, (Remember to change the recipient email address)



                                                                                                                                                    <?php
                                                                                                                                                    $to = 'nobody@example.com';
                                                                                                                                                    $subject = 'the subject';
                                                                                                                                                    $message = 'hello';
                                                                                                                                                    $headers = 'From: webmaster@example.com' . "rn" .
                                                                                                                                                    'Reply-To: webmaster@example.com' . "rn" .
                                                                                                                                                    'X-Mailer: PHP/' . phpversion();

                                                                                                                                                    mail($to, $subject, $message, $headers);
                                                                                                                                                    ?>


                                                                                                                                                    Hope this helped.







                                                                                                                                                    share|improve this answer












                                                                                                                                                    share|improve this answer



                                                                                                                                                    share|improve this answer










                                                                                                                                                    answered Apr 1 '17 at 3:30









                                                                                                                                                    Ronald P Mathews

                                                                                                                                                    1,051721




                                                                                                                                                    1,051721






















                                                                                                                                                        up vote
                                                                                                                                                        0
                                                                                                                                                        down vote













                                                                                                                                                        You can use libmail: http://lwest.free.fr/doc/php/lib/index.php3?page=mail&lang=en



                                                                                                                                                        include "libmail.php";
                                                                                                                                                        $m = new Mail(); // create the mail
                                                                                                                                                        $m->From( $_POST['form'] );
                                                                                                                                                        $m->To( $_POST['to'] );
                                                                                                                                                        $m->Subject( $_POST['subject'] );
                                                                                                                                                        $m->Body( $_POST['body'] );
                                                                                                                                                        $m->Cc( $_POST['cc']);
                                                                                                                                                        $m->Priority(4);
                                                                                                                                                        // attach a file of type image/gif to be displayed in the message if possible
                                                                                                                                                        $m->Attach( "/home/leo/toto.gif", "image/gif", "inline" );
                                                                                                                                                        $m->Send(); // send the mail
                                                                                                                                                        echo "Mail was sent:"
                                                                                                                                                        echo $m->Get(); // show the mail source





                                                                                                                                                        share|improve this answer

















                                                                                                                                                        • 4




                                                                                                                                                          Important to mention: this is merely a mail() function wrapper. Only adds message construction and some gimmicks.
                                                                                                                                                          – mario
                                                                                                                                                          Nov 16 '17 at 17:18















                                                                                                                                                        up vote
                                                                                                                                                        0
                                                                                                                                                        down vote













                                                                                                                                                        You can use libmail: http://lwest.free.fr/doc/php/lib/index.php3?page=mail&lang=en



                                                                                                                                                        include "libmail.php";
                                                                                                                                                        $m = new Mail(); // create the mail
                                                                                                                                                        $m->From( $_POST['form'] );
                                                                                                                                                        $m->To( $_POST['to'] );
                                                                                                                                                        $m->Subject( $_POST['subject'] );
                                                                                                                                                        $m->Body( $_POST['body'] );
                                                                                                                                                        $m->Cc( $_POST['cc']);
                                                                                                                                                        $m->Priority(4);
                                                                                                                                                        // attach a file of type image/gif to be displayed in the message if possible
                                                                                                                                                        $m->Attach( "/home/leo/toto.gif", "image/gif", "inline" );
                                                                                                                                                        $m->Send(); // send the mail
                                                                                                                                                        echo "Mail was sent:"
                                                                                                                                                        echo $m->Get(); // show the mail source





                                                                                                                                                        share|improve this answer

















                                                                                                                                                        • 4




                                                                                                                                                          Important to mention: this is merely a mail() function wrapper. Only adds message construction and some gimmicks.
                                                                                                                                                          – mario
                                                                                                                                                          Nov 16 '17 at 17:18













                                                                                                                                                        up vote
                                                                                                                                                        0
                                                                                                                                                        down vote










                                                                                                                                                        up vote
                                                                                                                                                        0
                                                                                                                                                        down vote









                                                                                                                                                        You can use libmail: http://lwest.free.fr/doc/php/lib/index.php3?page=mail&lang=en



                                                                                                                                                        include "libmail.php";
                                                                                                                                                        $m = new Mail(); // create the mail
                                                                                                                                                        $m->From( $_POST['form'] );
                                                                                                                                                        $m->To( $_POST['to'] );
                                                                                                                                                        $m->Subject( $_POST['subject'] );
                                                                                                                                                        $m->Body( $_POST['body'] );
                                                                                                                                                        $m->Cc( $_POST['cc']);
                                                                                                                                                        $m->Priority(4);
                                                                                                                                                        // attach a file of type image/gif to be displayed in the message if possible
                                                                                                                                                        $m->Attach( "/home/leo/toto.gif", "image/gif", "inline" );
                                                                                                                                                        $m->Send(); // send the mail
                                                                                                                                                        echo "Mail was sent:"
                                                                                                                                                        echo $m->Get(); // show the mail source





                                                                                                                                                        share|improve this answer












                                                                                                                                                        You can use libmail: http://lwest.free.fr/doc/php/lib/index.php3?page=mail&lang=en



                                                                                                                                                        include "libmail.php";
                                                                                                                                                        $m = new Mail(); // create the mail
                                                                                                                                                        $m->From( $_POST['form'] );
                                                                                                                                                        $m->To( $_POST['to'] );
                                                                                                                                                        $m->Subject( $_POST['subject'] );
                                                                                                                                                        $m->Body( $_POST['body'] );
                                                                                                                                                        $m->Cc( $_POST['cc']);
                                                                                                                                                        $m->Priority(4);
                                                                                                                                                        // attach a file of type image/gif to be displayed in the message if possible
                                                                                                                                                        $m->Attach( "/home/leo/toto.gif", "image/gif", "inline" );
                                                                                                                                                        $m->Send(); // send the mail
                                                                                                                                                        echo "Mail was sent:"
                                                                                                                                                        echo $m->Get(); // show the mail source






                                                                                                                                                        share|improve this answer












                                                                                                                                                        share|improve this answer



                                                                                                                                                        share|improve this answer










                                                                                                                                                        answered Oct 11 '17 at 5:21









                                                                                                                                                        Salim Ibrogimov

                                                                                                                                                        8832827




                                                                                                                                                        8832827








                                                                                                                                                        • 4




                                                                                                                                                          Important to mention: this is merely a mail() function wrapper. Only adds message construction and some gimmicks.
                                                                                                                                                          – mario
                                                                                                                                                          Nov 16 '17 at 17:18














                                                                                                                                                        • 4




                                                                                                                                                          Important to mention: this is merely a mail() function wrapper. Only adds message construction and some gimmicks.
                                                                                                                                                          – mario
                                                                                                                                                          Nov 16 '17 at 17:18








                                                                                                                                                        4




                                                                                                                                                        4




                                                                                                                                                        Important to mention: this is merely a mail() function wrapper. Only adds message construction and some gimmicks.
                                                                                                                                                        – mario
                                                                                                                                                        Nov 16 '17 at 17:18




                                                                                                                                                        Important to mention: this is merely a mail() function wrapper. Only adds message construction and some gimmicks.
                                                                                                                                                        – mario
                                                                                                                                                        Nov 16 '17 at 17:18





                                                                                                                                                        protected by Samuel Liew Oct 5 '15 at 8:58



                                                                                                                                                        Thank you for your interest in this question.
                                                                                                                                                        Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).



                                                                                                                                                        Would you like to answer one of these unanswered questions instead?



                                                                                                                                                        Popular posts from this blog

                                                                                                                                                        Bressuire

                                                                                                                                                        Vorschmack

                                                                                                                                                        Quarantine