setCC : Swift Mailer and Mandrill - symfony

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')

Related

How to assign a SendGrid category with Contact Form 7 and WP Mail SMTP

I'm trying to assign a SendGrid category on the emails sent by a form.
SendGrid documentation mentions the use of X-SMTPAPI, it says it should contain a json object in there, and adding {'category': 'cat1'} is supposed to do the trick.
This is what I've tried.
As WP Mail SMTP uses the v3 API to send mail, you need to add your custom categories to the request body.
You can do this with the following piece of code:
function wp_mail_smtp_add_cat( $body, $mailer ) {
$body['categories'] = array('testcat');
return $body;
}
add_filter( 'wp_mail_smtp_providers_mailer_get_body', 'wp_mail_smtp_add_cat', 10, 2 );
For other parameters, check the v3 documentation: https://sendgrid.com/docs/API_Reference/Web_API_v3/Mail/index.html

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.

Sending mail to thousands of customers at a time ( wp_mail -Wordpress)

I want to send a total of 1650 mails on Christmas eve from a WordPress website.
This is my current code:
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type: text/html; charset=" . get_bloginfo('charset') . "" . "\r\n";
$customers = get_users($args);
$cus_arr = array();
foreach($customers as $customer){
array_push($cus_arr,$customer->data->user_email);
}
wp_mail($cus_arr, $subject, $message, $headers);
/*another way*/
...
foreach($customers as $customer){
wp_mail($customer->data->user_email, $subject, $message, $headers);
}
Question 1 : Is the above way a good choice for sending a mail to this amount of customers? May there be any problem? Is there any better way of doing it?
Also I dont want customers to see each others mail. This is my current code:
wp_mail(array("BCC: xyz#xyz.com","BCC: abc#abc.com"), $subject, $message, $headers);
But it does not work. Without BCC: the mails are actually sent.
Question 2. How can I prevent users from seeing other mail ids?
Question 1: It's good practice to use a rate limit(throttling), e.g. max 100 mails/10 minutes. You can do this by storing the recipients in a database, and use a cronjob to send mail every 10 minutes. Another option is to use a 3rd party mail service, such as Mandrill or Sendgrid, they'll handle throttling for you.
Question 2: Your current code calls wp_mail for each recipient, so they'll never see other mail addresses (no need for BCC headers). However if you decide to use wp_mail to send to multiple recipients in 1 call, you need to use bcc headers, e.g.:
wp_mail('', $subject, $message, array("BCC: xyz#xyz.com","BCC: abc#abc.com"));
Hey there I think your code is a bad idea because of the following reasons:
The script may exceed the max execution time or the memory limit of the server
it may work, but wp_mail is just not a good solution for sending this huge amount of emails
What I would recommend is to use a library for sending bulk e-mails. There are existing WordPress Plugins for this purpose however you can easily do it yourself - here is my solution with the classic, well tested PHPMailerLibrary (https://github.com/PHPMailer/PHPMailer):
In your functions.php:
require("libs/phpmailer/PHPMailerAutoload.php");
/**
#param $from: The senders E-Mail
#param $from_name: The senders Name
#param $subject: The E-Mail subject
#param $mesesage: The E-Mail content HTML
#param $to: An array of receivers
*/
function my_custom_send_mass_mail($from,$from_name,$subject,$message,$to) {
// first use PHPMailer to send all the emails
$email = new PHPMailer();
$email->isSMTP();
$email->CharSet = 'utf-8';
$email->From = $from;
$email->FromName = $from_name;
$email->Subject = $subject;
$email->Body = $message;
$email->IsHTML();
if(is_array($to)) {
foreach($to as $t) {
$email->addBCC($t);
}
}
$ac = time();
update_option('mailsent-'.$ac,$email);
$success = $email->send();
if($success) {
update_option('mailsent-suc-'.$ac,"yes");
return "Bulk E-Mail successfully sent.";
}
update_option('mailsent-suc-'.$ac,$email->ErrorInfo);
return "Bulk E-Mail Error: Please contact admin.";
}
Just call this function and make sure $to is the array of receiver e-mails.
This is the solution I am using for a customer sending a 10000 E-Mail newsletter weekly and it works great so far.

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');

Sendmail and gnupg

I am looking for a way to configure wordpress on my RHEL4 machine to send out email notifications using gnupg (sendmail).
Can anyone point me in the right direction?
WordPress uses it's own function wp_mail() to send out emails. Since it is a 'pluggable' function, you can override it with your own version in a WordPress plugin;
/**
* Plugin Name: GNUPG Mail
* Description: Send mail using GNUPG.
*/
function wp_mail($to, $subject, $message, $headers = '', $attachments = array())
{
// use the PHP GnuPG library here to send mail.
}

Resources