I want to send emails to 100 users but the requirement is I want a list of successful and failed email ids.
I can achieve this by for loop but It is taking time for execution.
I also tried the below code to send bulk emails.
$subject = "Test Email";
$email_to = ['dhaval#test','yasin#yopmail.com'];
$result = Mail::send([], [], function ($message) use($email_to ,$subject){
$message->to($email_to)
->subject($subject);
});
dd( Mail:: failures()); // return empty array
dd($result); // return empty array
By sending this way, a function is not returning any failed emails list.
So my question is how to get list of successful/failed email ids in bulk sending Or Is there any alternate way to send bulk emails and get both list.
use it
$subject = "Test Email";
$emails = ['dhaval#test','yasin#yopmail.com'];
foreach($emails as $email_to){
$result = Mail::send([], [], function ($message) use($email_to ,$subject){
$message->to($email_to)->subject($subject);
});
}
Related
I am trying to create an application where one of the features is to send email to multiple addresses, and it has not been easy. I used a foreach loop, but whenever an error occurs, most especially if the recipient mail address is not valid the application stops.
This is my code
public function sendemails(Request $request){
$email = $this->emailSetUp($request);
$emails = $email->recipients;
foreach ($emails as $value) {
try{
$mail = Mail::to($value)->send(new SendEmail($email));
}catch( \Swift_RfcComplianceException $e){
$pattern = '/[a-z0-9_\-\+\.]+#[a-z0-9\-]+\.([a-z]{2,4})(?:\.[a-z]{2})?/i';
preg_match_all($pattern, $e, $matches);
$match = implode(',',$matches[0]);
$mail_t_del = Email::where('email','LIKE', '%'.$match)->first();
$mail_t_del->delete();
Log::info($match .' '. 'deleted');
$email = $this->emailSetUp($request);
$mail = Mail::send(new SendEmail($email));
}
}
}
How can i
Send the message to multiple recipients and also make it fail proof, by continuing after the exception has been thrown.
How can i track the progress of the application i.e know the emails that has been sent and the on that has been rejected.
You can pass an array of emails.
see here for details
https://laravel.com/api/5.5/Illuminate/Contracts/Mail/Mailer.html#method_to
Also I suggest that you create a custom request to check to make sure that emails are valid before processing.
You can see how to do that here:
https://laravel.com/docs/5.7/requests
- Send the message to multiple recipients
pass all the emails in your mail class
Example:
Mail::to($users)->send(new SendEmail($yourData));
put your codes in try catch
so that you can ignore exception and continue to send other mails
- Track the progress
If you want to get which mails are sent or not
then handle it on catch statement exception and log it in file or DB
To send an email to multiple recipients without displaying the sent email to all recipients, try this:
This method will loop through the emails supplied for form or database. in this example the user IDs come from form requests, then, email is retrieved from the database for selected users.
foreach($request->get_emails as $i => $a){
$user = User::find($a);
Mail::send('emails.template', ['name' => $user['name'], 'body' => $request->body], function ($m) use($user) {
$m->from('info#example.com', 'Sender Name');
$m->to($user['email'], $user['name'])->subject('this is the subject');
});
}
Also, you may choose to display emails to all recipient. To do that, try:
$emails = ['eamil1#example.com', 'email2#example.com','email3#example.com'];
Mail::send('emails.template', [], function($m) use ($emails)
{
$m->to($emails)->subject('This is the email subject');
});
I've posted this question twice in the last two days and I have really run dry of solutions. I'm creating a very simple comment box that when filled out sends the comment in an email to my company's safety department. I have been receiving this 5.5.4 Invalid Domain Error for the last couple days.
It's SMTP and TLS. The port and server name is correct. The server allows for anonymous emails, and does not validate. I'm using the Swiftmailer library so I shouldn't need to script a ELHO/HELO. The only property of the email that's not defined/hard coded is the body of the message. This is the code for my controller (index.php).
// Initialize Swiftmailer Library
require_once("./swiftmailer/lib/swift_required.php");
// Class that contains the information of the email that will be sent (from, to, etc.)
require_once("./classes/EmailParts.php");
// The class that "breaks" the data sent with the HTML form to a more "usable" format.
require_once("./classes/ContactForm.php");
// =====================
// Main Configuration
// =====================
define("EMAIL_SUBJECT", "Safety Concerns");
define("EMAIL_TO", "safetydepartment#company.com");
define("EMAIL_FROM", "safetydepartment#company.com");
// SMTP Configuration
define("SMTP_SERVER", 'exchange.company.local');
define("SMTP_PORT", 25);
function main($contactForm) {
// Checks if something was sent to the contact form, if not, do nothing
if (!$contactForm->isDataSent()) {
return;
}
// Validates the contact form and checks for errors
$contactForm->validate();
$errors = array();
// If the contact form is not valid:
if (!$contactForm->isValid()) {
// gets the error in the array $errors
$errors = $contactForm->getErrors();
} else {
// If the contact form is valid:
try {
// send the email created with the contact form
$result = sendEmail($contactForm);
// after the email is sent, redirect and "die".
// We redirect to prevent refreshing the page which would resend the form
header("Location: ./success.php");
die();
} catch (Exception $e) {
// an error occured while sending the email.
// Log the error and add an error message to display to the user.
error_log('An error happened while sending email contact form: ' . $e->getMessage());
$errors['oops'] = 'Ooops! an error occured while sending your email! Please try again later!';
}
}
return $errors;
}
// Sends the email based on the information contained in the contact form
function sendEmail($contactForm) {
// Email part will create the email information needed to send an email based on
// what was inserted inside the contact form
$emailParts = new EmailParts($contactForm);
// This is the part where we initialize Swiftmailer with
// all the info initialized by the EmailParts class
$emailMessage = Swift_Message::newInstance()
->setSubject($emailParts->getSubject())
->setFrom($emailParts->getFrom())
->setTo($emailParts->getTo())
->setBody($emailParts->getBodyMessage());
// Another Swiftmailer configuration..
$transport = Swift_SmtpTransport::newInstance(SMTP_SERVER, SMTP_PORT, 'tls');
$mailer = Swift_Mailer::newInstance($transport);
$result = $mailer->send($emailMessage);
return $result;
}
// Initialize the ContactForm with the information of the form and the possible uploaded file.
$contactForm = new ContactForm($_POST, $_FILES);
// Call the "main" method. It will return a list of errors.
$errors = main($contactForm);
// Call the "contactForm" view to display the HTML contact form.
require_once("./views/contactForm.php");
I've posted the entirety of my code at Dropbox. There's not much, but I think the problem must lie in the Index.
I am using YiiMail Extension to send mails.
I have my default contact.php file as my view. I am able to send mails for individuals but multiple emails are not allowed here.
mycontroller-
public function actionContact()
{
$model=new ContactForm;
if(isset($_POST['ContactForm']))
{
$message = new YiiMailMessage;
$message->Body=$_POST['body'];
$message->subject = $_POST['subject']
$message->addTo($_POST['email']);
$message->from = "frommail#gmail.com";
if(Yii::app()->mail->send($message) )
echo 'mail sent';
else
echo 'error while sending email';
}
}
I have tried the following too-
foreach ($model as $value)
{
$message->addTo($model[$value]);
}
It does not accept multiple email id's. How can this be resolved?
I have checked code in Yii Mailer. They haven't given such facility. If you like to achieve then you need to extend Yii Mail. It's ultimately useless because looping in your new extended class OR looping in your controller are same.
Regards...
You can pass multiple recipients mail ids as array
$recipients = array('test1#example.com','test2#example.com','test3#example.com');
$message->addTo('test1#example.com');
foreach($recipients as $email) {
$mail->AddBCC($email); // if you want more than one email
}
I am using the codeigniter email class to send emails when the user register to my website or perform some activity inside my website.
The thing is that, with this simple code below I can only send very simple HTML emails. And also I will have to pass the entire HTML message to the function. Is there any way so that I could load the entire template in the function itself and just pass a message which is to be placed inside the template as we do in codeigniter with views.
$message = 'Thank you for your order \n';
$message .= 'We will contact you soon';
$this->email($message);
public function email($message = NULL){
$this->load->library('email');
$this->email->mailtype('html');
$this->email->from('abbbba#gmail.com', 'Sohan Kc');
$this->email->to('abbbbaa#gmail.com');
$this->email->subject('Email Test');
$this->email->message($message);
$this->email->send();
}
Why don't you just do as you already stated? :)
The email classes message method requires a string to be passed. Just load a View and pass the returned string to the method.
public function email($message = NULL){
$this->load->library('email');
$mydata = array('message' => 'I am a message that will be passed to a view');
$message = $this->load->view('my_email_template', $mydata, true);
$this->email->mailtype('html');
$this->email->from('sohanmax2#gmail.com', 'Sohan Kc');
$this->email->to('sohanmax02#gmail.com');
$this->email->subject('Email Test');
$this->email->message($message);
$this->email->send();
}
I send emails but got the html code as body, I had to add the headers to get correct content like below and it will works out.
$this->email->set_header('MIME-Version', '1.0; charset=utf-8');
$this->email->set_header('Content-type', 'text/html');
I want to send emails using Email Class(CodeIgniter) Problem is that
Email is sending so simple in text format...
I want to design email with (html, tables and colors) or What is the advanced-way for Emailing with CodeIgniter.
function signup(){
#stripped out the validation code
#stripped out the db insert code
$data = array(
'some_var_for_view'=>'Some Value for View'
);
$htmlMessage = $this->parser->parse('user/email/signup_html', $data, true);
$txtMessage = $this->parser->parse('user/email/signup_txt', $data, true);
#send the message
$this->email->from('test#webdevkungfu.com', 'CSSNinja');
$this->email->to($this->input->post('email_address'));
$this->email->subject('Account Registration Confirmation');
$this->email->message($htmlMessage);
$this->email->alt_message($txtMessage);
$this->email->send();
}
My problem is now solved with this code found from link below
http://www.webdevkungfu.com/email-templates-with-codeigniter/