Can't send email via SMTP because "550 - Relay Not permitted" - php

I'm using CakePHP to send automated emails to clients. It's been working great, but it seems some recipients aren't receiving our emails. So I decided to use the SMTP option for sending emails, and route emails through our email provider at Media Temple.
However, when trying to send email from a Media Temple account, I get the error "550- relay not permitted".
That sounds like the Media Temple server is just plain not allowing me to send mail through it.
That's odd because I've confirmed the username and password I'm using is correct and I can send mail via SMTP through it from my macmail client and iPhone mail client. I've also confirmed my cakephp email settings are correct, because I can send emails via SMTP with a gmail account with the exact same configuration in cakephp.
Any idea why I'm getting this error and how to resolve it?
Thanks
Here's the code that handles sending an email. I use this class just like the regular EmailComponent from within many different controllers.
class CanadafindsEmailerComponent extends EmailComponent
{
...
function send($content = null, $template = null, $layout = null) {
if(!in_array(TECHY_MONITOR_EMAIL,$this->bcc) && is_array($this->bcc))
$this->bcc[]=TECHY_MONITOR_EMAIL;
else if (!in_array(TECHY_MONITOR_EMAIL,$this->bcc) && !is_array($this->bcc))
$this->bcc=array(TECHY_MONITOR_EMAIL);
if(DEVSITE){//commented-out code are settings for smtp with gmail, which works fine
$this->delivery = 'smtp';
$this->smtpOptions = array(
'port'=>'465',//'465',
'timeout'=>'30',//'30',
'auth' => true,
'host' => 'ssl://mail.thenumber.biz',//'ssl://smtp.gmail.com',
'username'=>USERNAME,//'USERNAME#gmail.com',
'password'=>SMTP_PASSWORD//,
);
$this->to=$this->correctFormatOn($this->to);
$this->bcc=$this->correctFormatOn($this->bcc);
$this->cc=$this->correctFormatOn($this->cc);
$this->replyTo=$this->correctFormatOn($this->replyTo);
$this->from=$this->correctFormatOn($this->from);
}
return parent::send($content,$template,$layout);
}
function correctFormatOn(&$email){
if(is_array($email)){
$copiedEmail=array();
foreach($email as $singleEmail){
$copiedEmail[]=$this->correctFormatOnSingle($singleEmail);
}
$email=$copiedEmail;
}else{
$email=$this->correctFormatOnSingle($email);
}
return $email;
}
function correctFormatOnSingle(&$email){
$subEmails=explode(",",$email);
$fixedSubEmails=array();
foreach($subEmails as $subEmail){
$fixedSubEmails[]=preg_replace('/<?([^< ]+)#([^>,]+)[>,]?/i', '<$1#$2>', trim($subEmail));
}
$email=implode(",",$fixedSubEmails);
return $email;
}
}

The main problem I was having was that clients weren't receiving emails from our server, (and so I wanted to use an SMTP server to see if that would fix it, instead of the server's default email server).
But I managed to get those clients to receive emails from the server by making some other changes, thus removing the need to use SMTP and the Media Temple email server.
(As an FYI, I found that we were getting bouncebacks from client email servers stating Diagnostic-Code: smtp; 550 Access denied - Invalid HELO name (See RFC2821
4.1.1.1), but they were being sent directly back to the server, and going into the linux user account "www-data". (I read them in /var/mail/www-data, just using tail and vim). I found that postfix, which was handling the sending of emails, was marking the email sender's hostname (ie, "HELO name") as canadafinds3, the name I gave the server in Rackspace, not the domain name: canadafinds.com. So I changed that in /etc/postfix/main.cf, restarted postfix, et voila! No more bouncebacks from those particular clients, and everyone's happy again.)

I ended up writing my own PHP mail() script based on https://web.archive.org/web/20180401094709/http://www.dreamincode.net/forums/topic/36108-send-emails-using-php-smtp-direct/ in order to circumvent this error.

Related

Laravel + Swift Mailer: Error: no valid recipients

Note: this sadly is no duplicate, I already tried everything mentioned there.
I tried to set up a connection to my mailserver with Laravel/Swiftmailer. The Mailing section of my .env looks as follows:
MAIL_DRIVER=smtp
MAIL_HOST=<THE MAILHOST>
MAIL_PORT=465
MAIL_USERNAME=<THE USERNAME>
MAIL_PASSWORD=<THE PASSWORD>
MAIL_ENCRYPTION=ssl
Personal information is censored for obvious reasons, but the configuration does work. I tested it by connecting to the server with this configuration with thunderbird and it works like a charm.
Here is where I call the Mailable:
public function from_id(string $id): User
{
.
.
.
Mail::to(<WORKING EMAIL ADRESS>)->send(new OrderShipped());
}
Thats how the Mailable looks like (it basically is the example one from the laravel docs):
public function build()
{
return $this->view('email_templates.simple_test');
}
I debugged into the code and there are actually two exceptions thrown, altough whoops! only shows the latter:
Expected response code 250/251/252 but got code "554", with message
"554 5.7.1 : Recipient address rejected: Access
denied"
Expected response code 354 but got code "554", with message "554 5.5.1
Error: no valid recipients"
I tried to sent an email from the perfectly working mailserver via thunderbird to the <WORKING EMAIL ADRESS> and the mail got sent and received in a fraction of a second and without any problems/errors. Basically, I tried to reproduce the exact same scenario with a different tool. So from my point of view, the error must be in the codebase.
Thanks in advance
This seems to be a SMTP error, what kind of error code you are getting ?
Code 354: means the SMTP server telling Laravel to proceed and send the body of the email. Note that this is what is expected, and not what you received.
Code 554: from a SMTP server indicates: "Transaction Failed", and you'll note that this is what you actually got.
The reason this is failing is indicated further in the message: "Error: no valid recipients."
Without further data it will be hard to tell why the recipient isn't set properly on your codebase.
For complete list of smtp erro codes can visit: https://sendgrid.com/blog/smtp-server-response-codes-explained/
You need to contact your sysadmin to ask if you need STARTTLS or just TLS.
STARTTLS starts a plain connection and then switches to a secure TLS 'channel', TLS connects to secure connection directly but is less compatible.
if the answer is STARTTLS use the port 587
else keep your port but verify it with your sysadmin
another reason could be that you account was banned or your IP blocked
My answer isn't Laravel specific, as I'm working in Yii2 environment, but the error message was the same as above. (Maybe I'll help someone someday.)
My original problem: the contact form was not working while at other places the app was sending out automatic mails properly - which was weird.
It turned out that when setting the 'from address' in the SwiftMailer config I put in the contacting person's (sender's) e-mail address, which was not allowed.
When I configured the proper user account (which I use to authenticate to the SMTP server) as the sender address, there was no issue. As soon as I tried to 'mimic' that the users are sending the e-mails from their own addresses, I was given that same message.
All I could do to "fix" this is to include the 'ReplyTo' parameter with the e-mail, this way when I reply to the incoming mail in Outlook, the actual sender's address is populated instead of the SMTP mailbox used for contact-mail-sending.
I've also included a short message at the bottom of the Body to indicate who the sender is (name and address) - just for convenience.
Your setup looks a little strange to me. Please try with the below syntax (note here I've included a $_orderObject variable as a placeholder to represent order details):
app/Mail/OrderShipped.php
<?php
namespace App\Mail;
use Illuminate\Mail\Mailable;
class OrderShipped extends Mailable
{
protected $_name;
protected $_orderObject;
public function __construct($name, $order)
{
$this->_name = $name;
$this->_orderObject = $order
}
public function build ()
{
return $this->to (<CONFIRMED WORKING EMAIL>)
->subject ('New Order!')
->view ('email_templates.simple_test')
->with ([
'name' => $this->_name,
'order' => $this->_orderObject
]);
}
}
//calling it
public function from_id(string $id): User
{
$name = << $this->getUserNameById ($id) >> // fake method
$orderObject = << $this->getLatestOrderByUserId($id) >> // fake method
Mail::send(new OrderShipped($name, $orderObject));
}

Why isn't the Codeigniter email class sending mail with sendmail?

I'm attempting to send an email address out to registrants who register for my site however this section of my code is not running. I'm attempting to find out why. I've followed all the necessary documentation. I replaced this section with php's mail function for debugging to see if it'll send an email on my server and it will. Does anybody have any thoughts on what it could be?
// User was successfully created and the user needs to verify their account.
// Send registered an email informing them how to validate their account.
$this->load->library('email');
$this->email->from('owner#owner.com', 'Owner Name');
$this->email->to($post_email_address);
$this->email->subject('Site Name Here Registration');
$this->email->message('Thank you for registering for our site. Here is your registration key to activate your account: '.$registration_key.' Please click on the following link to activate your account.<br />'.anchor('project-manager/login/verify/'.$registration_key, 'Click Here To Activate Your Account', ''));
$this->email->send();
Here is the configurations for the email.com inside of the application/config directory.
<?php
$config['protocol'] = 'sendmail';
$config['mailpath'] = '/usr/sbin/sendmail';
$config['charset'] = 'iso-8859-1';
$config['wordwrap'] = TRUE;
I know that it seems unnecessary, but I typically always use SMTP with the mail class. There are just too many server configuration issues that can result in mail not being sent. Sometimes anonymous (e.g. 'nobody' or 'www-data') users are restricted from using sendmail - a very common problem if the server isn't properly configured with suexec.
Additionally, using SMTP relies on known-good DNS / SPF settings of a known-working mail server - might not be the case on individual app servers that you use.
You could probably get to the bottom of the issue with a bit more digging, and possibly even get it corrected - but then you've got the same issue all over again if you need to move your app, or bring up more servers.
Just follow the email class documentation to use SMTP, which is just really a matter of changing the $config['protocol'] accordingly, then add the SMTP host / user / pass variables. Set up an email account, and give it a go. Mail forwarding settings can handle things like noreply a bit more gracefully as well.
To try it, just override the $config array in your controller, e.g.:
$myTempConfig['protocol'] = 'smtp';
$myTempConfig['smtp_host'] = 'mail.somedomain.com';
$myTempConfig['smtp_user'] = 'username';
$myTempConfig['smtp_pass'] = 'swordfish';
$myTempConfig['smtp_port'] = 25;
$myTempConfig['smtp_timeout'] = 30; // adjust as needed for busy mail servers
then call:
$this->email->initialize($myTempConfig)
so it loads the settings. You could just call it $config if you want, I just like making it clear what I'm doing when I do that.
It's really just better to rid yourself of the problem and be done with it once and for all. Once you have SMTP setup, your code just works, no matter where you put it.
Most decent hosting utilizes suexec correctly which means the mail class will 'just work' using sendmail. However, if you tend to deploy a lot of stuff with platform-as-a-service providers, or your own servers, it just makes better sense to use SMTP and save some headache and work.

sending emails with codeigniter received in junk mail

am trying to send emails using codeigniter email library to send emails to users using the following settings
$this->load->library('email');
$this->email->from('email#domain.com','Admin');
$this->email->to($recieverEmail);
$this->email->subject('Morgan MarketBook');
$this->email->message($message);
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
$config['protocol'] = 'smtp';
$config['smtp_host'] = 'mail.server';
$config['smtp_port'] = 26 ;
$config['smtp_user'] = 'user' ;
$config['smtp_pass'] = 'password' ;
$config['newline'] = "\r\n";
?>
my problem that the received emails are in the junk mail and not in the inbox...what is causing this problem ?
Mail getting into junk instead of inbox isn't code related or codeigniter related for that matter. You have to follow some guidelines in order for the email not to be considered spam:
Some of these guidelines are:
when sending html email, include also the text version of the mail
when sending html email, keep the html and images to a minimum (don't include javascript)
setup the mail server with spf and domain keys
the "from" field should contain a valid email address (with the same domain as the mail server)
if you send mass emails, try to limit the rate of sending
there are lots of guidelines for sending valid emails with php, just google "best practice for sending emails php"
Also, don't include your login credentials to your mail server.
Cheers
I beleive the problem is in your server, not in your CodeIgniter code. Try sending e-mail from the same e-mail address using a mail client. If you still receive the e-mail in the Junk mail you should contact your hosting provider and tell them about this problem, but my experience shows that they cannot do nothing about it.

Google App-Gmail Unable to set from. Gmail replacing from address when sending via swiftmailer SMTP transport (PHP)

Hello and thank you for any help in advance.
I'm using swiftmailer, SMTP transport method to send email from my PHP script.
Sending the message is fine. It works.
The problem is, no matter what I set the FROM,REPLYTO,SENDER settings to, The email comes through as the GMAIL mailbox (ME - me#mydomain.com) instead.
$transport = Swift_SmtpTransport::newInstance('imap.gmail.com', 465,'ssl')
->setUsername($login)
->setPassword($password)
;
$message = Swift_Message::newInstance($subject)
->setFrom(array('test#testdomain.com' => 'test mcttest'))
->setReplyTo(array('test#testdomain.com' => 'test mcttest'))
->setSender('test#testdomain.com')
->setTo(array($to))
->setBody(wordwrap($body, 70))
->setContentType('text/plain')
;
So the email goes through fine, but instead of being from TEST-test#testdomain.com as the sender... it's ME-me#mydomain.com
I switch to a separate (non-gmail) SMTP server to test and the email went through fine, without any problems... definitely think it's a GMAIL thing.
Anyone know how to get around this?
Yes, it is Gmail that changes the sender e-mail address to be the same of the account you are authenticating. There is no workaround for that, except maybe becoming a paid customer of Google Apps.
Funnily enough I've come across the same issue. But as a bit of an insight, I think this is actually a Swiftmailer issue.
There are other libraries and frameworks (Codeignitor pops in my head) that will let you use Googlemails SMTP servers while masking the from address as an email address of your choosing.

Why is email not being sent through PHP even though it is being reported as sent?

I'm trying to sends mails in PHP. The code I used for sending a mail in CakePHP is given below. I get the message 'Simple Email Sent' in my web page but the mail is not delivered to my inbox. Am I missing something?
The values in the to, subject and link fields are set with the values entered in the user interface.
$this->set('to',$this->params['form']['to']);
$this->set('subject',$this->params['form']['subject']);
$this->set('link',$this->params['form']['link']);
$this->Email->to = to;
$this->Email->subject = subject;
$this->Email->from = 'someperson#somedomain.com';
$this->Email->delivery= 'mail';
$this->Email->sendAs='text';
$this->Email->template = 'simple_message';
//$this->Email->send(link);
if ( $this->Email->send(link) ) {
$this->Session->setFlash('Simple email sent');
} else {
$this->Session->setFlash('Simple email not sent');
}
On a Linux system, you'll probably have a sendmail script installed already, and PHP will use that. If this is what you have and it's not working, then I'd look for mail configuration problems in your Linux system itself.
On a Windows system, you'll need to configure the SMTP server you want PHP to send mail to. The normal way to do this is in php.ini. The instructions for this are here.
Unless you have set Email->delivery this should be the same for CakePHP - it should default to whatever PHP uses.
Note: If you are using your own Linux install, it could just be that your ISP is blocking port 25, which your mail server is using. In that case you'll need to configure linux to route email to your ISP's email server. Maybe this will help?
Since when is 'to' (line 4) a valid destination email address?
You need to use variable syntax for setting to 'to' line, and the 'subject' line. Those lines should read
$this->Email->to = to;
$this->Email->subject = subject;
Also, I believe there is an attribute in the Email component called error (I cannot find it in the documentation currently) that will help you debug. This may not be totally correct; I use the Email component with SMTP, and there is an attribute that gets set by the Email component called smtpError. I believe there is one called error that you can use to check for an error -- it should contain code that will tell you where your problem lies.
In case that's an incorrect statement, you can always do a var_dump( $this->Email ); after you try to send an email. That will dump the entire contents of the object, so you can see if you have set attributes correctly, and it should help you find out what the error attribute is named.

Categories