Symfony Mailer base64 encoding - symfony

How can we use the setEncoder() function that we used in the old swiftmailer library on the new Symfony mailer? I think setEncoder has been removed from symfony mailer. Every mail sent is automatically quoted-printable.
The transport and email I created normally work fine.
$transport = Symfony\Component\Mailer\Transport::fromDsn($env);
$mailer = new Symfony\Component\Mailer\Mailer($transport);
$message = (new Symfony\Component\Mime\Email());
$message->from("..");
...
$message->html('<p>Lorem ipsum</p>','utf-8');
$mailer->send($message);
But where should we use the base64 encoder value?
$encoder = new Symfony\Component\Mime\Encoder\Base64Encoder();

Related

Contact form 7 not getting email

I'm using contact form 7 and facing a problem with this email id, I'm not getting any mail what we can do for that. I used SMTP also.
could you please suggest me any other option.
have you checked spam folder ? Perhaps emails are in spam for email not get in spam you can use https://wordpress.org/plugins/wp-mail-smtp/
Here is setup documentation https://wpforms.com/docs/how-to-set-up-smtp-using-the-wp-mail-smtp-plugin/.
Please check may be helpful for you.
The best option is using PhpMailer library in php. You can check if the email was sent successfully using php mailer. All you need to do is to take all the library code and add it to your FTP or install it using composer.
Here is a simple example.
<?php
// Import PHPMailer classes into the global namespace
// These must be at the top of your script, not inside a function
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
// Load Composer's autoloader
require 'vendor/autoload.php';
// Instantiation and passing `true` enables exceptions
$mail = new PHPMailer(true);
try {
//Server settings
$mail->SMTPDebug = 2; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = 'smtp1.example.com;smtp2.example.com'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = 'user#example.com'; // SMTP username
$mail->Password = 'secret'; // SMTP password
$mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 587; // TCP port to connect to
//Recipients
$mail->setFrom('from#example.com', 'Mailer');
$mail->addAddress('joe#example.net', 'Joe User'); // Add a recipient
$mail->addAddress('ellen#example.com'); // Name is optional
$mail->addReplyTo('info#example.com', 'Information');
$mail->addCC('cc#example.com');
$mail->addBCC('bcc#example.com');
// Attachments
$mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments
$mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name
// Content
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = 'Here is the subject';
$mail->Body = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';
$mail->send();
echo 'Message has been sent';
} catch (Exception $e) {
echo "Message could not be sent. Mailer Error: {$mail->ErrorInfo}";
}
You can get all the information on PhpMailer here Php Mailer On Github
The PhpMailer is well configured so you don't have to worry about your emails ending up in Spam folder. Also be careful on the number of mails you send periodically to single recipients.

Swiftmailer not sending emails to the delivery_addresses in dev mode

I am having trouble sending emails to the email address specified in config_dev.yml:
swiftmailer:
delivery_addresses: ['dev#example.com']
I am trying to send from a console command:
php app/console ecard:task:run send --env=dev
Console command code:
...
foreach ($emailQueue as $item) {
$transport = Swift_SmtpTransport::newInstance($item['smtp_host'])
->setPort($item['smtp_port'])
->setUserName($item['smtp_login'])
->setPassword($item['smtp_password'])
->setEncryption('ssl');
$mailer = Swift_Mailer::newInstance($transport);
$message = Swift_Message::newInstance()
->setSubject($item['email_subject'])
->setFrom($item['email_from'], $item['email_from_name'])
->setTo($item['email_to'])
->addPart($item['email_template'], 'text/html');
$ret = $mailer->send($message, $failedRecipients);
....
}
The emails are sent to the real recipient addresses, and not the one I specified in config_dev.yml.
If I dump $this->getContainer()->getParameter('kernel.environment') in code, its 'dev'.
I don't get the $mailer this way: $this->getContainer()->get('mailer'), because I want specify/change the smtp settings in cycle - the config is stored in the database, and not in the parameters.yml. Maybe this is the problem?
(Symfony version 2.8.16, Swiftmailer 5.4.5)
If you get your mailer with:
$mailer = Swift_Mailer::newInstance($transport);
then it ignores all the parameters specified in config.yml.
In this case you must set the destination address in code, as you do with the other mailer parameters, something like:
$message = Swift_Message::newInstance()
->setSubject($item['email_subject'])
->setFrom($item['email_from'], $item['email_from_name'])
->setTo($this->getContainer()->getParameter('swiftmailer.delivery_addresses')?:$item['email_to'])
->addPart($item['email_template'], 'text/html');

setCC : Swift Mailer and Mandrill

I am trying to send an email with a CC email address. Unfortunately, it seems that it doesn't work.
To set the CC address, I use the function setCc() like this :
$mailLogger = new \Swift_Plugins_Loggers_ArrayLogger();
$this->mailer->registerPlugin(new
\Swift_Plugins_LoggerPlugin($mailLogger));
/** #var \Swift_Message $message */
$message = \Swift_Message::newInstance()
->setSubject($subject)
->setFrom($from_array)
->setTo(trim($to_email))
->setCc('test#test.com')
->setBody($body, 'text/html');
I receive the email on the Cc address but it doesn't appear as 'Cc'.
Also, in Mandrill dashboard, I can see that two different emails has been sent.
Is there a way to send mail with Cc with SwiftMailer and Mandrill ?
Try to use instead of:
->setCc('test#test.com')
use:
->addCc('test#test.com')

Drupal 7 - Emails are not going out

I'm in need of help for a custom form in which emails are not being sent.
Context: Within Drupal, I have installed the following modules: PHPMailer, SMTP Authentication Support, Mail System and Mime Mail.
Configuring the above modules you have the option to test your configurations and when preforming such tests emails are being sent properly. However, when writing a module for a form, emails are not being sent.
I don't get any type of erros nor message. I just don't get the email.
Here is the snipped of code that I'm using:
function application_form_submit($form, &$form_state) {
$subject = "testing web form";
$body = array();
$body[] = "Mail body";
$send = FALSE;
$mail_message = drupal_mail('application', 'apply-jobs', 'email#gmail.com', language_default(), $params = array(), $from = 'user#test.com', $send);
$mail_message['subject'] = $subject;
$mail_message['body'] = $body;
$mail_system = drupal_mail_system('application', 'apply-jobs');
$mail_message = $mail_system->format($mail_message);
$mail_message['result'] = $mail_system->mail($mail_message);
}
Suggestions?
You've got an odd way of defining optional parameters. This bit:
$from = 'user#test.com'
will evaluate to... nothing
Try changing your drupal_mail() call like this:
$mail_message = drupal_mail('application', 'apply-jobs', 'email#gmail.com', language_default(), array(), 'user#test.com', $send);
I found the solution to my question. The solution is:
The Mail System module allows one to Configure Mail System settings per module, which means that I had to create new mail system for my customized module an indicate the mail system that I want to use. After I did this, all my email are being sent without any problems.
Hope this helps someone, as there is very little information about this.
Thank you all.

guzzle php http client cookies setup

I am trying to migrate from Zend Http Client to Guzzle Http Client. I find Guzzle well featured and easy to use for the most part, But I think it is not well documented when it comes to using Cookie plugin. So my question is how do you set cookies for the HTTP request you are going to make against the server, in Guzzle.
Using Zend Client you would do something as simple as :
$client = new HttpClient($url); // Zend\Http\Client http client object instantiation
$cookies = $request->cookies->all(); // $request Symfony request object that gets all the cookies, as array name-value pairs, that are set on the end client (browser)
$client->setCookies($cookies); // we use the above client side cookies to set them on the HttpClient object and,
$client->send(); //finally make request to the server at $url that receives the cookie data
So, how do you do this in Guzzle. I have looked at http://guzzlephp.org/guide/plugins.html#cookie-session-plugin. But I felt it is not straightforward and couldn't get my head around it. May be someone can help ??
This code should achieve what is asked for, i.e to set the cookies on the request before making guzzle client request
$cookieJar = new ArrayCookieJar(); // new jar instance
$cookies = $request->cookies->all(); // get cookies from symfony symfony Request instance
foreach($cookies as $name=>$value) { //create cookie object and add to jar
$cookieJar->add(new Cookie(array('name'=>$name, 'value'=>$value)));
}
$client = new HttpClient("http://yourhosturl");
$cookiePlugin = new CookiePlugin($cookieJar);
// Add the cookie plugin to the client object
$client->addSubscriber($cookiePlugin);
$gRequest = $client->get('/your/path');
$gResponse = $gRequest->send(); // finally, send the client request
When the response comes back from the server with set-cookie headers you have those cookies available in the $cookieJar.
Cookie jar can also be gotten from the CookiePlugin method
$cookiePlugin->getCookieJar();
Or without cookie plugin
$client = new HttpClient();
$request = $client->get($url);
foreach($cookies as $name => $value) {
$request->addCookie($name, $value);
}
$response = $request->send();

Resources