PHPMailer not able to send send email with ec2 - email

I'm using PHPmailer to send account verification mail, I'm using AWS ec2 instance, however, that mailer is working fine in localhost but when I upload that to server emails are not going,
at first, i used SendGrid credentials to send emails, failed, then tried Gmail SMTP, failed, and somewhere I read that ec2 can't send emails, then I created SES also, still can't able to send.
searched on the web abt that but no answers are fixing my problem,
in localhost, in can send emails with the same code and with SendGrid of Gmail credentials, why I can't send with the server?
my PHP mailer code is:
$sub = "Thankyou For registration! Confirm Your mail to Login";
$mailBody = "<h1>You are successfully registered<br />Visit site to login</h1>";
require 'mailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = "tls://email-smtp.us-east-1.amazonaws.com"; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = "smtp_username"; // SMTP username
$mail->Password = "smtp_password"; // SMTP password
// $mail->SMTPSecure = 'ssl'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465; // TCP port to connect to
$mail->setFrom("my_mail_id#gmail.com", "SMTP_REPLAY_NAME");
$mail->addReplyTo("my_mail_id#gmail.com", "SMTP_REPLAY_NAME");
$mail->addAddress("recipient_mail_id#gmail.com"); // Add a recipient
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $sub;
$mail->Body = $mailBody;
if(!$mail->send()) {
echo 'Message could not be sent.';
} else {
echo 'Message has been sent';
}
it shows Message has been sent but I cant receive emails, checked in spam folder also, no clue of mail!
even I have openSSL certificate also! opened SMTP port for both inbound and outbound in security group of ec2, everything working fine but PHPMailer!

Get your protocols straight. In the Host you're specifying tls, but telling it to connect to Port = 465, which will not work with TLS. Either change your Port to 587 (preferred) or change your encryption method to ssl. Enabling debug output (SMTPDebug = 2) will let you in on what's happening in the conversation with the server.
A perusal of the troubleshooting guide would probably help.

Related

Problem sending emails via PHPMailer due to DNS settings

This is my first post so please be gentle.
I've been trying to send automatic SMTP emails via PHPMailer using my webhost as the server. Below is the script I am using.
<?php
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use PHPMailer\PHPMailer\Exception;
require '../PHPMailer/src/Exception.php';
require '../PHPMailer/src/PHPMailer.php';
require '../PHPMailer/src/SMTP.php';
//Load Composer's autoloader
//require 'vendor/autoload.php';
//Create an instance; passing `true` enables exceptions
$mail = new PHPMailer(true);
try {
//Server settings
$mail->SMTPDebug = 4; //SMTP::DEBUG_SERVER;
$mail->isSMTP();
$mail->Mailer = "smtp";
$mail->Host = 'surrey.redbackinternet.net';
$mail->SMTPAuth = true;
$mail->Username = 'noreply#mydomain.com';
$mail->Password = 'password';
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$mail->Port = 587;
//Recipients
$mail->setFrom('noreply#mydomain.com', 'xxx');
$mail->addAddress('contact#mydomain.com');
//Content
$mail->isHTML(true);
$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}";
}
?>
Previously, I was using Gmail as the server, which worked fine, but now I want to change this. When I run the script with the new details I get authentication error - "SMTP ERROR: Password command failed: 535 Incorrect authentication data". The full PHPMailer script log is shown in the image below.
PHPMailer log
I know the username and passwords used are correct and have seen this error seems to be returned whenever a connection cannot be established. I have a hunch it's related to my email DNS settings, though I could be completely wrong here. When I check my DNS and MX settings using dnschecker.org all seems ok as far as I can naively tell. But using hardenize.com I see my domain fails their mail server and email TLS checks. The information provided is lacking but it does say "A network error occurred while we were trying to communicate with a server. Error message: java.net.SocketTimeoutException: Read timed out". I should add here that my site uses Cloudflare as a CDN.
Any ideas on what the issue is or how to resolve it?
Thanks in advance.

Not receiving email on Gmail using Gmail SMTP but receiving on web server using my server SMTP

I am trying to send contact form to my gmail when user submit the form, but I have not receiving any email on Gmail when I am using Gmail SMTP server, I am trying both tls with port 587 & ssl with port 465.
But when I used my server SMTP it's working properly and I also received email on my web server. Code is working on PLESK but not working on Cpanel. Code is here:
<?php
require('phpmailer/class.phpmailer.php');
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->SMTPDebug = 0;
$mail->SMTPAuth = TRUE;
$mail->SMTPSecure = "tls";
$mail->Port = 587;
$mail->Username = "**********#gmail.com";
$mail->Password = "**********";
$mail->Host = "smtp.gmail.com";
$mail->Mailer = "smtp";
$mail->SetFrom($_POST["userEmail"], $_POST["userName"]);
$mail->AddReplyTo($_POST["userEmail"], $_POST["userName"]);
$mail->AddAddress("**********#gmail.com");
$mail->Subject = "Website Contact Us Form Email";
$mail->WordWrap = 80;
$mail->Body = "User Name: ".$_POST["name"]."<br/><br/>"."User Phone Number: ".$_POST["phone"]."<br/><br/>"."User Email: ".$_POST["email"]."<br/><br/>"."User Message: ".$_POST["message"]."<br/><br/>";
if(is_array($_FILES)) {
$mail->AddAttachment($_FILES['attachmentFile']['tmp_name'],$_FILES['attachmentFile']['name']);
}
$mail->IsHTML(true);
if(!$mail->Send()) {
echo "<p class='error'>Problem in Sending Mail.</p>";
} else {
echo "<p class='success'>Contact Mail Sent.</p>";
}
?>
Assuming your authentication credentials are correct, it sounds like this could be a networking issue. Have you confirmed that your firewall(s) have permitted outbound traffic on port 587? The local mail agent may be listening on localhost and sending the mail back to you that way.
Also, since SMTP is used to SEND messages, I believe the from address should actually be the same as the authenticated account (you're sending this email to yourself, not from the user's email account). However, if you're using two different email's this doesn't apply completely.

Sending email from specific server gives socket forbidden error

I get this error
An attempt was made to access a socket in a way forbidden by its access permissions xxx.xxx.xxx.xxx:587 when sending email from a godaddy server.
I have seen many questions like this but what's unique here is that, this code works on my local computer. It also works on my other Go daddy hosting Server.
This original server has TLS 1.0, because i needed TLS 1.2, I purchased a deluxe hosting plan and moved my code to this new server, then i start getting this error. I've searched everywhere and used every combination of port 587, 465, 25 along with ssl = false or true.
Any ideas please?
using (SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", 587))
{
MailMessage mail = new MailMessage();
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = new NetworkCredential("********#gmail.com", "********");
smtpClient.EnableSsl = true;
string fromEmail = "********#gmail.com";
mail.From = new MailAddress(fromEmail, "System");
mail.To.Add(new MailAddress(toEmail));
mail.Body = body.ToString();
mail.Subject = subject;
smtpClient.Send(mail);
}
Sounds like a firewall or AV or other port blocking software preventing outbound connections to port 587. Check your server config and look in the windows event log as there might be an entry in there indicating who did the blocking.
Try to use port 2525 for 587, 465, 25. Some cloud providers disable all outbound traffic from 587, 465, 25 ports.
It seems like Godaddy is blocking emails from its servers when you use an outside smtp like smtp.gmail.com. At least that seems like the case with this Plesk Hosting Account. The other Economy Hosting works well with Gmail smtp.
Also, the emails will only send from Godaddy server, running the code locally on Visual studio gave an error.
I changed my code to this:
using (SmtpClient smtpClient = new SmtpClient("relay-hosting.secureserver.net", 25))
{
MailMessage mail = new MailMessage();
smtpClient.Credentials = new NetworkCredential("yourdomain#yourdomain.com", "****");
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
string fromEmail = "yourdomain#yourdomain.com";
mail.From = new MailAddress(fromEmail, "Name");
mail.To.Add(new MailAddress(toEmail));
mail.Body = body.ToString();
mail.Subject = subject;
smtpClient.Send(mail);
}

PHPMailer SMTP configuration

For the past 2 hours I've been looking online to see if any other people encountered this problem, and it seems a lot has, bot none of the answers are working for me.
SMTP -> FROM SERVER:220 mx.google.com ESMTP vq7sm928004oeb.13
SMTP -> FROM SERVER: 250-mx.google.com at your service, [50.57.114.141] 250-SIZE 35882577 250-8BITMIME 250-STARTTLS 250 ENHANCEDSTATUSCODES
SMTP -> FROM SERVER:220 2.0.0 Ready to start TLS
SMTP -> FROM SERVER: 250-mx.google.com at your service, [50.57.114.141] 250-SIZE 35882577 250-8BITMIME 250-AUTH LOGIN PLAIN XOAUTH XOAUTH2 250 ENHANCEDSTATUSCODES
SMTP -> FROM SERVER:530-5.5.1 Authentication Required. Learn more at 530 5.5.1 http://support.google.com/mail/bin/answer.py?answer=14257 vq7sm928004oeb.13
SMTP -> ERROR: MAIL not accepted from server: 530-5.5.1 Authentication Required. Learn more at 530 5.5.1 http://support.google.com/mail/bin/answer.py?answer=14257 vq7sm928004oeb.13
The following From address failed: my#email.com
$mail->IsSMTP();
$mail->SMTPDebug = 2;
$mail->SMPTAuth = true;
$mail->SMTPSecure = 'tls';
$mail->Host = "smtp.gmail.com";
$mail->Mailer = "smtp";
$mail->Port = 587;
$mail->Username = "my#email.com";
$mail->Password = "password";
I've tried almost every setting for PHPMailer, but can't figure out what's still going wrong, are there any server settings I need to take care of?
I also tried the normal php mail() function, but that's not sending mail either, although when using Drupal forms it just sends an email.
For first you must configure the correct server to send emails (see at gmail.com):
SMTP server address: smtp.gmail.com
SMTP user name: Your full Gmail address (e.g. example#gmail.com)
SMTP password: Your Gmail password
SMTP port: 465 or 587
SMTP TLS/SSL required: yes
In PHPMailer:
$mail->SMTPAuth = true; // There was a syntax error here (SMPTAuth)
$mail->SMTPSecure = 'tls';
$mail->Host = "smtp.gmail.com";
$mail->Mailer = "smtp";
$mail->Port = 465;
$mail->Username = "YOU#gmail.com";
$mail->Password = "YOUR_GMAIL_password";
My problem was that I use the 2-step verification. So I had to remember to go to Authorised Access for my Google Account & then assign a Application-specific passwords for the domain that I'm using to send the mail.
Just noticed I typed SMTP wrong in this line $mail->SMPTAuth = true;, correcting this solved my problem.
The suggested port+protocol-combination seems wrong to me, because it's different to the official phpMailer-wiki:
Don't mix up these modes; ssl on port 587 or tls on port 465 will not
work.
In PHPMailer:
$mail->SMTPAuth = true;
$mail->SMTPSecure = 'tls';
$mail->Host = "smtp.gmail.com";
$mail->Mailer = "smtp";
$mail->Port = 587; //use port 465 when using SMPTSecure = 'ssl'
$mail->Username = "YOU#gmail.com";
$mail->Password = "YOUR_GMAIL_password";
Setting up mail in php can be quite hard. have you tried using port=25?

.NET - Mail server doesn't send mail through SmtpClient.Send

I wrote a single console application (just a part of a site code, but it must work apart too, and it has the same fault result as inside the site) (C#):
MailMessage message = new MailMessage("login#ourDomenInPunycode", "toMail")
{
Subject = "Hello",
Body = "Hello world"
};
SmtpClient client = new SmtpClient();
client.Host = "ourIP";
client.Credentials = new System.Net.NetworkCredential("login#ourDomenInPunycode", "ourPassword");
client.Port = 25;
client.UseDefaultCredentials = false;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.Send(message);
So, isn't to send e-mail should be trivial? But wherever I send mail from local machine through our mail server (just running this console application), the following exception appears:
System.Net.Mail.SmtpFailedRecipientException: Mailbox unavailable. The server response was: 5.7.1 Relaying to denied (authentication required)
If I change the "login#ourDomenInPunycode" data to my own mailbox (at gmail or something else - no matter), all works fine. It also not depend from "toMail" address.
So, what could be wrong with our mail server? Any special settings? We use Windows Server 2008 virtualized inside another Windows Server 2008 and Kerio Connect 7 as mail server at virtual Windows Server 2008. All other mail programs like Outlook works well with sending e-mails from our mail server.
All articles which I read in Internet about SmtpClient's settings have only these (above) trivial settings and code, nothing special.
UPDATE
I done some fixes in text above.
Here is a part of log of our mail server when I tried to send mail through console application launched from the mail server virtual PC ("mail.ourDomen.local" related to "ourIP" above):
Task 215 handler BEGIN
Task 215 handler starting
SMTP server session begin; client connected from mail.ourDomen.local:49399
Sent SMTP greeting to mail.ourDomen.local:49399
Command EHLO OurMailServer
Sent reply to EHLO: 250 mail.ourDomenInPunycode ...
Command MAIL FROM:<login#ourDomenInPunycode>
Sent reply to MAIL: 250 2.1.0 Sender <login#ourDomenInPunycode> ok
Command RCPT TO:<toMail>
Sent reply to RCPT: 550 5.7.1 Relaying to <toMail> denied
Connection to SMTP server mail.ourDomen.local lost: connection closed by remote host.
SMTP server session end
Task 215 handler END
"Sent reply to RCPT: 550 5.7.1 Relaying to denied" -
Why this happened?
Well, we use this description
https://kb.kerio.com/article/550-571-relaying-to-email%40addresscom-denied-authentication-required-411.html .
Although we know about this settings, but we tangled with our virtual machines. We have a virtual machine for the web server and another one for the mail server. Permissions were configured for the mail server virtual machine only in the Kerio Connect, not for the web server. We just added permission for the virtual machine of the web server and the mail is sent normally.
And the "ourIP" in the
SmtpClient client = new SmtpClient();
client.Host = "ourIP";
is the IP of our virtual machine of the mail server. No settings of IP of the web server virtual machine in the SmtpClient object.
As suggested your mail server needs to be configured to allow "Relaying" over port 25. It is the "Relaying" setting/config you are looking for.
The idea/purpose behind "Relaying" is stop your server being (ab)used for sending spam.
Try the code without setting client.Host and client.DeliveryMethod properties.
//used this referances
using System.Net.Mail;
using System.Net;
using System.IO;
try
{
string em_from = "your seding e mail";
string em_to = Ricever e mail Address;
SmtpClient Smtp_Server = new SmtpClient();
MailMessage e_mailx = new MailMessage();
Smtp_Server.UseDefaultCredentials = false;
Smtp_Server.Credentials = new System.Net.NetworkCredential("sender email address", "sender passsword");
Smtp_Server.Port = 25; //your mail server port
Smtp_Server.EnableSsl = false;
Smtp_Server.Host = "192.XXX.XX.XX"; //your mail server IP
e_mailx = new MailMessage();
e_mailx.From = new MailAddress(em_from);
e_mailx.To.Add(em_to);
e_mailx.Bcc.Add("BCC Address");//you cad add both BCC and CC addresss
e_mailx.IsBodyHtml = false;
e_mailx.Subject = esub;
e_mailx.Body = ebody;
e_mailx.Attachments.Add(new Attachment(emsg)); //emsg mean attach file name with location
Smtp_Server.Send(e_mailx);
return 1;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return 0;
}