I'm trying to figure out my issue, I have a shared hosting provide that had the PHP mail function disable for security reasons.
I need to send some forms data using PHP, I only know how to send form data using the PHP mail function.
I tried using phpmailer, got ir from GitHub, but it didn't worked.
My question is very simple, is there a workaround to send form data without the PHP mail function to bypass the disabled phpmail and nobody=on tweak on my shared web hosting?
<?php
function send_mail($email, $recipient_name, $message='')
{
require("phpmailer/class.phpmailer.php");
require("phpmailer/class.smtp.php");
$mail = new PHPMailer();
$mail->CharSet="utf-8";
$mail->IsSMTP();
$mail->Host = 'localhost';
$mail->Port = 465;
$mail->SMTPAuth = true;
$mail->Username = "user@mysite.com";
$mail->Password = "password";
$mail->setFrom($email, $recipient_name);
$mail->AddAddress($email, $recipient_name);
$mail->WordWrap = 50;
$mail->IsHTML(false);
$mail->Subject = "Contact form";
$mail->Body = $message;
if(!$mail->Send())
{
echo "Message could not be sent. <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}
echo "Message has been sent";
}
//vars
$subject = "Contac form";
$from = $_POST['email'];
//data
$msg = "NAME: " .$_POST['name'] ."<br><br>
";
$msg .= "EMAIL: " .$_POST['email'] ."<br><br>
";
$msg .= "WEBSITE: " .$_POST['website'] ."<br><br>
";
$msg .= "MESSAGE: " .$_POST['message'] ."<br>
";
//Headers
$headers = "MIME-Version: 1.0
";
$headers .= "Content-type: text/html; charset=UTF-8
";
$mail="support@mysite.com";
//send mail
//mail($mail, $subject, $msg, $headers, "-f $from");
send_mail($from, $_POST['name'], $msg);
?>
In essence it all boils down to knowing the environment your shared hosting provider is giving you.
Since they disable php's mail() function, I presume they are trying to not have to deal with spammers using their services.
So they probably also will not run an SMTP service on localhost, and most likely will filter out outgoing traffic to port 25 to prevent you from delivering it on other mail servers as well. But again: they might not (yet).
But it's worth checking out what is available:
It's a bit of work and how to get the answers depend a lot on how minimal the service is that you get and what other resources you have around the internet. Anyway the easiest is to simply ask your supplier: they know what they allow, what they block and what they want to do next depending on where they are trying to get rid of spammers.
Anyway, your best bet is to use authenticated delivery towards a google server with a destination address on gmail or so. That's the least likely to get filtered away by either Google or a shared hosting provider who know what they do. Take care: authenticated delivery means the webserver has a way to authenticate towards google, so if the separation between different customers is suboptimal (it's by far not always foolproof), your "neighbours" in the shared hosting can get to those credentials with relative ease, so make sure it's not an account you care about a lot - hence: create a gmail address just for that use...
If they don't allow anything. You can still save the email on the server, retrieve it from a desktop or other server and send it from there with some programming.
First you have to include all requirements to send Mail with PHP mailer
require 'assets/PHPMailer/PHPMailer/src/Exception.php';
require 'assets/PHPMailer/PHPMailer/src/OAuth.php';
require 'assets/PHPMailer/PHPMailer/src/PHPMailer.php';
require 'assets/PHPMailer/PHPMailer/src/POP3.php';
require 'assets/PHPMailer/PHPMailer/src/SMTP.php';
require('assets/PHPMailer/simple_html_dom.php');
After that you define settings, here is config I use: (I use gmail to send mails, to use gmail you have to enable config in your gmail to use less secure apps)
$mailSub = filter_input('insert mail subject here');
$mailMsg = $html_new;
$mailto = 'inset mail you wish to send here';
$mail = new PHPMailer\PHPMailer\PHPMailer();
$mail->IsSmtp();
$mail->SMTPDebug = 0;
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'ssl';
$mail->Host = 'smtp.gmail.com';
$mail->Port = 465;
$mail->IsHTML(true);
$mail->CharSet = 'UTF-8';
// LOGIN INFO TO YOUR GMAIL
$mail->Username = 'gmail';
$mail->Password = 'password for gmail';
$mail->SetFrom('name you wish to show as sender');
//-------------------------------------------
$mail->Subject = 'subject';
$mail->Body = 'body of a mail, you can use html here';
$mail->AddAddress('reciever mail');
$mail->SMTPOptions = array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
);
$mail->Send()
Here is a link to my github repo with full app that utilize php mailer, maybe it would be of some help.