<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Dusan Walla</title>
    <description>The latest articles on DEV Community by Dusan Walla (@webfluence_digital).</description>
    <link>https://dev.to/webfluence_digital</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2528149%2F0cea1040-5c70-4359-824f-bd77722bd8ac.png</url>
      <title>DEV Community: Dusan Walla</title>
      <link>https://dev.to/webfluence_digital</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/webfluence_digital"/>
    <language>en</language>
    <item>
      <title>Help Needed: Struggling to Set Up PHP Mailer for My Contact Form</title>
      <dc:creator>Dusan Walla</dc:creator>
      <pubDate>Thu, 05 Dec 2024 13:17:53 +0000</pubDate>
      <link>https://dev.to/webfluence_digital/help-needed-struggling-to-set-up-php-mailer-for-my-contact-form-3gpa</link>
      <guid>https://dev.to/webfluence_digital/help-needed-struggling-to-set-up-php-mailer-for-my-contact-form-3gpa</guid>
      <description>&lt;p&gt;Hey Dev Community! 👋&lt;/p&gt;

&lt;p&gt;I’m a digital marketing enthusiast and beginner in PHP, and I need some help with a problem on my website.&lt;/p&gt;

&lt;p&gt;My Website&lt;br&gt;
📍 &lt;a href="https://webfluence.digital/" rel="noopener noreferrer"&gt;WebFluence&lt;/a&gt;&lt;br&gt;
Specifically, the contact form here: &lt;a href="https://webfluence.digital/#pr__contact" rel="noopener noreferrer"&gt;Contact Form&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The Issue&lt;br&gt;
The contact form came with a prebuilt PHP file located in includes/sendmail.php. Unfortunately, the script uses the outdated mail() function, which Hostinger (my hosting provider) told me is insecure and not recommended. They advised me to use PHPMailer, which is preinstalled on their business hosting plans.&lt;/p&gt;

&lt;p&gt;I’ve read articles about setting up PHPMailer, but honestly, it’s all a bit overwhelming for me since I’ve never worked with PHP before. The template author hasn’t responded, and I’m stuck trying to figure this out.&lt;/p&gt;

&lt;p&gt;Details&lt;br&gt;
Domain: Registered with GoDaddy&lt;br&gt;
Hosting: Hostinger Business Plan&lt;br&gt;
Current Mail Script: The outdated mail() function script is here:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;?php

// Read the form values
$success = false;
$successTxt = "";
$senderName = isset( $_POST['name'] ) ? preg_replace( "/[^\.\-\' a-zA-Z0-9]/", "", $_POST['name'] ) : "";
$senderEmail = isset( $_POST['email'] ) ? preg_replace( "/[^\.\-\_\@a-zA-Z0-9]/", "", $_POST['email'] ) : "";
$subject = isset( $_POST['subject'] ) ? preg_replace( "/[^\.\-\' a-zA-Z0-9]/", "", $_POST['subject'] ) : "";
$budget = isset( $_POST['budget'] ) ? preg_replace( "/^[A-Za-z0-9\\-\\.]+$/", "", $_POST['budget'] ) : "";
$message = isset( $_POST['message'] ) ? preg_replace( "/(From:|To:|BCC:|CC:|Subject:|Content-Type:)/", "", $_POST['message'] ) : "";
$txt = "Client budget: " . $budget . "\n\n"  . $message . "\n\n" . "Regards,\n\n" . $senderName . " | " .$senderEmail;

// If all values exist, send the email
if ( $senderName &amp;amp;&amp;amp; $senderEmail &amp;amp;&amp;amp; $message ) {
  $mailTo = "dusan@webluence.digital"; // change it to your host mail for example (contact@yourdomain.com).
  $headers = "From: " . $senderEmail;
  $success = mail( $mailTo, $subject, $txt, $headers );
  $successTxt = "&amp;lt;p class='uk-alert uk-alert-success uk-margin-large-bottom success' data-uk-alert=''&amp;gt;Thanks for contacting us. We will contact you ASAP!&amp;lt;/p&amp;gt;";
  echo $successTxt;
}

?&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What I Need&lt;br&gt;
I want to replace this script with a secure PHPMailer-based script. Hostinger has preinstalled PHPMailer, and I’ve been told it’s more reliable. I found an article about setting it up, but it’s too confusing for me as someone new to PHP.&lt;/p&gt;

&lt;p&gt;This is what I have right now:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;?php
require 'vendor/autoload.php';

use PHPMailer\PHPMailer\PHPMailer;

$mail = new PHPMailer;

$mail-&amp;gt;isSMTP();
$mail-&amp;gt;SMTPDebug = 0;
$mail-&amp;gt;Host = 'smtp.hostinger.com';
$mail-&amp;gt;Port = 587;
$mail-&amp;gt;SMTPAuth = true;
$mail-&amp;gt;Username = 'sales@webfluence.digital';
$mail-&amp;gt;Password = 'c;Ge?H9unUs#:T0J';
$mail-&amp;gt;setFrom('sales@webfluence.digital', 'Dusan Walla');
$mail-&amp;gt;addReplyTo('sales@webfluence.digital', 'Dusan Walla');

// Read the form values and sanitize them to prevent injection attacks
$senderName = isset($_POST['name']) ? preg_replace("/[^\.\-\' a-zA-Z0-9]/", "", $_POST['name']) : ""; // Remove any characters that are not letters, numbers, spaces, dots, hyphens, or apostrophes
$senderEmail = isset($_POST['email']) ? preg_replace("/[^\.\-\_\@a-zA-Z0-9]/", "", $_POST['email']) : ""; // Remove any characters that are not letters, numbers, dots, hyphens, underscores, or @
$subject = isset($_POST['subject']) ? preg_replace("/[^\.\-\' a-zA-Z0-9]/", "", $_POST['subject']) : ""; // Remove any characters that are not letters, numbers, spaces, dots, hyphens, or apostrophes
$budget = isset($_POST['budget']) ? preg_replace("/[^\.\-\' a-zA-Z0-9]/", "", $_POST['budget']) : ""; // Remove any characters that are not letters, numbers, spaces, dots, hyphens, or apostrophes
$message = isset($_POST['message']) ? preg_replace("/(From:|To:|BCC:|CC:|Subject:|Content-Type:)/", "", $_POST['message']) : ""; // Remove any email headers to prevent header injection

$mail-&amp;gt;addAddress('sales@webfluence.digital', 'Dusan Walla');
$mail-&amp;gt;Subject = $subject;
$mail-&amp;gt;Body = &amp;lt;&amp;lt;&amp;lt;EOD
Client budget: $budget

$message

Regards,

$senderName | $senderEmail
EOD;

if (!$mail-&amp;gt;send()) {
    echo 'Mailer Error: ' . $mail-&amp;gt;ErrorInfo;
} else {
    echo "&amp;lt;p class='uk-alert uk-alert-success uk-margin-large-bottom success' data-uk-alert=''&amp;gt;Thanks for contacting us. We will contact you ASAP!&amp;lt;/p&amp;gt;";
}
}
?&amp;gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Questions&lt;br&gt;
How do I replace this script with PHPMailer in the simplest way possible?&lt;br&gt;
Is there a step-by-step guide that breaks down what I need to do?&lt;br&gt;
How do I configure Hostinger’s SMTP settings in the script?&lt;br&gt;
Any help would be deeply appreciated! 🙏&lt;/p&gt;

&lt;p&gt;Additional Resources&lt;br&gt;
Here’s the article I was referring to: &lt;a href="https://www.hostinger.com/tutorials/send-emails-using-php-mail#PHPMailer_vs_mail_Function_Pros_and_Cons" rel="noopener noreferrer"&gt;How to Use PHPMailer for Secure Email Sending&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Thank you in advance for your guidance! 😊&lt;/p&gt;

</description>
      <category>php</category>
      <category>development</category>
      <category>frontend</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
