Codeigniter email function and Justhost SMTP - codeigniter

I had somebody build me a site using PHP and Codeigniter framework.On the test server everything was working properly, but now, once the site is transfered on Justhost, email function doesnt work.At first i tought i didnt used the correct SMTP settings, but they are fine (support took a look).
Basically, owner can put his property on the site (like this:http://goo.gl/AZcrj), and when the visitor fills the contact form on the bottom, email is sent to owners email address, and the he can reply to the visitors email.
First time i used Justhost SMTP settings and tested it, email was sent.But from that point, till now, i always get an error when i try to send message via contact form.I even tried using Gmail SMTP settings, same thing happens :\
Here is the code, does anybody have any clue?
Btw, im a PHP rookie, so i dont know much, but i did my best to explain
public function send_mail($res)
{
$this->load->library('email');
$config['protocol'] = 'smtp';
$config['smtp_host'] = 'sslsmtp.gmail.com'; --- i tried with ssl://smtp.gmail.com, same thing happens
$config['smtp_port'] = '465';
$config['smtp_timeout'] = '7';
$config['smtp_user'] = 'email';
$config['smtp_pass'] = 'password';
$this->email->initialize($config);
$this->email->from($res['sender_email'], $res['sender_name']);
$this->email->to($res['reciever']);
$this->email->reply_to($res['sender_email'], $res['sender_name']);
$this->email->subject('Email from site.');
$this->email->message($res['message']);
if($this->email->send()) {
return true;
} else {
return false;
}
//configure mail agent...
}

It seems Justhost blocks outgoing ports for shared IPs. I was having the same issue and came across this link (https://my.justhost.com/cgi/help/405) while searching for an answer.
From the above link:
Outgoing Ports Are Blocked
Just Host restricts port access as a result of the shared hosting
environment. In this environment, every account on a server shares a
single IP address. As a result, most ports are blocked because no
single account is assigned the IP address for the box.
To gain access to these ports, it is necessary to lease a dedicated IP
address through Just Host.

Related

Can I send email with mailgun sandbox domain under my local OS?

I registered myself at mailgun with domain I use for my laravel apps
Also I see that with my domain there is sandbox domain, which looks like :
sandboxdXXXXXXXXXXXXXXXXXXXX.mailgun.org
and under SMTP block I see text:
Grab your SMTP credentials:
SMTP hostname: smtp.mailgun.org
Port: 587 (recommended)
Username: postmaster#sandboxdXXXXXXXXXXXXXXXXXXXX.mailgun.org
Default password: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX-XXXXXXXXXXXXXXXX
Working on my local OS, apache 2 under ubuntu 18
I try to send email in my local having in .env :
MAIL_DRIVER=mailgun
MAIL_HOST=smtp.mailgun.org
MAIL_PORT=587
MAIL_USERNAME=postmaster#sandboxdXXXXXXXXXXXXXXXXXXXX.mailgun.org
MAIL_PASSWORD=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX-XXXXXXXXXXXXXXXX
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS='from_email#site.com'
MAIL_FROM_NAME='FROM NAME'
MAILGUN_SECRET='XXXXXX-XXXXXXX'
MAILGUN_DOMAIN='sandboxdXXXXXXXXXXXXXXXXXXXX.mailgun.org'
As result I sent email without errors, But I did not receive emails for 2 my users which are in Authorized Recipients of mailgun.
What for this sandbox domain, can I use it for local OS and can I send emails under my local OS ?
UPDATED :
In my local .env I set mailtrap.io params and it works ok.
In Account Security->API security->API keys of my mailgun account I have parameters L
Private API key
Public validation key
HTTP webhook signing key
Setting app on remote dev server
MAIL_MAILER=smtp
MAIL_DRIVER=mailgun
MAIL_HOST=smtp.mailgun.org
MAIL_PORT=587
MAIL_USERNAME=XXX
MAIL_PASSWORD=XXX
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS='from_email#site.com'
MAIL_FROM_NAME='FROM NAME'
If MAIL_USERNAME and MAIL_PASSWORD must be filled with values from Account Security->API security->API keys
also if MAIL_FROM_ADDRESS must one of Authorized Recipients I created in the settings before ?
UPDATED BLOCK 2:
I try to fill parameters from live domain, but failed.
I search paramters here : https://prnt.sc/18ouojv
?
Thanks in advance!
As Sandbox domains are restricted to authorized recipients only.So make sure to verify recipients emails in
https://app.mailgun.com/app/sending/domains
If recipients has not received email to inbox then check in spam
folder.
Ref:https://help.mailgun.com/hc/en-us/articles/360011702394-Why-Do-My-Emails-Go-to-Spam-
If still not working then change mailer to smtp
MAIL_MAILER=smtp
Then run
php artisan config:clear
Env
MAIL_MAILER=smtp
MAIL_HOST=smtp.mailgun.org
MAIL_PORT=587
MAIL_USERNAME=postmaster#sandbox**************.mailgun.org
MAIL_PASSWORD=************************
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=youraccountemailaddress
MAIL_FROM_NAME="${APP_NAME}"
I would not use the sandbox domain, as it has problems, does not work like a real domain and there is no reason to not start with the real domain for free.
Short description to start with MailGun with your own domain:
Create an account at mailgun including a credit card
-> If you don't add a credit card, you will not be able to use MG
-> This don't means, that you have to pay unless you don't reach the limit
of 5'000 emails / month
add your (existing) domain in MG (only possible, if you have added a CC)
under "Sending" - button "Add new Domain" (Button only visible if a CC is added)
follow exactly the steps that are showed at the right top corner
in the MG portal. Title "Getting started"
You have to reach 100% before you will be able to send your first emails
Note:
This also includes "illogical" steps - e.g. send an email to mailgun#YourDomain
although, a mailbox "mailgun" does not exist
You further have to send an email to yourself
These steps seems to be needed for initial verification at MG
add exactly the DNS entries from the MG portal (spf and mta) to your domain
To do this you need access to the portal of your domain provider
in MG - under DNS Records - press button "Verify DNS settings"
the DNS settings have to be verified (a green check is showed), before you can send emails
If anything is O.K., you will receive an eMail from MG:
Good news! The DNS settings you added for YourDomain have been verifyed.
If you have received this email, you should be able to send emails over MG
Further notes to development (I work with Visual Studio 2017, Windows Forms and vb.net):
I had to set TLS 1.2 to be able to connect:
vb.net code:
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12
I further have added the NuGet package: RestSharp (as the code examples in MG are based on RestSharp).
Code example for vb.net (after adding the NuGet to RestSharp):
Imports System.Net
Imports System.Net.Mail
Imports RestSharp
Imports RestSharp.Authenticators
...
Dim client As RestClient = New RestClient()
client.BaseUrl = New Uri("https://api.eu.mailgun.net/v3/")
Dim cAPIKey As String = "YourKey from MG"
client.Authenticator = New HttpBasicAuthenticator("api", cAPIKey)
Dim request As RestRequest = New RestRequest()
request.AddParameter("domain", "YourDomain", ParameterType.UrlSegment)
request.Resource = "{domain}/messages"
request.AddParameter("from", "Your Description <Mailbox#YourDomain>")
' add mail address(es):
request.AddParameter("to", "mail-address1")
' further, if needed (Note: I send a single email to each recipient):
request.AddParameter("to", "mail-address2")
' add bcc mail address (if needed):
request.AddParameter("bcc", "bcc-mail-address")
' fill the mail:
request.AddParameter("subject", cMailBetreff)
request.AddParameter("html", cHTMLMailBody)
request.Method = Method.POST
Dim status As IRestResponse
status = client.Execute(request)
' state handling:
If status.StatusCode.ToString = "OK" Then
' O.K handling
Else
' Error handling
End If
Notes to the code above:
I send a complex html, that I have stored in a file that is loaded in the variable cHTMLMailBody.
After the load, some variables in the file are change to customer values stored on a SQL server (cMailBetreff also contains a customer specific generated text).
As I'm not from thee USA, I have to use the url: https://api.eu.mailgun.net/v3/".
If you are from the USA, you have to use another url (see MG portal).
Notes to the error handling in MG (Log in MG):
I have done some tests with the code above (that works).
As I also want to know (and store), if the sent email has reached the recipient, I have done some tests and sent emails to a not existing domain and an existing domain, but not existing mailbox.
To the not existing domain, an error was logged in MG (what is correct).
To the not existing mailbox, the state OK was logged in MG (what is wrong).
The reason to the wrong state (OK) to the not existing mailbox was, that MG don't receive non delivery mails, if there is no MX record to the domain that targets the MG server.
As we (manually) also send out emails to the domain from our own mail server, I don't wanted to change the mx record to our domain in general to the MG server.
Therefore I have added a sub domain to our domain (in MG and also at our DNS provider) and then added a MX record to the new sub domain that points to the MG server.
Then, I have changed the domain parameter to SubDomain.Domain:
request.AddParameter("domain", "SD.Domain", ParameterType.UrlSegment)
The problem then was, that a not nice "on behalf of" was generated automatically in the header.
I was able to solve the issue by adding a further parameter:
request.AddParameter("h:sender", "Your Description <Mailbox#YourDomain>")
(same value as in from parameter, whereby the h: is needed in addition)
After doing that, MG also receives non delivery emails and add also an error to not existing mailboxes.
So.. if you need to get/store the states to the sent emails and don't want to change your mx record generally to MG, you should add a sub domain (if not, you should not need a sub-domain).
For me, everything now seems to work as it should:
we can sed out html mails over MG
we can capture the states to the sent emails over the MG API
we still can send (manually) emails over our own mail server
And important:
if a recipient answers to an email, the answer is delivered to our mailbox on our server
Hope this helps somebody...

Outgoing Email Sending Issue in Godaddy Windows 2012 VPS

i'm trying to send email from web Application deployed in Godaddy Windows 2012 Vitual Private Server.
Error Displayed "Server actively refused the connection".
Email send from my local system, but outgoing email is not working on Godaddy Hosting.
using (var smtp = new SmtpClient()) {
var credential = new NetworkCredential {
UserName = "user#outlook.com",
Password = "password" };
smtp.Credentials = credential;
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.EnableSsl = true;
await smtp.SendMailAsync(message);
return RedirectToAction("Sent");
}
Firstly, try adding:
smtp.UseDefaultCredentials = false;
Before:
smtp.Credentials = credential;
Since June of 2016, Gmail changed its DMARC policy from p="none" to p="reject".
Any emails sent using a from address ending with #gmail.com, will have to originate from within Gmail's infrastructure.
You will need to use an email sending service, such as SendGrid (www.sendgrid.com) or mailgun (www.mailgun.com).
You should be using one of these anyway, as they help you keep off spam blacklists and provide lots of other benefits, including details of showing you if emails have been blocked due to bad email addresses, spam reports, etc.
More information on what DMARC is and how it works is available in this article on the Sendgrid blog.

expose application to IP range or validate a login requet against an IP range

Is there a Wakanda way to:
expose a Wakanda application only to a specific IP address or IP range
validate a login request (custom login) against an IP address or IP range
I will answer the questions one by one :
1-
This should be done using your OS' firewall (using iptables for instance if you are on Linux)
2-
If you restrict access to the app using the firewall, you might not need to do this. But if you really need to :
Today there is no good way to do this, because you can't disable the authentication REST API. The workaround I propose is as follows (but I don't think it will work if you are using active directory ):
Add a custom request handler for authentication /login where you do something like :
function login(request,response){
var ip = request.remoteAddress;
if( ! isIPAuthorized(ip)){
response.statusCode = 403;
return;
}
sessionStorage["login-request"] = true;
/*
* Your login code here
* For instance you can use loginByPassword, createUserSession ..
*/
sessionStorage["login-request"] = false;
}
Inside your Login Listener you can check if the login request came from you custom login function or not by checking the sessionStorage :
if(!sessionStorage["login-request"]){
return {
"error" : 1024,
"errorMessage" : "Unautorized Login Attempt"
}
}
This way any login attempt using the default REST authentication API where IPs are not checked will be refused.
The best way to expose any application to a specific range of IP addresses (while blocking all others) is with a firewall; whether it be a software-based firewall (such as iptables or windows-firewall) or a hardware-based firewall (such as a Cisco ASA). Using a firewall completely alleviates the need for the second part of your question.
For the first question, enable the Cross-origin resource sharing in the Project Settings file ( Settings.waSettings ), and you need to define the list of Domain name or IP address from where external pages are allowed to send data requests to the Wakanda Server via CORS. Multiple domain attributes can be added to create a white list.

Sending mail via relay using PHP on Windows

I am using PHPMailer (via SMTP) to send out emails via my websites. I am using a windows 2012 server as my mail server which is using Hmailserver. I am using Mailgun to relay my emails.
Things I have done:
I have setup and validated my mailgun settings.
I have tested sending and receiving emails via the server without the relay (works fine).
My dilemma:
At one stage I am going to have to state the relay information, such as the authentication or hostname. So far, i see two place where i can declare this - see below:
1) Specify via PHPMailer script:
$mail->IsSMTP();
$mail->host = "smtp.mailgun.org";
$mail->Username = "username";
$mail->Password = "password";
2) Specify in hmailserver admin (on the server):
http://puu.sh/cJLpk/c3d548981c.png
Which way do I do this if I want to relay all my emails?
Using your local mail server (hmailserver) as a relay will be faster (at least for your client scripts) and much more reliable. It should be configured to point at mailgun, as in your screen shot. Mailgun should provide you with credentials that you can use for authenticating the relay.
When you send with PHPMailer, you should configure it to point at localhost, like this:
$mail->IsSMTP();
$mail->Host = 'localhost';
$mail->Username = "username";
$mail->Password = "password";
(You may not need username and password for your local server). Though it may sound odd, using SMTP to localhost is often faster than calling a local sendmail binary (or using PHP's mail() function).
In your original code you had host instead of Host - PHP is case-senetive for property names, so that would have been failing if that was your real code.
That should be all there is to it.
The only other complication is if hmailserver is also sending messages that are not supposed to go through mailgun, in which case you will need to get further into your hmailserver config.

Sending Email from Windows Azure Through BPOS

I'm trying to send an email via asp.net mvc2, and all works well on our companies mail server.
However, we're looking to switch to using our client's BPOS account. This works fine locally, but when deployed to azure, we're getting some timeout errors.
Can anybody confirm I have the following correct?
SmtpClient smtp = new SmtpClient("Smtp.mail.emea.microsoftonline.com");
smtp.Port = 587;
smtp.EnableSsl = true;
smtp.Credentials = new NetworkCredential("BPOS-EMAIL", "BPOS-PASSWORD");
In addition, does anyone know why it works when run locally, but not when run on azure?
Are you running the web role in Full Trust? By default, SmtpClient is going to execute under ASP Medium trust. Under that model, Smtp is only allowed to communicate over port 25 in Azure.
If you need to use the SmtpClient.Port property, you'll need to have the role run under full trust.
That's achieved by setting the enableNativeCodeExecution attribute on the Web Role in the Service Definition.
<WebRole name="WebRole1" enableNativeCodeExecution="true">
Thats the only thing I can think it might be. If that's not it and you can post error logs, that may show more details as to the problem.

Resources