i tried to send email through swiftmailer
response code 200 ok but still no email recived.
controller code
public function EmaiAction(Request $request)
{
$mailer = $this->get('mailer');
$logger = new \Swift_Plugins_Loggers_ArrayLogger();
$mailer->registerPlugin(new \Swift_Plugins_LoggerPlugin($logger));
$message = \Swift_Message::newInstance()
->setSubject('Hello')
->setFrom('mohd#abc.com')
->setTo('***#gmail.com')
->setBody('This is a test email.');
if ($this->get('mailer')->send($message)) {
echo '[SWIFTMAILER] sent email to ';
} else {
echo '[SWIFTMAILER] not sending email: ' . $mailer->dump();
}
die("email send");
}
output
[SWIFTMAILER] sent email to
config.yml
swiftmailer:
disable_delivery: true
transport: smtp
encryption: ssl
port: 465
auth_mode: login
host: smtp.gmail.com
username: ***#gmail.com
password: passwrd
delivery_address: abc#gmail.com
on doing echo in if/else its going in if condition that means its true.
so why im not recieving emails.
no error logs appears while sending an emails
im using cpanel app is production enviornment.
disable_delivery: true
This disables mail sending for development (See Docs)
Additionally try the swiftmailer config like this
swiftmailer:
transport: gmail
username: ***#gmail.com
password: passwrd
$message = \Swift_Message::newInstance()
->setSubject('Hello')
->setFrom('mohd#abc.com')
->setTo('***#gmail.com')
->setBody('This is a test email.');
$this->get('mailer')->send($message);
Related
I am building this website and on one of the pages I have a contact form from which the user should be able to send a message to us.
.env :
###> symfony/google-mailer ###
# Gmail SHOULD NOT be used on production, use it in development only.
MAILER_DSN=gmail://myEmail.com:myPassword#default?verify_peer=0
###< symfony/google-mailer ###
mailer.yaml :
framework:
mailer:
dsn: '%env(MAILER_DSN)%'
the code that is supposed to send the mail :
/**
* #Route("/contact", name="api_mail")
*/
public function mailAPI(Request $request, MailerInterface $mailer)
{
if($_SERVER["REQUEST_METHOD"] == "POST"){
$name = $_POST["Name"];
$mail = $_POST["Mail"];
$game = $_POST["Game"];
$category = $_POST["Categ"];
$message = $_POST["Msg"];
$email = (new Email())
->from($mail)
->to('myEmail#gmail.com')
->subject('Testing the mail sender from symfony.')
->text($message)
->html('<p> This is a message </p>');
$mailer->send($email);
}
return $this->render('default/index.html.twig', [
'controller_name' => 'DefaultController',
]);
}
}
At first I used mailCatcher and it caught the mails, now I configured the gmail thingy to send the mails via the gmail transport but it doesn't do anything.
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.
I am using laravel 5.4 and configured gmail smtp details for sending emails. Below is code and return expection error.
Email Code:
try {
Mail::raw('Text', function ($message) {
$message->to('lpkapil#mailinator.com');
});
} catch (\Exception $e) {
echo "<pre>";
print_r($e->getMessage());
print_r($e->getCode());
echo "</pre>";
die();
}
Returned Execption Message & Error code
Error Message:
Expected response code 250 but got code "530", with message "530 5.7.1 Authentication required
Error Code:
530
Gmail smtp details used:
MAIL_HOST: smtp.gmail.com
MAIL_PORT: 587
MAIL_ENCRYPTION: tls
username: gmail id
password: password
Please suggest a solution.
This is a easy way to send mails using Laravel.You can use this function on your controller. You have to make a blade template file as example "emailfile.blade.php" with what ever the details you want to show on the email and pass the variables to that blade using the inputs array as I mentioned below.
$inputs = array(
'name'=> 'Your Name',
'address' =>'Your Address',
'company' =>'Your Company',
);
Mail::send('emailfile', $inputs, function ($message) {
$message->from('sender#gmail.com', 'Sender Name');
$message->to('reciver#gmail.com',$name=null)->subject('Mail Subject!');
$message->cc('cc#gmail.com');
});
First I tried send it using telnet:
$ telnet smtp.yandex.ru 25
Trying 77.88.21.38...
Connected to smtp.yandex.ru.
Escape character is '^]'.
220 smtp12.mail.yandex.net ESMTP (Want to use Yandex.Mail for your domain? Visit http://pdd.yandex.ru)
EHLO yandex.ru
250-smtp12.mail.yandex.net
250-8BITMIME
250-PIPELINING
250-SIZE 42991616
250-STARTTLS
250-AUTH LOGIN PLAIN
250-DSN
250 ENHANCEDSTATUSCODES
AUTH LOGIN
334 VXNlcm5hbWU6
bWFpbEBua3QubWU=
334 UGFzc3dvcmQ6
*******
235 2.7.0 Authentication successful.
MAIL FROM:mail#nkt.me
250 2.1.0 <mail#nkt.me> ok
RCPT TO:dev#nkt.me
250 2.1.5 <dev#nkt.me> recipient ok
DATA
354 Enter mail, end with "." on a line by itself
Subject: Q^BP5Q^AQ^B
To: dev#nkt.me
.
250 2.0.0 Ok: queued on smtp12.mail.yandex.net as 6VPPHaRoyW-LYnSwHm7
QUIT
221 2.0.0 Closing connection.
Connection closed by foreign host.
All is ok, I got the mail
Then I setup swiftmailer params:
mailer_transport: smtp
mailer_host: smtp.yandex.ru
mailer_user: mail#nkt.me
mailer_password: *****
mailer_auth_mode: login
mailer_encryption: ~
mailer_port: 25
And create command for send emails:
class SendMailCommand extends ContainerAwareCommand
{
protected function configure()
{
$this
->setName('mail:send')
->setDescription('Send email')
->addOption('to', null, InputOption::VALUE_REQUIRED, 'Destination email address')
->addOption('body', 'b', InputOption::VALUE_REQUIRED, 'Mail body')
->addOption('subject', 'sub', InputOption::VALUE_OPTIONAL, 'Mail title');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$mailer = $this->getContainer()->get('mailer');
$to = $input->getOption('to');
$subject = $input->getOption('subject');
$body = $input->getOption('body');
$message = \Swift_Message::newInstance()
->setTo($to)
->setSubject($subject)
->setBody($body);
$output->writeln('To: ' . $to);
$output->writeln('Subject: ' . $subject);
$output->writeln('Body: ' . $body);
$output->writeln('Result: ' . $mailer->send($message));
}
}
And run it:
$ app/console mail:send --to="dev#nkt.me" --body="Test body" --subject="Test"
To: dev#nkt.me
Subject: Test
Body: Test body
Result: 1
Well, actually not, otherwise I wouldn't have posted the question.
I tried to use ssl, but still not working, what could be the problem?
PS Now i get smtp instead of spool:
protected function execute(InputInterface $input, OutputInterface $output)
{
$mailer = $this->getContainer()->get('swiftmailer.transport.real');
$message = \Swift_Message::newInstance()
->setFrom(['mail#nkt.me' => 'Admin'])
->setTo($input->getOption('to'))
->setSubject($input->getOption('subject'))
->setBody($input->getOption('body'));
$output->writeln(sprintf('Sent %s emails', $mailer->send($message)));
}
But also get error:
[Swift_TransportException]
Expected response code 250 but got code "", with message ""
I got that same error when I sent directly using the transport instead of passing the transport to a swift mailer instance and then sending.
$mailer = new \Swift_Mailer($transport);
$mailer->send($message);
With php mail http://php.net/manual/en/function.mail.php if the mail is sent ok it returns true.
But with my web host the sent rate is 3000/hour and the server will then store 450 e-mails after the 3000 limit has been reached (which is 15% of the 3000 limit).
What I would like confirmation of is that when the php mail function returns true is it coping with these settings. Does the mail server confirm to the mail function that it sent OK or is the mail function 'blind' to that?
Does the mail server say to the function, limit reached email not sent so return false?
Using the mail() function that comes with PHP is not an optimal solution. Use SWIFTMAILER http://swiftmailer.org/ This will use as SMTP service:
Example of code when using swiftmailer as SMTP service:
require_once 'lib/swift_required.php';
// Create the Transport
$transport = Swift_SmtpTransport::newInstance('smtp.example.org', 25)
->setUsername('your username')
->setPassword('your password')
;
/*
You could alternatively use a different transport such as Sendmail or Mail:
// Sendmail
$transport = Swift_SendmailTransport::newInstance('/usr/sbin/sendmail -bs');
// Mail
$transport = Swift_MailTransport::newInstance();
*/
// 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('receiver#domain.org', 'other#domain.org' => 'A name'))
->setBody('Here is the message itself')
;
// Send the message
$result = $mailer->send($message);