Swift Mailer: Multi recipients option doesn't work - php

Although there are many same questions, none of the solutions worked for me. Mail gets sent only if there is only one recipient otherwise generates error below. Also I'm only sending internal emails, no external.
Any idea?
CODE:
$to = 'one#example.com, two#example.com, three#example.com';
$transport = Swift_SmtpTransport::newInstance('192.168.whatever', 25);
$mailer = Swift_Mailer::newInstance($transport);
$message = Swift_Message::newInstance('Test')
->setFrom(array('myself#example.com'))
->setTo(array($to))
->setBody('html content goes here', 'text/html');
$result = $mailer->send($message);
echo $result;
ERROR:
Fatal error: Uncaught exception 'Swift_RfcComplianceException' with message 'Address in mailbox given [one#example.com, two#example.com, three#example.com] does not comply with RFC 2822, 3.6.2.' in C:\wamp\www\ssl\swift_emailer\lib\classes\Swift\Mime\Headers\MailboxHeader.php:308 Stack trace: #0 C:\wamp\www\ssl\swift_emailer\lib\classes\Swift\Mime\Headers\MailboxHeader.php(238): Swift_Mime_Headers_MailboxHeader->_assertValidAddress('lukas.borecki#l...') #1 C:\wamp\www\ssl\swift_emailer\lib\classes\Swift\Mime\Headers\MailboxHeader.php(96): Swift_Mime_Headers_MailboxHeader->normalizeMailboxes(Array) #2 C:\wamp\www\ssl\swift_emailer\lib\classes\Swift\Mime\Headers\MailboxHeader.php(60): Swift_Mime_Headers_MailboxHeader->setNameAddresses(Array) #3 C:\wamp\www\ssl\swift_emailer\lib\classes\Swift\Mime\SimpleHeaderFactory.php(60): Swift_Mime_Headers_MailboxHeader->setFieldBodyModel(Array) #4 C:\wamp\www\ssl\swift_emailer\lib\classes\Swift\Mime\SimpleHeaderSet.php(75): Swift_Mime_SimpleHead in C:\wamp\www\ssl\swift_emailer\lib\classes\Swift\Mime\Headers\MailboxHeader.php on line 308
LOOKED AT:
Swift website
swift mailer error 'Swift_RfcComplianceException'....
Google forum
And many more.

Try
$to = array('one#example.com', 'two#example.com', 'three#example.com');
....
->setTo($to)

TRy this example with swiftmailer:
parameters.yml
destinatarios: [email1, email2, email3]
services:
your service name:
class: your namespace
arguments: ["%destinatarios%"]
the class of the service:
protected $destinatarios;
public function __construct($destinatarios)
{
$this->destinatarios = $destinatarios;
}
->setCc($this->destinatarios)

Related

Can't send mail with Gmail

I'm trying to send an email when a user is subscribing in my web site.
To save me time ... I'm using Swift_Mailer with Symfony.
First, I tried to send a mail with the console :
php .\bin\console swiftmailer:email:send
And that's working, the mail was sent and the client received it.
So now I said to myself, I will be able to send emails with the code.
That's my Controller code :
public function register(Request $request, \Swift_Mailer $mailer)
{
// Here saving new user in the database
$this->sendMail($user->getEMail(), $mailer);
$content = $this->renderView("register.html.twig");
return new Response($content);
}
private function sendMail($email, \Swift_Mailer $mailer)
{
$message = (new \Swift_Message('Hello Email'))
->setFrom(['my.email#gmail.com' => "my name"])
->setTo($email)
->setBody('Here is the message itself');
$mailer->send($message);
}
And That's my .env file :
MAILER_URL=gmail://my.email#gmail.com:myPasswordHere#localhost
When I'm trying to send the mail, no error, I tried this StackOverflow link, but no problem, echo "Successfull", but nothing in the Gmail send box and nothing my client email box.
Can you please help me to ... save me time ^^
------------------------------------------------------------- EDIT 1 ------------------------------------------------------------
I never edited the config/packages/swiftmailer.yaml file, that's it's content :
swiftmailer:
url: '%env(MAILER_URL)%'
spool: { type: 'memory' }
------------------------------------------------------------- EDIT 2 ------------------------------------------------------------
I also tryed to make a new transporter with this code :
$transport = (new \Swift_SmtpTransport('smtp.gmail.com', 465))
->setUsername('my.email#gmail.com')
->setPassword('myPasswordHere');
$mailer = new \Swift_Mailer($transport);
And that's the error I get : Connection could not be established with host smtp.gmail.com [php_network_getaddresses: getaddrinfo failed: Unknown host. #0]
------------------------------------------------------------- EDIT 3 ------------------------------------------------------------
I've got a swiftmailer.yaml fil in the config/packages/dev folder and that's it's default content :
# See https://symfony.com/doc/current/email/dev_environment.html
swiftmailer:
# send all emails to a specific address
#delivery_addresses: ['me#example.com']
I tried to put the same content as in the config/packages/swiftmailer.yaml, but no result.
I don't see something like this in your code:
// Create the Transport
$transport = (new \Swift_SmtpTransport('mail hoster etc', port))
->setUsername('your email')
->setPassword('your password');
// Create the Mailer using your created Transport
$mailer = new \Swift_Mailer($transport);
Maybe it's a reason why your mails isn't sent.

Swiftmailer config : send mail using gmail

I can send email from my pc using swiftmailer, but mail not sending in server.
I'm using swiftmailer 5.0.1. Project details are,
A simple php project in netbeans
swiftmailer 5.0.1
twig 1.13.1
My code is
public function init() {
$this->username = 'username#gmail.com';
$this->password = 'password';
$this->host = 'ssl://smtp.gmail.com';
$this->port = 465;
$this->from = 'username#gmail.com';
$this->subject = 'Company - contact';
$this->body_part_type = 'text/html';
}
public function send_email($from_name_add, $to_add, $fullname, $email, $mobile, $content) {
$this->init();
$transport = Swift_SmtpTransport::newInstance($this->host, $this->port)
->setUsername($this->username)
->setPassword($this->password);
$mailer = Swift_Mailer::newInstance($transport);
$message = Swift_Message::newInstance();
$cid = $message->embed(Swift_Image::fromPath('../public_html/pic/logo.png'));
$this->body = $this->renderEmailTemplate('email', $fullname, $email, $mobile, $content, $cid);
$message->setSubject($this->subject)
->setFrom(array('username#gmail.com' => '' . $from_name_add))
->setTo($to_add)
->setContentType($this->body_part_type)
->setBody($this->body);
$result = $mailer->send($message);
return $result;
}
This code works FINE in my pc. But after upload this code/project to server, mail not sending. Error is,
<br />
<b>Fatal error</b>: Uncaught exception 'Swift_TransportException' with message 'Connection could not be established with host ssl://smtp.gmail.com [Connection timed out #110]' in /home/am***/lib/Swift/classes/Swift/Transport/StreamBuffer.php:259
Stack trace:
#0 /home/am***/lib/Swift/classes/Swift/Transport/StreamBuffer.php(64): Swift_Transport_StreamBuffer->_establishSocketConnection()
#1 /home/am***/lib/Swift/classes/Swift/Transport/AbstractSmtpTransport.php(115): Swift_Transport_StreamBuffer->initialize(Array)
#2 /home/am***/lib/Swift/classes/Swift/Mailer.php(80): Swift_Transport_AbstractSmtpTransport->start()
#3 /home/am***/controller/send_mail.php(54): Swift_Mailer->send(Object(Swift_Message))
#4 /home/am***/public_html/contact.php(43): send_mail->send_email('Am*** Inc', 'fe****#gma...', 'asdf', 'asdf#in.com', '111111111111', 'Testing mail')
#5 {main}
thrown in <b>/home/am***/lib/Swift/classes/Swift/Transport/StreamBuffer.php</b> on line <b>259</b><br />
HINT: There is an allready running php symfony2 project in that server, this project can send mail successfully.
Here is the symfony2 code,
$message = \Swift_Message::newInstance()
->setSubject($sub)->setFrom($from)->setTo($to)->setContentType("text/html")
->setBody($this->renderView('FZAm***Bundle:Layout:mail.html.twig', array
('name' => $this->fullname, 'mobile' => $this->mobile, 'email' => $this->email,
'content' => $this->content, 'time' => $this->sys_time, 'ip' => $userip,
'server_time' => date('Y-m-d H:i:s'))
));
try {
$this->get('mailer')->send($message);
// catch and other follows.
Config details are,
mail_contact_from: username#gmail.com
mail_contact_to: username#gmail.com
mail_contact_sub: Contact info
I passed only this details and all settings are default. If any info needed please ask i'l post.
Reason i'm changing from symfony2 project to this ordinary php+swift+twig is my hosting is only 100mb and i need to upload more images. But symfony2 occupy more space.
Looks like your live server is missing OpenSSL, so you need to enable it in order to get secure connections working (i.e. enable the php_openssl module). Also check this question.
After some search in google.
To send mail using gmail auth you need to use this code,
$transport = Swift_SmtpTransport::newInstance('ssl://smtp.gmail.com', 465);
This is the correct way to send mail. This mail will be send by gmail.
PHP use mail() to send just a mail,
mail($to,$subject,$message,$headers);
This code sends a mail using php code.
Swiftmailer can also send a mail using this php mail() function,
Here is swift code.
$transport = Swift_MailTransport::newInstance();
// nothing inside ()
The problem in this mail is, gmail displays following message,
This message may not have been sent by: username#gmail.com
Sometimes this mail will go to spam.

Error 501 by sending mail using Swift mailer

<?php
require_once '../plugin/swift/lib/swift_required.php';
// Create the Transport
$transport = Swift_SmtpTransport::newInstance('pod51003.outlook.com',587,'tls')
->setUsername('user#connect.polyu.hk')
->setPassword('pw')
;
// Create the Mailer using your created Transport
$mailer = Swift_Mailer::newInstance($transport);
// Create a message
$message = Swift_Message::newInstance('Wonderful Subject')
->setFrom(array('john#doe.com' => 'John Doe'))
->setTo(array('foodil#hotmail.com', 'foodil#yahoo.com.hk' => 'A name'))
->setBody('Here is the message itself')
;
// Send the message
$result = $mailer->send($message);
printf("Sent %d messages\n", $result);
?>
It turned out:
Fatal error: Uncaught exception 'Swift_TransportException'
with message 'Expected response code 250 but got code "501", with message "501 5.5.4 Invalid domain name "'
in C:\xampp\htdocs\fyp\plugin\swift\lib\classes\Swift\Transport\AbstractSmtpTransport.php:422
Stack trace:
#0 C:\xampp\htdocs\fyp\plugin\swift\lib\classes\Swift\Transport\AbstractSmtpTransport.php(306): Swift_Transport_AbstractSmtpTransport->_assertResponseCode('501 5.5.4 Inval...', Array)
#1 C:\xampp\htdocs\fyp\plugin\swift\lib\classes\Swift\Transport\EsmtpTransport.php(224): Swift_Transport_AbstractSmtpTransport->executeCommand('HELO [::1]??', Array, Array)
#2 C:\xampp\htdocs\fyp\plugin\swift\lib\classes\Swift\Transport\AbstractSmtpTransport.php(323): Swift_Transport_EsmtpTransport->executeCommand('HELO [::1]??', Array)
#3 C:\xampp\htdocs\fyp\plugin\swift\lib\classes\Swift\Transport\EsmtpTransport.php(272): Swift_Transport_AbstractSmtpTransport->_doHeloCommand()
#4 C:\xampp\htdocs\fyp\plugin\swift\lib\classes\Swift\Transport\AbstractSmtpTransport.php(124) in C:\xampp\htdocs\fyp\plugin\swift\lib\classes\Swift\Transport\AbstractSmtpTransport.php on line 422
Actually I follow the tutorial straightly, so is that any thing I omitted in my code? The SMTP server information is valid.
External SMTP setting:
Server name: pod51003.outlook.com - you can also see the note below on how to determine the server name
Port: 587
Encryption method: TLS
after adding the line: there is another error:
Warning: stream_socket_enable_crypto() [streams.crypto]: this stream does not support SSL/crypto in C:\xampp\htdocs\fyp\plugin\swift\lib\classes\Swift\Transport\StreamBuffer.php on line 102
Fatal error: Uncaught exception 'Swift_TransportException'
with message 'Unable to connect with TLS encryption'
in C:\xampp\htdocs\fyp\plugin\swift\lib\classes\Swift\Transport\EsmtpTransport.php:283
Stack trace:
#0 C:\xampp\htdocs\fyp\plugin\swift\lib\classes\Swift\Transport\AbstractSmtpTransport.php(124): Swift_Transport_EsmtpTransport->_doHeloCommand()
#1 C:\xampp\htdocs\fyp\plugin\swift\lib\classes\Swift\Mailer.php(79): Swift_Transport_AbstractSmtpTransport->start()
#2 C:\xampp\htdocs\fyp\mail\send.php(26): Swift_Mailer->send(Object(Swift_Message))
#3 {main} thrown in C:\xampp\htdocs\fyp\plugin\swift\lib\classes\Swift\Transport\EsmtpTransport.php on line 283
Try adding this line here as shown:
// Create the Transport
$transport = Swift_SmtpTransport::newInstance('pod51003.outlook.com',587,'tls')
->setUsername('user#connect.polyu.hk')
->setPassword('pw')
;
// ADD THIS LINE
$transport->setLocalDomain('[127.0.0.1]');
// Create the Mailer using your created Transport
$mailer = Swift_Mailer::newInstance($transport);
I'm guessing your SMTP server doesn't like/understand IPv6, based on the response you are getting to the HELO command.

I get following error when sending a mail using zend frame work. I also included my code

Fatal error: Uncaught exception 'Zend_Controller_Dispatcher_Exception'
with message 'Invalid controller specified (error)'
in C:\xampp\htdocs\zend\library\Zend\Controller\Dispatcher\Standard.php:248 Stack trace:
#0 C:\xampp\htdocs\zend\library\Zend\Controller\Front.php(954):
Zend_Controller_Dispatcher_Standard->dispatch(Object(Zend_Controller_Request_Http), Object(Zend_Controller_Response_Http))
#1 C:\xampp\htdocs\zend\library\Zend\Application\Bootstrap\Bootstrap.php(97):
Zend_Controller_Front->dispatch()
#2 C:\xampp\htdocs\zend\library\Zend\Application.php(366):
Zend_Application_Bootstrap_Bootstrap->run()
#3 C:\xampp\htdocs\intern\lc.intern\public\index.php(26):
Zend_Application->run()
#4 {main} Next exception 'Zend_Controller_Exception'
with message 'Invalid controller specified (error)'
#0 C:\xampp\htdocs\zend\library\Zend\Controller\Front.php(954):
Zend_Controller_Dispatcher_Standard->dispatch(Object(Zend_Controller_Request_Http), Object(Zend_Controller_Response_Http))
#1 C:\xampp\htdocs\zend\library\Zend\Application\B
in C:\xampp\htdocs\zend\library\Zend\Controller\Plugin\Broker.php on line 336
my code for sending mail is as follows:
$mail = new Zend_Mail();
$mail->addTo('myemail#gmail.com', 'my name')
->setFrom('myemanil#gmail.com', 'my name')
->setSubject('My Subject')
->setBodyText('Email Body')
->send();
when I remove the last line i.e., send() then it works fine , but to send a mail we require send() :-P
I'm stuck here ...
please help me ...
thanks in advance
It looks to me like two things happening here:
The send() method is throwing an exception. Since you are not specifying a $transport object, it is attempting to use standard sendmail as a default. It looks like you are using Windows. Do you have sendmail installed? I, too, get exceptions when sending on my local Windows dev environment. Maybe test with an smtp-based transport?
The thrown exception is triggering the error handler, so it is looking for the ErrorController::errorAction(). The message you see is the result of a missing or incorrectly placed ErrorController.
Just thinking out loud.
First of all, please update to the latest ZF stable branch.
As a quickfix, you should try to replace your code with this :
$mail = new Zend_Mail();
$mail->addTo('myemail#gmail.com', 'my name')
->setFrom('myemanil#gmail.com', 'my name')
->setSubject('My Subject')
->setBodyText('Email Body');
$mail->send();
Depending on the ZF version you are using, method chaining could be the problem here.
try like this
example from my project
$options = array(
'auth' => 'login',
'username' => 'username#gmail.com',
'password' => 'password',
'ssl' => 'tls',
'port' => 587
);
$mailTransport = new Zend_Mail_Transport_Smtp('smtp.gmail.com', $options);
Zend_Mail::setDefaultTransport($mailTransport);
$m = new Zend_Mail('UTF-8');
$m->setHeaderEncoding(Zend_Mime::ENCODING_BASE64);
$m->addTo("username#gmail.com");
$m->setFrom('username2b#gmail.com', 'username');
$m->setSubject('usertext');
$errortext = "report";
$m->setBodyHtml($errortext, 'UTF-8', Zend_Mime::ENCODING_BASE64);
$m->send();
you have to set the default transport for your application. Where you are writing the code, in the constructor of the class, you have to use this.
function __construct() {
$tr = new Zend_Mail_Transport_Smtp(<your smtp IP address>);
Zend_Mail::setDefaultTransport($tr);
}
Note: Along with the above code, please check in your php.ini file of your apache server for the following setting. The below option should be uncommented.
extension=php_openssl.dll

Swiftmailer and Symfony2

Having some problems implementing swiftmailer with the new symfony2 beta4, below is my code
$mailer = $this->container->get('mailer');
$name = ucwords(str_replace('.',' ', $user->getScreenName()));
$email = 'me#me.com'; //$user->getEmail();
$message = $mailer::newInstance()
->setSubject('New Password')
->setFrom('Neokeo <blah#blah.com>')
->setTo("$name <$email>")
->setBody($this->renderView('MyBundle:User:reset.html.php', array('user',$user)));
$mailer->send($message);
and the error
Catchable fatal error: Argument 1 passed to Swift_Mailer::newInstance() must implement interface Swift_Transport, none given
does anyone have any idea what i can do to fix this?
$mailer is an instance of the Swift_Mailer class (which is the class used for sending messages), but for creating a message, you need the Swift_Message class.
$message = Swift_Message::newInstance()
http://swiftmailer.org/docs/message-quickref

Categories