Puncte:0

Error 500 on Digital Ocean while using SwiftMailer to send email

drapel vn

I am trying to send an email to myself from my seconday email address to my primary email address when a contact form is submitted to my website. I am using Swiftmailer to achieve this. The code works perfectly when I use it offline on my XAMPP server on my computer, but when I try to run it on my Digital Ocean Droplet running a LAMP stack, which is almost identical to my offline setup, it doesn't work and returns an error 500.

I looked into it and it turns out, SMTP is disabled to prevent spam and it can be lifted if I contacted support. After I contacted them, they told me they couldn't lift the restriction on port 25 but told me to try alternative ports and that SMTP doesn't work with Floating IPs. I removed my floating IP to help with this and I am currently trying to use port 578 with a Hotmail email address, but it still doesn't work.

What I have tried so far:

I used this command to open up the port 587 as suggested by Digital Ocean Support here.

sudo iptables -A INPUT -p tcp --dport 587 -m conntrack --ctstate NEW,ESTABLISHED -j ACCEPT
sudo iptables -A OUTPUT -p tcp --sport 587 -m conntrack --ctstate ESTABLISHED -j ACCEPT

After that, my server is able to connect to smtp.office365.com at port 587 as showed by the command below:

nc -vz smtp.office365.com 587
Connection to smtp.office365.com 587 port [tcp/submission] succeeded!

The command "ufw status" shows that port 587 is open on both ipv4 and ipv6:

To                         Action      From
--                         ------      ----
22/tcp                     LIMIT       Anywhere
80/tcp                     ALLOW       Anywhere
443/tcp                    ALLOW       Anywhere
3306/tcp                   ALLOW       Anywhere
587/tcp                    ALLOW       Anywhere
22/tcp (v6)                LIMIT       Anywhere (v6)
80/tcp (v6)                ALLOW       Anywhere (v6)
443/tcp (v6)               ALLOW       Anywhere (v6)
3306/tcp (v6)              ALLOW       Anywhere (v6)
587/tcp (v6)               ALLOW       Anywhere (v6)

But, when I try to run the contact form, it doesnt work in the end on the droplet, while it does work offline just fine and I received the emails as intended.

Is there something I am missing?

I will attach the code for my swiftmailer part below, just incase it helps identify any potential errors in the code that I seem to be missing.

include_once 'private/pass.inc.php';


//These fields usually get the data using a a form using POST method and PHP but left with placeholders here for the sake of simplicity.
$name = 'name';
$email = 'email';
$phone = 'phone';
$subject = 'subject';
$message = 'message';

// Send Email
// Create the Transport
$transport = (new Swift_SmtpTransport('smtp.office365.com', 587, 'tls'))
    ->setUsername($emailusername)
    ->setPassword($emailpassword);

// Create the Mailer using your created Transport
$mailer = new Swift_Mailer($transport);

function sendVerificationEmail($name, $email, $phone, $subject, $message)
{
    global $mailer;
    $sentfrom = "[email protected]";
    $sentto = "[email protected]";
    $body = 'Message From:' . $name . '<br>' . $email . '<br>' . $phone . '<br>' . $subject . '<br>' . $message;
    // Create a message
    $message = (new Swift_Message("A New Message From " . $name))
        ->setFrom([$sentfrom])
        ->setTo([$sentto])
        ->setBody($body, 'text/html');

    // Send the message
    $result = $mailer->send($message);
}

sendVerificationEmail($name, $email, $phone, $subject, $message);

echo "<script> window.location.assign('./contactformsuccess'); </script>";

EDIT: Added Logs

[Sun Aug 15 07:34:58.851255 2021] [php:warn] [pid 182629] [client 162.158.167.91:37848] PHP Warning: stream_socket_enable_crypto(): SSL operation failed with code 1. OpenSSL Error messages:\nerror:1416F086:SSL routines:tls_process_server_certificate:certificate verify failed in /var/www/site.com/html/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Transport/StreamBuffer.php on line 94 [Sun Aug 15 07:34:58.851487 2021] [php:error] [pid 182629] [client 162.158.167.91:37848] PHP Fatal error: Uncaught Swift_TransportException: Unable to connect with TLS encryption in /var/www/site.com/html/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Transport/EsmtpTransport.php:349\nStack trace:\n#0 /var/www/site.com/html/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Transport/AbstractSmtpTransport.php(148): Swift_Transport_EsmtpTransport->doHeloCommand()\n#1 /var/www/site.com/html/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Mailer.php(65): Swift_Transport_AbstractSmtpTransport->start()\n#2 /var/www/site.com/html/includes/cf2.inc.php(39): Swift_Mailer->send()\n#3 /var/www/site.com/html/includes/cf2.inc.php(42): sendVerificationEmail()\n#4 {main}\n thrown in /var/www/site.com/html/vendor/swiftmailer/swiftmailer/lib/classes/Swift/Transport/EsmtpTransport.php on line 349

Puncte:0
drapel vn

am rezolvat-o. Aveam aplicația mea de autentificare Microsoft conectată la contul secundar pe care încercam să-l folosesc și din cauza asta a eliminat erori. Nu aveam activată autentificarea cu 2 factori, dar încă mai provoca probleme, așa că am deconectat-o.

De asemenea, am actualizat transportul swiftmailer cu asta:

// Creați transportul
$transport = (nou Swift_SmtpTransport('smtp.live.com', 587, 'tls'))
 ->setUsername($emailusername)
 ->setPassword($emailpassword)
 ->setStreamOptions(array('ssl' => array('allow_self_signed' => true, 'verify_peer' => false, 'verify_peer_name' => false)));

Ambele combinate mi-au rezolvat problema. E-mailul merge așa cum a fost prevăzut acum.

Postează un răspuns

Majoritatea oamenilor nu înțeleg că a pune multe întrebări deblochează învățarea și îmbunătățește legătura interpersonală. În studiile lui Alison, de exemplu, deși oamenii își puteau aminti cu exactitate câte întrebări au fost puse în conversațiile lor, ei nu au intuit legătura dintre întrebări și apreciere. În patru studii, în care participanții au fost implicați în conversații ei înșiși sau au citit transcrieri ale conversațiilor altora, oamenii au avut tendința să nu realizeze că întrebarea ar influența – sau ar fi influențat – nivelul de prietenie dintre conversatori.