I have a website and PHP web email form hosted on Godaddy. Everything is working fine. I have already added my proper custom domain email into the php file. But each time I receive an email via the web form, the From address is
accountname@p3plcpnl0913.prod.phx3.secureserver.net
Is there a way to change it? I would like to make some customisation.
Is there a way to customise it in a way that my From email address is the same as what I added into my php? I have tried number of methods from this example. https://www.php.net/manual/en/function.mail.php But I did not have any luck.
Can I set the From address dynamically same as the Email that will be entered into the form? How can I achieve this?
Currently I have set the Subject to be static. I would like to tweak the subject to be a little dynamic. Example - "Sample Contact Form - Entered Email - Current Date"
Here is my PHP file.
<?php
function validate($type, $data = ''){
$valid = true;
if(empty($data)){
$valid = false;
}
switch($type){
case 'email':
$valid = filter_var($data, FILTER_VALIDATE_EMAIL);
break;
case 'telephone':
$valid = (strlen($data) >= 10);
break;
case 'captcha':
$valid = ($data == 27);
break;
}
return $valid;
}
if($_SERVER['REQUEST_METHOD'] == 'POST' && ($_GET['nonce'] + 22 == 9022)){
$errors = array();
$sendTo = array(
'info@mydomain.com',
);
$subject = 'Sample Contact Form';
$firstname = strip_tags($_POST['firstname']);
$secondname = strip_tags($_POST['secondname']);
$email = strip_tags($_POST['email']);
$captcha = strip_tags($_POST['captcha']);
$message = strip_tags($_POST['message']);
if(!validate('text', $firstname)){
$errors['firstname'] = 'First name is not entered correct';
}
if(!validate('text', $secondname)){
$errors['secondname'] = 'Last name is not entered correct';
}
if(!validate('email', $email)){
$errors['email'] = 'Email address is not entered correct';
}
if(!validate('captcha', $captcha)){
$errors['captcha'] = 'Captcha is not entered correct';
}
if(!validate('text', $message)){
$errors['message'] = 'Message is not entered correct';
}
if(!empty($errors)){
http_response_code(400);
echo json_encode($errors);
exit;
}
$email_content = "First Name: $firstname\n";
$email_content .= "Last Name: $secondname\n";
$email_content .= "Email: $email\n";
$email_content .= "Message: $message\n";
if (mail($sendTo[0], $subject, $email_content)) {
http_response_code(200);
echo "Message Received";
} else {
$errors[] = 'Unable to send request.';
http_response_code(500);
echo json_encode($errors);
}
}else{
http_response_code(403);
echo "Error";
}
Would really appreciate if any PHP experts provide a helping hand. Thank you!
Edit 1
But I have checked my Godaddy Settings on cPanel. They seems to be fine. I feel it has to do with my PHP. I am also using Google Workspace for my email.
After attempting several time. I some what manage to change the From address base on the code below. The result I got was,
sendermail@senderdomain via p3plcpnl0913.prod.phx3.secureserver.net
I followed this example. PHP mail function 'from' address
$from = $_POST['email'];
(mail($sendTo[0], $subject, $email_content, "From:" . $from))
Now I am facing a new problem. Gmail is marking the email as SPAM.
How can I solve this? Please advice.