pohrebacek Posted Friday at 06:37 AM Share Posted Friday at 06:37 AM (edited) Hi, im trying to make webpage where user type their email, subject and message in html form and then hit submit. After that I want to send that form to my email address. I'm using mailjet.com as SMTP server. When I hit submit on my page, I get response that email has been sent, but none emaill has arrived. Any ideas on how to make it work? Maybe some other scripts or ways to achieve it? Please help. my php script: <?php use PHPMailer\PHPMailer\PHPMailer; use PHPMailer\PHPMailer\SMTP; use PHPMailer\PHPMailer\Exception; require 'vendor/autoload.php'; if($_SERVER['REQUEST_METHOD'] == 'POST') { $mail = new PHPMailer(true); try { //Server settings $mail->SMTPDebug = SMTP::DEBUG_SERVER; //Enable verbose debug output $mail->isSMTP(); //Send using SMTP $mail->Host = 'in-v3.mailjet.com'; //Set the SMTP server to send through $mail->SMTPAuth = true; //Enable SMTP authentication $mail->AuthType = 'LOGIN'; $mail->Username = 'API KEY'; //SMTP username $mail->Password = 'SECRET KEY'; //SMTP password $mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS; //Enable implicit TLS encryption $mail->Port = 587; //TCP port to connect to; use 587 if you have set `SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS` //Recipients $mail->setFrom($_POST["email"]); $mail->addAddress('MY EMAIL ADDRESS'); //Add a recipient //$mail->addAddress('MY EMAIL ADDRESS'); //Name is optional //$mail->addReplyTo('info@example.com', 'Information'); //$mail->addCC('cc@example.com'); //$mail->addBCC('bcc@example.com'); //Attachments //$mail->addAttachment('/var/tmp/file.tar.gz'); //Add attachments //$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); //Optional name //Content $mail->isHTML(true); //Set email format to HTML $mail->Subject = $_POST["subject"]; $mail->Body = $_POST["msg"]; $mail->AltBody = $_POST["msg"]; $mail->send(); echo 'Message has been sent'; //header("Location: ./"); } catch (Exception $e) { echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}"; } } Edited yesterday at 01:49 PM by Psycho Formatted code content Quote Link to comment Share on other sites More sharing options...
gw1500se Posted Friday at 03:19 PM Share Posted Friday at 03:19 PM You need to use the MailJet API. 1 Quote Link to comment Share on other sites More sharing options...
jodunno Posted Friday at 03:52 PM Share Posted Friday at 03:52 PM 9 hours ago, pohrebacek said: <?php use PHPMailer\PHPMailer\PHPMailer; use PHPMailer\PHPMailer\SMTP; use PHPMailer\PHPMailer\Exception; require 'vendor/autoload.php'; but you are using PHP Mailer. Interesting predicament. <?php require 'vendor/autoload.php'; use \Mailjet\Resources; ... i tried a Google search to find a good example: https://github.com/mailjet/mailjet-apiv3-php?tab=readme-ov-file#make-your-first-call Quote Link to comment Share on other sites More sharing options...
pohrebacek Posted Monday at 03:35 PM Author Share Posted Monday at 03:35 PM On 9/20/2024 at 5:19 PM, gw1500se said: You need to use the MailJet API. when i use MailJet API I get this error: Failed to send email. Error: Unauthorized array(0) { } my code: <?php /* This call sends a message to one recipient. */ require 'vendor/autoload.php'; use \Mailjet\Resources; $mj = new \Mailjet\Client(getenv('MY_APIKEY'), getenv('MY_SECRETKEY '),true,['version' => 'v3.1']); if($_SERVER['REQUEST_METHOD'] == 'POST') { $body = [ 'Messages' => [ [ 'From' => [ 'Email' => $_POST["email"], 'Name' => "Mailjet Pilot" ], 'To' => [ [ 'Email' => "MY_EMAIL", 'Name' => "passenger 1" ] ], 'Subject' => $_POST["subject"], 'TextPart' => $_POST["msg"], 'HTMLPart' => $_POST["msg"] ] ] ]; $response = $mj->post(Resources::$Email, ['body' => $body]); if ($response->success()) { $data = $response->getData(); echo "Email was sent successfully.\n"; var_dump($data); } else { echo "Failed to send email. Error: " . $response->getReasonPhrase() . "\n"; var_dump($response->getData()); } } ?> Quote Link to comment Share on other sites More sharing options...
gw1500se Posted Monday at 03:59 PM Share Posted Monday at 03:59 PM Please use the code icon (<>) and select PHP for your code. It formats the code to make it easier to read. Quote Link to comment Share on other sites More sharing options...
pohrebacek Posted Monday at 04:19 PM Author Share Posted Monday at 04:19 PM 19 minutes ago, gw1500se said: Please use the code icon (<>) and select PHP for your code. It formats the code to make it easier to read. Oh my bad, I'm new here <?php /* This call sends a message to one recipient. */ require 'vendor/autoload.php'; use \Mailjet\Resources; $mj = new \Mailjet\Client(getenv('MY_APIKEY'), getenv('MY_SECRETKEY '),true,['version' => 'v3.1']); if($_SERVER['REQUEST_METHOD'] == 'POST') { $body = [ 'Messages' => [ [ 'From' => [ 'Email' => $_POST["email"], 'Name' => "Mailjet Pilot" ], 'To' => [ [ 'Email' => "MY_EMAIL", 'Name' => "passenger 1" ] ], 'Subject' => $_POST["subject"], 'TextPart' => $_POST["msg"], 'HTMLPart' => $_POST["msg"] ] ] ]; $response = $mj->post(Resources::$Email, ['body' => $body]); if ($response->success()) { $data = $response->getData(); echo "Email was sent successfully.\n"; var_dump($data); } else { echo "Failed to send email. Error: " . $response->getReasonPhrase() . "\n"; var_dump($response->getData()); } } ?> Quote Link to comment Share on other sites More sharing options...
jodunno Posted Monday at 05:21 PM Share Posted Monday at 05:21 PM 1. unauthorized error = You have specified an incorrect Api Key / API Secret Key. 2. To use the Mailjet Email API, you need to: Create a Mailjet account, then retrieve your API and Secret keys. They will be used for authentication purposes. have you created an account? just trying to clarify. if you have valid api credentials, then Initialize your Mailjet Client:https://github.com/mailjet/mailjet-apiv3-php?tab=readme-ov-file#authentication Quote Link to comment Share on other sites More sharing options...
pohrebacek Posted Monday at 05:56 PM Author Share Posted Monday at 05:56 PM 24 minutes ago, jodunno said: 1. unauthorized error = You have specified an incorrect Api Key / API Secret Key. 2. To use the Mailjet Email API, you need to: Create a Mailjet account, then retrieve your API and Secret keys. They will be used for authentication purposes. have you created an account? just trying to clarify. if you have valid api credentials, then Initialize your Mailjet Client:https://github.com/mailjet/mailjet-apiv3-php?tab=readme-ov-file#authentication Yes I have mailjet account and I'm using my real API keys in my code. I tried to Initialize my Mailjet Client like you said, but now I'm getting different error, Failed to send email. Error: At least FromEmail or Sender must be provided array(0) { } But althrough i can see my from email in array when i echo it, I still get this error. <?php /* This call sends a message to one recipient. */ require 'vendor/autoload.php'; use \Mailjet\Resources; $apikey = 'MY_APIKEY'; $apisecret = 'MY_APISECRETKEY'; $mj = new \Mailjet\Client($apikey, $apisecret); if($_SERVER['REQUEST_METHOD'] == 'POST') { $body = [ 'Messages' => [ [ 'From' => [ 'Email' => $_POST["email"], 'Name' => "Mailjet Pilot" ], 'To' => [ [ 'Email' => "MY_EMAIL", 'Name' => "passenger 1" ] ], 'Subject' => $_POST["subject"], 'TextPart' => $_POST["msg"], 'HTMLPart' => $_POST["msg"] ] ] ]; $response = $mj->post(Resources::$Email, ['body' => $body]); echo print_r($body); if ($response->success()) { $data = $response->getData(); echo "Email was sent successfully.\n"; // Zobraz nebo zpracuj data podle potřeby var_dump($data); } else { echo "Failed to send email. Error: " . $response->getReasonPhrase() . "\n"; // Zobraz nebo zpracuj chybové informace var_dump($response->getData()); } } ?> Quote Link to comment Share on other sites More sharing options...
jodunno Posted yesterday at 03:36 AM Share Posted yesterday at 03:36 AM I do not use Mailjet but i have discovered that other users have had a similar problem with the api. try specifying the api $mj = new \Mailjet\Client($apikey, $apisecret, true,['version' => 'v3.1']); otherwise, i have to run a setup myself to see if i can make it work. Quote Link to comment Share on other sites More sharing options...
jodunno Posted yesterday at 07:30 AM Share Posted yesterday at 07:30 AM 13 hours ago, pohrebacek said: 'From' => [ 'Email' => $_POST["email"], 'Name' => "Mailjet Pilot" ], 'To' => [ [ 'Email' => "MY_EMAIL", 'Name' => "passenger 1" ] i do not use Mailjet but i read an article and the author suggests that your email is to be placed in the From array and the post email in the To array. It doesn't make sense to me but the author of the article uses this method in his form submission code. Quote Link to comment Share on other sites More sharing options...
maxxd Posted yesterday at 12:06 PM Share Posted yesterday at 12:06 PM I don't see where you've posted your form code in the thread - just to confirm that you've got all the data you think you have, put the following right after the `if($_SERVER['REQUEST_METHOD'] == 'POST') {` line: die('<pre>'.var_export($_POST, true).'</pre>'); Then check to make sure there's keys for email, subject, and msg in the $_POST array. Quote Link to comment Share on other sites More sharing options...
pohrebacek Posted 4 hours ago Author Share Posted 4 hours ago On 9/24/2024 at 5:36 AM, jodunno said: I do not use Mailjet but i have discovered that other users have had a similar problem with the api. try specifying the api $mj = new \Mailjet\Client($apikey, $apisecret, true,['version' => 'v3.1']); otherwise, i have to run a setup myself to see if i can make it work. Thanks, there aren't any errors anymore, but I still can't see any email that was sent thanks to my script. I added this line and switched "from" and "to" email but it still doesn't work. Quote Link to comment Share on other sites More sharing options...
pohrebacek Posted 4 hours ago Author Share Posted 4 hours ago On 9/24/2024 at 2:06 PM, maxxd said: I don't see where you've posted your form code in the thread - just to confirm that you've got all the data you think you have, put the following right after the `if($_SERVER['REQUEST_METHOD'] == 'POST') {` line: die('<pre>'.var_export($_POST, true).'</pre>'); Then check to make sure there's keys for email, subject, and msg in the $_POST array. Everything is fine according to this. Quote Link to comment 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.