Chrisj Posted March 9, 2019 Share Posted March 9, 2019 The html Form I'm using works successfully with this php code: <?php //check if form was sent if($_POST){ $to = 's@hmail.com'; $subject = 'Form1'; $name = $_POST['name']; $email = $_POST['email']; $message = $_POST['message']; $headers = $name; $message .= "\r\n\r\n" . $name; if( empty($_POST["some_place"]) or $_POST['some_place'] != "glory" ) { header("HTTP/1.0 403 Forbidden"); }else{ mail( $to, $subject, $message, $email, $headers ); } header('Location: https://.......com'); exit; } ?> The problem is that when the email is received it shows the (from) email address to be my domain account user name @ the server name, like this:domain1@host3servername.com, where I’d prefer something more like noReply@actualdomain.com Any help or suggested remedy will be appreciated Quote Link to comment https://forums.phpfreaks.com/topic/308442-email-address-is-domain-account-user-name-the-server-name/ Share on other sites More sharing options...
gw1500se Posted March 9, 2019 Share Posted March 9, 2019 Because you are not setting the from parameter. You should be using PHPMailer which is simpler and gives you much better control. Quote Link to comment https://forums.phpfreaks.com/topic/308442-email-address-is-domain-account-user-name-the-server-name/#findComment-1565130 Share on other sites More sharing options...
Chrisj Posted March 9, 2019 Author Share Posted March 9, 2019 Thanks for your reply and suggestion. Would you be interested in providing a simple basic example of a php mailer code similar to what I have shown in my posting? Quote Link to comment https://forums.phpfreaks.com/topic/308442-email-address-is-domain-account-user-name-the-server-name/#findComment-1565133 Share on other sites More sharing options...
Chrisj Posted March 10, 2019 Author Share Posted March 10, 2019 (edited) Thanks for your reply. I have PHPMailer installed. Maybe you can help direct me to tie-in this example code to my Form and to the PHPMailer: <?php /** * PHPMailer simple file upload and send example. */ //Import the PHPMailer class into the global namespace use PHPMailer\PHPMailer\PHPMailer; $msg = ''; if (array_key_exists('userfile', $_FILES)) { // First handle the upload // Don't trust provided filename - same goes for MIME types // See http://php.net/manual/en/features.file-upload.php#114004 for more thorough upload validation $uploadfile = tempnam(sys_get_temp_dir(), hash('sha256', $_FILES['userfile']['name'])); if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) { // Upload handled successfully // Now create a message require '../vendor/autoload.php'; $mail = new PHPMailer; $mail->setFrom('from@example.com', 'First Last'); $mail->addAddress('whoto@example.com', 'John Doe'); $mail->Subject = 'PHPMailer file sender'; $mail->Body = 'My message body'; // Attach the uploaded file $mail->addAttachment($uploadfile, 'My uploaded file'); if (!$mail->send()) { $msg .= "Mailer Error: " . $mail->ErrorInfo; } else { $msg .= "Message sent!"; } } else { $msg .= 'Failed to move file to ' . $uploadfile; } } ?> And my Form looks like this: <form action='/submit/submit.php' method='post' name='myform'> <input type="hidden" id="some-place" name="some_place" value="classified"> <div class="row"> <div class="col-sm-14"> <textarea name='message' placeholder="Message..." class="form-control" rows="9" ></textarea> </div> </div> <div class="row"> <input class="form-control" type="text" name="name" placeholder="Name "> <input class="form-control" type="email" name="email" placeholder="Email" required> </div> <div class="row"> <input class="btnbtn-action" type='submit' value="Send" onclick="return UpdateTheHiddenField()" > <br/><br/> </div> </div> </div> </form> Any additional guidance is appreciated Edited March 10, 2019 by Chrisj Quote Link to comment https://forums.phpfreaks.com/topic/308442-email-address-is-domain-account-user-name-the-server-name/#findComment-1565137 Share on other sites More sharing options...
gw1500se Posted March 10, 2019 Share Posted March 10, 2019 (edited) It seems to be pretty straight forward. You replace your mail code with this and use the same $_POST values to set the respective values as shown in the example. The only difference is you set the from address to whatever you want. That being said your ISP may munge it as it passes through the mail server. Nothing you can do about that. Edited March 10, 2019 by gw1500se Quote Link to comment https://forums.phpfreaks.com/topic/308442-email-address-is-domain-account-user-name-the-server-name/#findComment-1565140 Share on other sites More sharing options...
Chrisj Posted March 10, 2019 Author Share Posted March 10, 2019 Thanks for your reply. It's not so straight forward to me. Here's what I tried, without success: <?php use PHPMailer\PHPMailer\PHPMailer; $msg = ''; if (array_key_exists('userfile', $_FILES)) { $uploadfile = tempnam(sys_get_temp_dir(), hash('sha256', $_FILES['userfile']['name'])); if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) { // Upload handled successfully // Now create a message require '../vendor/autoload.php'; $mail = new PHPMailer; $mail->setFrom('from@example.com', 'First Last'); $mail->addAddress('s@hmail.com', 'John Doe'); $mail->Subject = 'PHPMailer file sender'; $mail->message = 'My message body'; // Attach the uploaded file $mail->addAttachment($uploadfile, 'My uploaded file'); if (!$mail->send()) { $msg .= "Mailer Error: " . $mail->ErrorInfo; } else { $msg .= "Message sent!"; } } else { $msg .= 'Failed to move file to ' . $uploadfile; } header('Location: https://........com'); exit; } ?> any additional guidance will be appreciated Quote Link to comment https://forums.phpfreaks.com/topic/308442-email-address-is-domain-account-user-name-the-server-name/#findComment-1565141 Share on other sites More sharing options...
ginerjm Posted March 10, 2019 Share Posted March 10, 2019 So - You had no success. Care to tell us why/what/how? Did your send give you an error or not? Quote Link to comment https://forums.phpfreaks.com/topic/308442-email-address-is-domain-account-user-name-the-server-name/#findComment-1565142 Share on other sites More sharing options...
Chrisj Posted March 10, 2019 Author Share Posted March 10, 2019 Thanks for your reply. I'm now trying the code below, but am getting this error: Warning: require(../assets/import/PHPMailer/PHPMailerAutoLoad.php): failed to open stream: No such file or directory in /home/public_html/submit/submit.php on line 11Warning: require(../assets/import/PHPMailer/PHPMailerAutoLoad.php): failed to open stream: No such file or directory in /home/public_html/submit/submit.php on line 11Fatal error: require(): Failed opening required '../assets/import/PHPMailer/PHPMailerAutoLoad.php' (include_path='.:/opt/cpanel/ea-php70/root/usr/share/pear') in /home/public_html/submit/submit.php on line 11 <?php /** * This example shows how to handle a simple contact form. */ //Import PHPMailer classes into the global namespace use PHPMailer\PHPMailer\PHPMailer; $msg = ''; //Don't run this unless we're handling a form submission if (array_key_exists('email', $_POST)) { date_default_timezone_set('Etc/UTC'); require '../assets/import/PHPMailer/PHPMailerAutoLoad.php'; //Create a new PHPMailer instance $mail = new PHPMailer; //Tell PHPMailer to use SMTP - requires a local mail server //Faster and safer than using mail() $mail->isSMTP(); $mail->Host = 'localhost'; $mail->Port = 25; //Use a fixed address in your own domain as the from address //**DO NOT** use the submitter's address here as it will be forgery //and will cause your messages to fail SPF checks $mail->setFrom('chrisj@hmail.com', 'First Last'); //Send the message to yourself, or whoever should receive contact for submissions $mail->addAddress('chrisj@hmail.com', 'John Doe'); //Put the submitter's address in a reply-to header //This will fail if the address provided is invalid, //in which case we should ignore the whole request if ($mail->addReplyTo($_POST['email'], $_POST['name'])) { $mail->Subject = 'PHPMailer contact form'; //Keep it simple - don't use HTML $mail->isHTML(false); //Build a simple message body $mail->Body = <<<EOT Email: {$_POST['email']} Name: {$_POST['name']} Message: {$_POST['message']} EOT; //Send the message, check for errors if (!$mail->send()) { //The reason for failing to send will be in $mail->ErrorInfo //but you shouldn't display errors to users - process the error, log it on your server. $msg = 'Sorry, something went wrong. Please try again later.'; } else { $msg = 'Message sent! Thanks for contacting us.'; } } else { $msg = 'Invalid email address, message ignored.'; } } ?> Any additional guidance is appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/308442-email-address-is-domain-account-user-name-the-server-name/#findComment-1565143 Share on other sites More sharing options...
gw1500se Posted March 10, 2019 Share Posted March 10, 2019 The error message seems clear to me. The path you specified in the require statement is not correct. Quote Link to comment https://forums.phpfreaks.com/topic/308442-email-address-is-domain-account-user-name-the-server-name/#findComment-1565144 Share on other sites More sharing options...
Chrisj Posted March 10, 2019 Author Share Posted March 10, 2019 Yes, thanks, got it. Now I see this: Fatal error: Uncaught Error: Class 'PHPMailer\PHPMailer\PHPMailer' not found in /home/public_html/submit/submit.php:13 Stack trace: #0 {main} thrown in /home/public_html/submit/submit.php on line 13 any additional help is appreciated Quote Link to comment https://forums.phpfreaks.com/topic/308442-email-address-is-domain-account-user-name-the-server-name/#findComment-1565145 Share on other sites More sharing options...
gw1500se Posted March 10, 2019 Share Posted March 10, 2019 Use the correct directory. Quote Link to comment https://forums.phpfreaks.com/topic/308442-email-address-is-domain-account-user-name-the-server-name/#findComment-1565146 Share on other sites More sharing options...
ginerjm Posted March 10, 2019 Share Posted March 10, 2019 (edited) So what happened to using the info I posted on how to get the right paths? Sorry - wrong topic. Thought I showed you how to use a DOCUMENT_ROOT to base your relative paths off of. Edited March 10, 2019 by ginerjm Quote Link to comment https://forums.phpfreaks.com/topic/308442-email-address-is-domain-account-user-name-the-server-name/#findComment-1565147 Share on other sites More sharing options...
Chrisj Posted March 10, 2019 Author Share Posted March 10, 2019 Thanks for the replies. In tried this: use assets\import\PHPMailer; and I see this error: Fatal error: Uncaught Error: Class 'assets\import\PHPMailer' not found in /home/public_html/submit/submit.php:18 Stack trace: #0 {main} thrown in ... and I tried this: use assets/import/PHPMailer; and I see this error: Parse error: syntax error, unexpected '/', expecting ',' or ';' in any additional help will be welcomed. Quote Link to comment https://forums.phpfreaks.com/topic/308442-email-address-is-domain-account-user-name-the-server-name/#findComment-1565148 Share on other sites More sharing options...
gw1500se Posted March 11, 2019 Share Posted March 11, 2019 "use" ???????? Quote Link to comment https://forums.phpfreaks.com/topic/308442-email-address-is-domain-account-user-name-the-server-name/#findComment-1565162 Share on other sites More sharing options...
Barand Posted March 11, 2019 Share Posted March 11, 2019 36 minutes ago, gw1500se said: "use" ???????? It's a namespacey thing. Quote Link to comment https://forums.phpfreaks.com/topic/308442-email-address-is-domain-account-user-name-the-server-name/#findComment-1565164 Share on other sites More sharing options...
salathe Posted March 11, 2019 Share Posted March 11, 2019 On 3/10/2019 at 4:54 PM, Chrisj said: use PHPMailer\PHPMailer\PHPMailer; This is for PHPMailer 6. On 3/10/2019 at 4:54 PM, Chrisj said: require '../assets/import/PHPMailer/PHPMailerAutoLoad.php'; This is for PHPMailer 5. Which version do you actually have? Quote Link to comment https://forums.phpfreaks.com/topic/308442-email-address-is-domain-account-user-name-the-server-name/#findComment-1565165 Share on other sites More sharing options...
gw1500se Posted March 12, 2019 Share Posted March 12, 2019 I know it was a namespace statement but it had nothing to do with the original problem. It still looks like a path issue for the PHPMailer code. Quote Link to comment https://forums.phpfreaks.com/topic/308442-email-address-is-domain-account-user-name-the-server-name/#findComment-1565177 Share on other sites More sharing options...
gizmola Posted March 20, 2019 Share Posted March 20, 2019 On 3/12/2019 at 6:13 AM, gw1500se said: I know it was a namespace statement but it had nothing to do with the original problem. It still looks like a path issue for the PHPMailer code. You need to load the autoloader before you do anything else. It's not optional. The autoloader figures out where your component libraries are and resolves them for you. It should be the first thing you do in your script. Quote Link to comment https://forums.phpfreaks.com/topic/308442-email-address-is-domain-account-user-name-the-server-name/#findComment-1565384 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.