PHPMailer and PHP code correct? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

PHPMailer and PHP code correct?

Good day. I've looked around at answers most of the day and nothing is answering my questions. everyone adds the link to the PHPMailer file in GitHub and i cant even make sense of that. Let me say this now, I DON'T KNOW ANYTHING ABOUT PHP... But for this project i need the PHP script to email form submissions to an email address. I know the PHP code works but I've added the PHPMailer script in and I'm not sure if it's the correct thing to do... The form is on an additional html file that has a link to the php file using the action attribute. I know the php script works because it works 100% on my website but my father's host requires something about smtp, and that's where i go blank. What i did on the script is everything that i, sort of, understood, if that's even correct. Please let me know if i am correct. Thank you. <?php use PHPMailer\PHPMailer\PHPMailer; use PHPMailer\PHPMailer\Exception; require 'path/to/PHPMailer/src/Exception.php'; require 'path/to/PHPMailer/src/PHPMailer.php'; require 'path/to/PHPMailer/src/SMTP.php'; $mail = new PHPMailer(true); // Passing `true` enables exceptions try { //Server settings $mail->SMTPDebug = 2; // Enable verbose debug output $mail->isSMTP(); // Set mailer to use SMTP $mail->Host = 'smtp1.example.com;smtp2.example.com'; // Specify main and backup SMTP servers $mail->SMTPAuth = true; // Enable SMTP authentication $mail->Username = 'username'; // SMTP username $mail->Password = '******'; // SMTP password $mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted $mail->Port = 25; // TCP port to connect to $webmaster_email = "sales@raincor.co.za"; $feedback_page = "feedback_form.html"; $error_page = "error_message.html"; $thankyou_page = "thank_you.html";

22nd Jan 2019, 7:58 PM
Byron Cross
10 Answers
+ 4
I will use my real (fake) hotmail account to send emails. I will set hotmail SMTP host and port and my real email address and password use PHPMailer\PHPMailer\PHPMailer; use PHPMailer\PHPMailer\Exception; $mail = new PHPMailer(true); // Passing `true` enables exceptions try { //Server settings $mail->SMTPDebug = 2; // Enable verbose debug output $mail->isSMTP(); // Set mailer to use SMTP $mail->Host = 'smtp.live.com'; // Specify main and backup SMTP servers $mail->SMTPAuth = true; // Enable SMTP authentication $mail->Username = 'toni.toni@hotmail.com'; // SMTP username $mail->Password = 'realpassword'; // SMTP password $mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted $mail->Port = 587; // TCP port to connect to //Recipients // This 'setFrom' email address has to be same as your username email $mail->setFrom('toni.toni@hotmail.com', 'Toni Isotalo'); $mail->addAddress('destination@email.com', 'Joe User'); //Content $mail->isHTML(true); // Set email format to HTML $mail->Subject = 'Here is the subject'; $mail->Body = 'This is the HTML message body <b>in bold!</b>'; $mail->AltBody = 'This is the body in plain text for non-HTML mail clients'; $mail->send(); echo 'Message has been sent'; } catch (Exception $e) { echo 'Message could not be sent. Mailer Error: ', $mail->ErrorInfo; }
25th Jan 2019, 2:48 PM
Toni Isotalo
Toni Isotalo - avatar
+ 2
The PHP part you'll have to find out yourself, but I can refer you to someone who can help you setup SMTP. Refer to the following: https://www.mailjet.com/feature/smtp-relay/ MailJet allows you to setup an SMTP server with ease, and provides you with all the necessary connection information.
22nd Jan 2019, 8:55 PM
Dread
Dread - avatar
+ 2
No problem.
22nd Jan 2019, 9:01 PM
Dread
Dread - avatar
+ 2
You must use your real email as username and real password. The SMTP host depends on what email you are using. For example for hotmail SMTP settings are host: smtp.live.com port: 587 for gmail host: smtp.gmail.com port: 587 You should first try to setup it with hotmail or gmail account just to know that it works and after that you can change it. What ever email you are using you need to find the SMTP host and port for it
25th Jan 2019, 2:43 PM
Toni Isotalo
Toni Isotalo - avatar
+ 1
Are you asking why it is asking for SMTP authentication information? You can setup an SMTP server on a multitude of websites that host it for free, so it definitely isn't a challenge to setup.
22nd Jan 2019, 8:07 PM
Dread
Dread - avatar
+ 1
thank you
22nd Jan 2019, 8:57 PM
Byron Cross
+ 1
What email you want the message to be sent from? What host and is it with cpanel? Your SMTP host is wrong. The email specified in "setFrom" method must be equal to your username property.
22nd Jan 2019, 9:29 PM
Toni Isotalo
Toni Isotalo - avatar
0
@Default the host said i need to configure the smtp so that the informationsubmitted in the form is sent to the email address. i got some code from PHPMailer on GitHub and added it to the original PHP code but im nit sure if the code is correct. or how to set up a SMTP...
22nd Jan 2019, 8:35 PM
Byron Cross
0
@Toni, I did't know we require an email address to send from. Could this be the receiving email address, so itll be to and from one email address? The Host is Vox, and they have their own sort of cpanel. What should the SMTP host be? At the moment it's just the example....... because i just copied that piece of code from PHPMailer. And where should i include the setfrom attribute?
23rd Jan 2019, 5:50 AM
Byron Cross
0
thanks everyone!! managed to get it right thanks to your answers!!
30th Jan 2019, 6:40 PM
Byron Cross