Copy link to clipboard
Copied
Hello,
This is my final hope for my PHP form, I really hope I can get some help here, I have not succeeded anywhere else. This is my issue: I have to mail addresses that are receiving mails sent through this website, one is a gmail address and the other one is on the server (testmail@1alarm.se). When I try to send mails, they arrive at my gmail address, but not on the server address. I checked with the provider, one.com, that informed me that something was wrong with my code, but since they don't support scripts that they not have made, they could not help me. So now I'm turning to this highly evaluated brain trust, can anybody here help me?
Address for website form: https://1alarm.se/kontakt.html
php code:
<?php
/*
THIS FILE USES PHPMAILER INSTEAD OF THE PHP MAIL() FUNCTION
*/
use PHPMailer\src\PHPMailer;
use PHPMailer\src\SMTP;
use PHPMailer\src\Exception;
require "PHPMailer/PHPMailerAutoload.php";
//require "PHPMailer/src/Exception.php";
//require "PHPMailer/src/PHPMailer.php";
//require "PHPMailer/src/SMTP.php";
// require ReCaptcha class
require("recaptcha-master/src/autoload.php");
/*
* CONFIGURE EVERYTHING HERE
*/
$mail = new PHPMailer(true);
// an email address that will be in the From field of the email.
$fromEmail = "testmail@1alarm.se";
$fromName = "Kontaktformulär";
// an email address that will receive the email with the output of the form
$sendToEmail = "testmail@1alarm.se";
$sendToName = "Kontaktformulär";
$sendToEmail = "testmail@1alarm.se";
$sendToName = "Kontaktformulär";
// subject of the email
$subject = "Nytt meddelande från hemsidan";
// smtp credentials and server
$smtpHost = "mailout.one.com";
$smtpUsername = "testmail@1alarm.se";
$smtpPassword = "whoopi123";
// form field names and their translations.
// array variable name => Text to appear in the email
$fields = array('name' => 'Namn', 'surname' => 'Efternamn', 'phone' => 'Telefon', 'email' => 'Email', 'message' => 'Meddelande', 'need' => 'Vad gäller ditt meddelande?');
// message that will be displayed when everything is OK 🙂
$okMessage = 'Ditt meddelande är skickat. Tack, vi återkommer snart till dig!';
// If something goes wrong, we will display this message.
$errorMessage = 'Något gick fel och ditt meddelande har inte skickats. Försök igen senare, tack.';
// ReCaptch Secret
$recaptchaSecret = '6LeYUr8ZAAAAAHN_E-2VwB39M9-thW0jI9MKn9wI';
/*
* LET'S DO THE SENDING
*/
// if you are not debugging and don't need error reporting, turn this off by error_reporting(0);
error_reporting(E_ALL & ~E_NOTICE);
try
{
if (!empty($_POST)) {
// validate the ReCaptcha, if something is wrong, we throw an Exception,
// i.e. code stops executing and goes to catch() block
if (!isset($_POST['g-recaptcha-response'])) {
throw new \Exception('ReCaptcha is not set.');
}
// do not forget to enter your secret key from https://www.google.com/recaptcha/admin
$recaptcha = new \ReCaptcha\ReCaptcha($recaptchaSecret, new \ReCaptcha\RequestMethod\CurlPost());
// we validate the ReCaptcha field together with the user's IP address
$response = $recaptcha->verify($_POST['g-recaptcha-response'], $_SERVER['REMOTE_ADDR']);
if (!$response->isSuccess()) {
throw new \Exception('ReCaptcha was not validated.');
}
try
{
if(count($_POST) == 0) throw new \Exception('Form is empty');
}}
$emailTextHtml = "<h1>Du har ett nytt meddelande från hemsidan</h1><hr>";
$emailTextHtml .= "<table>";
foreach ($_POST as $key => $value) {
// If the field exists in the $fields array, include it in the email
if (isset($fields[$key])) {
$emailTextHtml .= "<tr><th>$fields[$key]</th><td>$value</td></tr>";
}
}
$emailTextHtml .= "</table><hr>";
$emailTextHtml .= "<p>Ha en bra dag!</p>";
$mail = new PHPMailer;
$mail->setFrom($fromEmail, $fromName);
$mail->addAddress($sendToEmail, $sendToName); // you can add more addresses by simply adding another line with $mail->addAddress();
$mail->addReplyTo($from);
$mail->isHTML(true);
$mail->Subject = $subject;
$mail->Body = $emailTextHtml;
$mail->msgHTML($emailTextHtml); // this will also create a plain-text version of the HTML email, very handy
$mail->isSMTP();
//Enable SMTP debugging
// 0 = off (for production use)
// 1 = client messages
// 2 = client and server messages
$mail->SMTPDebug = 0;
$mail->Debugoutput = 'html';
//Set the hostname of the mail server
// use
// $mail->Host = gethostbyname('smtp.gmail.com');
// if your network does not support SMTP over IPv6
$mail->Host = gethostbyname;
//Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
$mail->Port = 587;
//Set the encryption system to use - ssl (deprecated) or tls
$mail->SMTPSecure = "STARTTLS";
//Whether to use SMTP authentication
$mail->SMTPAuth = true;
//Username to use for SMTP authentication - use full email address for gmail
$mail->testmail = $smtpUsername;
//Password to use for SMTP authentication
$mail->whoopi123 = $smtpPassword;
if (!$mail->send()) {
throw new \Exception('I could not send the email.' . $mail->ErrorInfo);
}
$responseArray = array('type' => 'success', 'message' => $okMessage);
} catch (\Exception $e) {
// $responseArray = array('type' => 'danger', 'message' => $errorMessage);
$responseArray = array('type' => 'danger', 'message' => $e->getMessage());
}
// if requested by AJAX request return JSON response
if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
$encoded = json_encode($responseArray);
header('Content-Type: application/json');
echo $encoded;
}
// else just display the message
else {
echo $responseArray['message'];
}
Copy link to clipboard
Copied
"When I try to send mails, they arrive at my gmail address, but not on the server address."
In that case, the script IS working. If the script were NOT working, you wouldn't receive mail at ANY address.
Copy link to clipboard
Copied
Exactly, it IS working, but it's not sending mail to all my intended recipients. And I don't understand why the emails are not reaching the mailaddresses on the other server?
Copy link to clipboard
Copied
That all looks a bit freaking complex to me. Normally when using PHPMailer you can just add multiple email addresses like below:
$mail->addAddress('someOne@thisAddress.com');
$mail->addAddress('someOneElse@thatAddress.com');
Have you tried that approach?
So have a go at changing your existing code block to something like below and see what happens:
$mail->setFrom($fromEmail, $fromName);
$mail->addAddress('email_1@addressOne.com'); // you can add more addresses by simply adding another line with $mail->addAddress('email_2@addressTwo.com);
$mail->addReplyTo($from);
Copy link to clipboard
Copied
This is a classic case of WHERe the 'post editing' is a must.
There should be an ' at the end of the second $mail->address, as below marked in red.
$mail->addAddress('email_2@addressTwo.com');
Copy link to clipboard
Copied
Or you could assign the email addresses to an array:
$emailAddress = array('email_1@addressOne.com' , 'email_2@addressTwo.com')
Then loop through them using a foreach loop:
$mail->setFrom($fromEmail, $fromName);
foreach($emailAddress as $address) {
$mail->addAddress($address);
}
$mail->addReplyTo($from);
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more