title: PHP Emails with PHP Mailer
body: <p>I'm creating a form that sends an email to the email that was entered into it. I have installed PHP mailer on my server.</p>
  
  <p>Currently when sending emails via PHP they send like this:
  <a href="https://i.stack.imgur.com/T0rA7.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/T0rA7.png" alt="Example of look."></a></p>
  
  <p>All emails sent are via the hosting server when I'd like them to look like this.</p>
  
  <p><a href="https://i.stack.imgur.com/qfgoQ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/qfgoQ.png" alt="Example of expected look"></a></p>
  
  <p>Just like any other email sent on the domain they should be mailed by <code>domain.com</code> rather than the server.</p>
  
  <p>I'm just testing this so using a simple form as a proof of concept.</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;form method="post" name="process.php" action="process.php"&gt;
  &lt;p&gt;Name:&lt;/p&gt;&lt;br&gt;&lt;input type="text" name="name"&gt;&lt;br&gt;&lt;br&gt;
  &lt;p&gt;Email Address:&lt;/p&gt;&lt;br&gt;&lt;input type="text" name="email"&gt;&lt;br&gt;&lt;br&gt;
  &lt;br&gt;
  &lt;input type="submit" value="Send Email"&gt;
  &lt;/form&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p>I'm then using this PHP to send the email:</p>
  
  <pre><code>&lt;?php
  $name = $_POST['name'];
  $email = $_POST['email'];
  
  use PHPMailer\PHPMailer\PHPMailer;
  require 'vendor/autoload.php';
  $mail = new PHPMailer;
  $mail-&gt;isSMTP();
  $mail-&gt;SMTPDebug = 2;
  $mail-&gt;Host = 'smtp.hostinger.com';
  $mail-&gt;Port = 587;
  $mail-&gt;SMTPAuth = true;
  $mail-&gt;Username = 'noreply@domain.com';
  $mail-&gt;Password = 'password';
  
  $email_from = "noreply@domain.com";
  $email_subject = "Test Email";
  
  $to = $email;
  $headers = "From: noreply@domain.com \r\n";
  $headers .= "Bcc: noreply@domain.com \r\n";
  $headers .= "Reply-To: me@domain.com \r\n";
  $headers .= "Content-Type: text/html; charset=UTF-8\r\n";
  
  
  $email_body = &lt;&lt;&lt;EOM
  &lt;p color="#000000"&gt;Hello, $name.&lt;br&gt;&lt;br&gt; This is a test email for mailing from the domain rather than the server.&lt;br&gt;&lt;br&gt; &lt;/p&gt;
  EOM
      ;
  
  mail($to, $email_subject, $email_body, $headers);
  
  ?&gt;
  </code></pre>
  
  <p>Basically, I want PHP emails mailed by my domain and I don't know how to do this so any help would be appreciated, my web host doesn't seem able to help me with this.</p>
  
  <p>Thanks in advance.</p>
  
  <p><code>UPDATE</code></p>
  
  <p>This code for the form.
  <div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;h1&gt;The email gets sent to a bookings address.&lt;/h1&gt;
  
  &lt;form method="post" name="process.php" action="process.php"&gt;
  &lt;p class= "whitetextsubsmall"&gt;Name:&lt;/p&gt;&lt;br&gt;&lt;input type="text" name="name"&gt;&lt;br&gt;&lt;br&gt;
  &lt;p class= "whitetextsubsmall"&gt;Email Address:&lt;/p&gt;&lt;br&gt;&lt;input type="text" name="email"&gt;&lt;br&gt;&lt;br&gt;
  &lt;br&gt;
  &lt;input type="submit" value="Send Email"&gt;
  &lt;/form&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p>And this code for <code>process.php</code></p>
  
  <pre><code>&lt;?php
  $name = $_POST['name'];
  $email = $_POST['email'];
  
  use PHPMailer\PHPMailer\PHPMailer;
  require 'vendor/autoload.php';
  $mail = new PHPMailer;
  $mail-&gt;isSMTP();
  $mail-&gt;SMTPDebug = 2;
  $mail-&gt;Host = 'smtp.hostinger.com';
  $mail-&gt;Port = 587;
  $mail-&gt;SMTPAuth = true;
  $mail-&gt;Username = 'noreply@domain.com'; /* This is the sender of the bookings. */
  $mail-&gt;Password = 'password';
  
  
  $mail-&gt;setFrom('noreply@domain.com');
  $mail-&gt;addAddress('bookings@domain.com', 'Company Bookings');
  $mail-&gt;addReplyTo($email, $name); /* Reply to the user who submitted the form from the bookings email. */
  $mail-&gt;Subject = 'Booking Request Test';
  
  $mail-&gt;isHTML(TRUE);
  $mail-&gt;Body = 'Message test &lt;br&gt; Booking Request from: $name &lt;br&gt;&lt;br&gt; Email: $email.';
  
  if(!$mail-&gt;send()) {
    echo 'Message was not sent.';
    echo 'Mailer error: ' . $mail-&gt;ErrorInfo;
  } else {
    echo 'Message has been sent.';
  }
  ?&gt;
  </code></pre>