Symfony 1.4: email configuration - php

I'm trying to simply send an email from my server running Symfony 1.4.
I have the usual basic mail server setup, with the server name mail.tixxit.com.au and port 25. I have this configuration in my factories.xml:
mailer:
param:
transport:
class: Swift_SmtpTransport
param:
host: mail.tixxit.com.au
port: 25
encryption: ssl # Not sure on this but have tried "tls" too
username: myaccount#tixxit.com.au
password: mypassword
Then I have a basic bit of send code in one of my actions:
$this->getMailer()->composeAndSend(
"myaccount#tixxit.com.au",
"anotheraccount#gmail.com",
"Message title",
"Message content"
);
From my what I've seen mentioned in other documentation, this should be all I need to do to send email from my server using Symfony in PHP, but this is not working. The send code does not give any errors, I can hit the mail.tixxit.com.au server from my web server fine, my credentials are definitely correct, and I have confirmed with support from my email hosting company that this should work and no configuration is required on their side to allow this.
But my mail isn't being sent. I have tried a whole lot of different settings in factories.xml but nothing works. It appears to send, but nothing ever arrives at anotheraccount#gmail.com. I have tried this on my local machine and my web server and get the same result.
What am I missing here? What Symfony/PHP/server/mail account settings do I need to actually make this work? Is there some fundamental other configuration that I am supposed to have set before the Symfony stuff that will allow me to send?

I couldn't find an answer to this. Nothing seemed to work. I gave up and used the natural Swift classes e.g.:
$transport = Swift_SmtpTransport::newInstance('mail.tixxit.com.au', 25)
->setUsername('myaccount#tixxit.com.au')->setPassword('mypassword');
$mailer = Swift_Mailer::newInstance($transport);
$message = Swift_Message::newInstance("Message title")
->setFrom(array('myaccount#tixxit.com.au' => 'App'))
->setTo(array('anotheraccount#gmail.com'));
$message->setBody("Message content.");
if ($mailer->send($message, $errors)) {
$success = true;
}

Related

Symfony swiftmail won't send from code

Been struggling with swiftmail for a while now.
I have a linux server and properly working sendmail. Sending mail from command line works.
And sending with swiftmail via command line works as well so the configuration must be OK.
But from code it doesn't. Must have read all the issues of this and I'm totally confused what's going on.
The token is created OK and everything seems to work. No errors in logs. But the mail just doesn't come. I have tried to send to different domains as well. And as I said, it works from command line with mail and swiftmail commands like this:
bin/console swiftmailer:email:send --subject="Test" --body="test" --from="info#domain.com" --to="test#anotherdomain.com"
Here is the current code:
$mailer = $this->get('mailer');
$message = $mailer->createMessage()
->setSubject('You forgot password')
->setFrom('info#domain.com')
->setTo($username)
->setBody('You forgot your password. Go and change it from http://url.com/change-password/'.$user->getConfirmationToken());
$mailer->send($message);
Here is config.yml:
swiftmailer:
transport: sendmail
host: /usr/sbin/sendmail -bs
I had the same issue and fixed it by disabling spool on swift mailer.
You didn't specify the Symfony version you are using. If you are using Symfony 4, there should be a config file in /config/packages/swiftmailer.yaml
Here is mine:
swiftmailer:
url: '%env(MAILER_URL)%'
#spool:
#type: 'memory'
#type: file
#path: '%kernel.project_dir%/var/spool'

Symfony 2: Swiftmailer not sending, no errors

Ive recently migrated this website (made with Symfony 2) to a new server, and we decided to go with Google Apps to send emails.
After some connection problems, Ive managed to set it up. I know is working because I can send email in the command line using:
app/console swiftmailer:email:send --subject="Test" --body="test" --from="xxxx" --to="yyyyy"
It works, the email is sent and I receive it immediately.
The problem is: inside the site, is not working. Here is the code:
$mailer = $this->get('mailer');
$message = $mailer->createMessage()
->setSubject('Caderno Mágico: Redefinir Senha')
->setFrom(array($this->container->getParameter('server_mail_address') => 'Caderno Mágico'))
->setTo($usuario[0]->getEmail())
->setBody($this->renderView('Emails/recuperar_conta.html.twig', array('username' => $usuario_selecionado->getUsername(), 'dados' => $hash)), 'text/html');
$mailer->send($message);
Seems right, was working before. The user email there is the same email I used on the command line test. And the config used there is supposed to be the same that's being used when I send it though the command line:
swiftmailer:
transport: smtp
host: smtp.gmail.com
username: yyyy
password: ******
auth_mode: login
port: 465
encryption: ssl
spool: { type: memory }
swiftmailer.plugins.loggerplugin:
class: 'Swift_Plugins_LoggerPlugin'
arguments: ['#swiftmailer.plugins.loggerplugin.logger']
tags: [{ name: 'swiftmailer.default.plugin' }]
swiftmailer.plugins.loggerplugin.logger:
class: 'Swift_Plugins_Loggers_EchoLogger'
arguments: [false]
There is no errors on any log, and the "send" method return true. It just dont works. (Ive tried with and without the spool: no difference). No email is sent.
Ive set this "EchoLogger" plugin, and it works on the command line. I see can all messages and errors at the output, which helped me setting the config and fixing the connection problems. But that doest not seems to do anything when being executed by the server, I cant see any messages on the screen or in any logs.
Ive even tried writing all the values in the code exactly like they are on my command line test, to rule out the possibility of something being wrong there, and still nothing:
$mailer = $this->get('mailer');
$message = $mailer->createMessage()
->setSubject('Test')
->setFrom('yyyyy')
->setTo('xxxxx')
->setBody('test');
$mailer->send($message);
So what can I do? What could be wrong? I dont even know how to debug this. Why would it work when run at the command line and fail silently when run inside the website? Is there any way or any place to see those EchoLogger messages when the code is being run at the server?
Ok, Ive found my own solution. Well, more like a workaround, actually.
It seems like I was having a spool problem: messages were going to the spool and then never sent, for some reason.
So Ive found 2 ways to make ir work:
force a spool flush right after sending:
$mailer->send($message);
$spool = $mailer->getTransport()->getSpool();
$transport = $this->get('swiftmailer.transport.real');
$spool->flushQueue($transport);
explicitly disable the spool in the config:
spool:
enabled: false
In both ways, emails are sent normally
This might be because you didn't tell the controller to send the mail, use this:
$this->get('mailer')->send($message);

Trying to send emails with Symfony 2 using SwiftMailer : Connection could not be established with host smtp.gmail.com [ #0]

Here's a description of my environment first, since it's a bit particular :
I use a Windows 8.1 PC combined with a virtual machine running with Vagrant. It's basically a Debian 64bits machine.
This VM is in fact my server.
Then, I use Symfony 2 for my project and I'm trying to use SwiftMailer with gmail (using my own gmail adress) in order to send emails thanks to a contact form (email address of the user, name of the user and content of the mail).
My action fills the email data with the ones given in the form and sends the mail.
Btw, it checks if the data is valid by using the ->isValid() method.
My problem comes after I submit the form, I receive an Exception from SwiftMailer : Connection could not be established with host smtp.gmail.com [ #0]
Here's my config_dev.yml :
swiftmailer:
transport: gmail
username: "%gmail_user%"
password: "%gmail_password%"
delivery_address: "%gmail_user%"
encryption: ssl
host: smtp.gmail.com
port: 465
auth_mode: login
The "%gmail_user%" and stuff is defined in a parameters.yml file. Here's the content :
parameters:
mailer_transport: gmail
mailer_user: null
mailer_password: null
gmail_user: my_gmail_address#gmail.com
gmail_password: my_gmail_passwd
After a lot of search through the whole Internet, I've tried many things from here or from elsewhere, and nothing did work : Using tls, trying port 587, trying with the IP of the smtp.gmail.com server gave me the same error.
The SSL extension seems to be enabled too :
php --info | grep openssl
OpenSSL support => enabled
and some other stuff that tells the version of OpenSSL.
I checked my IMAP settings and the general setting that enables or disables applications to access to my gmail, it is enabled.
I also tried to telnet smtp.google.com 465 and it worked fine since I reached to connect.
I just don't know what to do now. I hope some of you had the same issue and can help me.
Have a nice day !
Edit : Problem solved and a new one appeared
After further investigation, I randomly tried to change the config_dev.yml file that way :
transport: mail
This solved my problem with the SwiftMailerException, though the mail doesn't come to the delivery_address email address even if I change it to another address I own (an hotmail one).
I'll work on it, but I hope my solution can help.
Edit #2 : Tried to solve the sending problem but nothing worked.
This is going to be a book..
I tried to add a spool system by adding this in the config.yml :
spool: {type: memory }
The mail seem to be sent since the profiler tells me he sent it.
I added this to my controller to send all the mails that could be spooled :
$spool = $transport->getSpool();
$sent = $spool->flushQueue($this->container->get('swiftmailer.transport.real'));
I also added this to check if it's really sent :
if ($this->mailer->send($message)) {
echo '[SWIFTMAILER] sent email to ' . $toEmail;
} else {
echo '[SWIFTMAILER] not sending email: ' . $mailLogger->dump();
}
I am really lost and don't know what to do..
Here's the code that send the mail in the controller :
$message = \Swift_Message::newInstance()
->setSubject("The subject from the form")
->setFrom("The address from the form")
->setTo("The address which comes from an entity")
->setBody("The body from the form made with a twig template");
$mailer = $this->container->get('mailer');
$mailLogger = new \Swift_Plugins_Loggers_ArrayLogger();
$mailer->registerPlugin(new \Swift_Plugins_LoggerPlugin($mailLogger));
if ($mailer->send($message)) {
echo 'sent email';
} else {
echo 'not sending email: ' . $mailLogger->dump();
}
This always tells me 'sent email', but I never receive it.
Did you try to make a connection with cURL? I had something similar with elasticsearch (elastica), apparently it used a cURL HTTP proxy I didn't know about because it was configured in php.ini.
off-topic: You can also try if mailcatcher works for your needs.
I had the same error (Connection could not be established with host smtp.gmail.com [ #0]) and the reason was in antivirus (Avast!). When I disabled it error has gone.
I found the solution and never answered here but since this question has like 2000+ views I feel like it's important.
So, everything was fine and the only thing I didn't know about was that during development (aka when in dev environment), swiftmailer seems to disable delivery, as told here :
http://symfony.com/doc/current/email/dev_environment.html#disabling-sending
All I had to do was to set this in config_dev.yml and config_test.yml files :
swiftmailer:
disable_delivery: false
Just be careful to which address your mails go. For example, on this project, it would have sent dummy mails to our clients which would have been quite problematic.
To prevent this, you can do what's told in the symfony documentation (http://symfony.com/doc/2.8/email/dev_environment.html#sending-to-a-specified-address-es)
# app/config/config_dev.yml
swiftmailer:
delivery_addresses: ['dev#example.com']

How can I debug email sending within my Symfony2 application?

I am using the FOSUser bundle within my Symfony2 application. I am trying to get the email functionality working but can't get any emails to send successfully.
My development environment is a Ubuntu virtual machine. I tried to use my Gmail account details as described here. I also checked the error logs but nothing regarding the email sending is logged.
I attempted to send a test email using a test controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class TestEmailController extends Controller
{
public function sendEmailAction()
{
$message = \Swift_Message::newInstance()
->setSubject('Hello Email')
->setFrom('send#example.com')
->setTo('me#myemailaddress.com') // use a valid email in actual code...
->setBody('yo, wassup!');
$this->get('mailer')->send($message);
return $this->render('SysDevPunctualityBundle:TestEmail:sendEmail.html.twig', array(
// ...
));
}
}
However this triggers an error with Symfony's web profiler toolbar and I get the following message; "Token "3c494e" was not found in the database."
After some digging around I found this question I followed the suggestion of removing the spool option and also added a 'from_email' option. This made a difference, I am now getting a timeout error:
Connection could not be established with host smtp.gmail.com [Connection timed out #110]
My guess is that the firewall is blocking the request, I don't know how to find out for sure.
Appreciate if anyone can point me in the right direction.
I discovered that the source of the problem was that my work firewall was blocking access to the Gmail SMTP server. I switched to another SMTP server (set up by our IT department) and email sending now works fine.
Check these two links from the cookbook, to find out how to handle emails while developing: http://symfony.com/doc/current/cookbook/email/dev_environment.html & http://symfony.com/doc/current/cookbook/email/spool.html

Swiftmailer working on localhost but not in production

On my localhost (Linux Mint OS) Swiftmailer is working fine with the following code, but whenever I move it up to my server is just hangs at the send function and then gives me an internal server error.
I currently have my email setup through Google Apps, so maybe SMTP will not work for this situation? If thats the case, how do you suggest I change this from SMTP.
Also, whenever I send an email like this, it is showing up with a from address of the one in the username area. I want it to show up with the from address in the "setFrom" function.
//Include the swiftmailer class
require_once 'comm/swiftmailer/swift_required.php';
//Create a message
//Pass it as a parameter when you create the message
$message = Swift_Message::newInstance();
$message->setSubject('My Subject');
$message->setFrom(array('noreply#domain.com' => 'No Reply'));
$message->setTo(array('me#domain.com' => 'Me'));
$message->setBody($emailContent, 'text/html');
//Create transport class and email the message
$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, 'ssl')->setUsername('useracctname')->setPassword('password');
$mailer = Swift_Mailer::newInstance($transport);
$result = $mailer->send($message);
Thanks a lot for any help!
This might be a port problem on the server. Port 465 could be closed to prevent spamming. It could also be that the server's version of PHP lacks SSL support.
I am wondering though, could it be that I am not using the local mail server so it wont allow SMTP?
If you address gmail as explicitly as you do, it's very unlikely you're using another transport type or a different server.
Also, what about using one of the other transport types?
I think SMTP is your only option to get it running with Gmail. It could, however, be that your server is providing a mail() based service (obviously with a different sender address than GMail though). In that case, you may be able to use Swiftmailer's mail transport.
I had the same problem : I was able to send gmail email in local (after updating that configuration : https://www.google.com/settings/security/lesssecureapps ) but not in production.
To fix my problem I've logged to my gmail account with my production ip / from my production server and answer the gmail security question.
One of the solution to log to your gmail account with your production ip, is to
open an ssh tunnel (ssh -2NfCT -D 5000 yoursshuser#yourdomain.org)
configure you browser to use that proxy (eg Firefox :to Preferences > Advanced > Network > Settings > Manual proxy configuration > SOCKS Hosts: localhost / Post: 5000 / SOCKS v5 )
log to you gmail account as usual
The error outputed by Symfony was :
app.ERROR: Exception occurred while flushing email queue: Failed to authenticate on SMTP server with username "joe.doe" using 1 possible authenticators [] []
well, here is the solution.
if you are sending SMTP mail you should create email account at the server then use this email info at your code.
the server will not send the email if you don't have valid email account to send using it
when you use "from".
this should be a true correct email account a fake one.
the only possible reason to send using fake account is using mail() function.

Categories