how we can send email with multiple email account as sender - php

I want to send email with some accounts to some targets.But when use this code all emails are delivered to first sender account only.
from() just change name of sender in message and it could not change sender account
while(true)
{
$config = array(
'driver' => 'smtp',
'host' => $smtp,
'from' => array('address' => $senders[$p], 'name' =>
$senderName),
'username' => $senders[$p],
'password' => $senderpasses[$p],
'port' => '587',
'encryption' => 'tls'
);
Config::set('mail', $config);
$data = [
'target' => $email[$m],
'text' => $text,
'title' => $title,
'sender' => $senders[$p],
'senderName' => $senderName
];
try {
Mail::send('emails.mail', ['data' => $data], function
($message) use ($data) {
$message->from($data['sender'], $data['senderName']);
$message->to($data['target'])-
>subject($data['titl']);
});
} catch (\Exception $e) {
echo $e->getMessage();
}
$m++;
$p++;
if ($p >= count($senders)) {
$p = 0;
}
if ($m >= count($email)) {
return ($m);
}
}
it send email just with first sender and other users are not used.

Emails are, by definition, sent from a single sender to multiple addresses, so it is not possible to achieve what you are asking for.
You have to send the mail multiple times, one for each sender. May I ask you what is the purpose of this scenario?

Related

Cannot send multiple email Yii2

I working with framework Yii2 and I want send mail with Mailing. 1 email address sent successfully but array email address sent fail and Show error message:
Exception 'Swift_RfcComplianceException' with message 'Address in mailbox given [xxx#gmail.com,xxx#gmail.com] does not comply with RFC 2822, 3.6.2.'
My code send mail:
Yii::$app->mailer->setTransport([
'class' => 'Swift_SmtpTransport',
'host' => 'smtp.gmail.com',
'username' => $valueEmailSystem->username_mail,
'password' => $valueEmailSystem->password_mail,
'port' => $valueEmailSystem->port,
'encryption' => 'tls',
]);
$message = Yii::$app->mailer->compose();
$message->setFrom([$valueEmailSystem->username_mail => 'TEST']);
$str = implode(',', (array)$modelUser);
$message->setBcc($str);
$message->setSubject('Xác nhận yêu cầu');
$strUserName = implode(',', (array)User::getNameById($pk));
foreach ($valueContent as $key => $item) {
$reValue = str_replace("[hoten]",$strUserName,$item->value);
$message->setHtmlBody($reValue);
}
$message->send();
Help me! Thank all.
The problem is that you are imploding the addresses into one string. The setBcc() method accepts one address as string or multiple addresses as array, for example like that:
$message->setBcc(['first#example.com', 'second#example.com']);
Or if you want to specify names
$message->setBcc([
'first#example.com' => 'First Person',
'second#example.com' => 'Second Person',
]);
Documentation of method.

send mail from other configuration if first config fails laravel

I want to create a code such that if one configuration fails to send mail the other config will kick in to send the mail, I have created two configurations, One in the provider which is set on page load the other is a helper function which can be set as we need.
if I set the helper function before sending any mail the function kicks in but if I keep it in try-catch block the helper doesn't work
try {
Mail::to($administrator->email)->send(new ResetPassword($data));
} catch (\Exception $e) {
$mailsenderr = true;
}
if ($mailsenderr) {
$newConfig = new SecondaryMailer;
$newConfig->setmailer();
try {
Mail::to($administrator->email)->send(new ResetPassword($data));
} catch (\Exception $e) {
return [
"status" => 0,
"message" => 'Error sending mail.'
// , "error" => $e->getMessage()
];
}
}
return ["status" => 1, "message" => "Mail sent successflly"];
Figured out how to
Just create a provider and set the code like
$mail = DB::table('mail_variables')->where('mailer_type', 'primary')->where('status', 'active')->first();
try {
$transport = new \Swift_SmtpTransport($mail->host, $mail->port, $mail->encryption);
$transport->setUsername($mail->username);
$transport->setPassword($mail->password);
$mailer = new \Swift_Mailer($transport);
$mailer->getTransport()->start();
} catch (\Swift_TransportException $e) {
$mail = DB::table('mail_variables')->where('mailer_type', 'secondary')->where('status', 'active')->first();
}
$config = array(
'driver' => $mail->driver,
'host' => $mail->host,
'port' => $mail->port,
'from' => array(
'address' => $mail->from_address,
'name' => $mail->from_name
),
'encryption' => $mail->encryption,
'username' => $mail->username,
'password' => $mail->password,
'sendmail' => '/usr/sbin/sendmail -bs',
'pretend' => false,
);
Config::set('mail', $config);

Address in mailbox given [API base URL: ] does not comply with RFC 2822, 3.6.2. laravel

config/mail.php
<?php
return [
'driver' => 'smtp',
'host' => '',
'port' => '',
'from' => ['address' => null, 'name' => null],
'encryption' => '',
'username' => '',
'password' => '',
'sendmail' => '/usr/sbin/sendmail -bs',
'pretend' => false,
];
Usercontroller.php
public function welcome_email_confirmation($user)
{
$data['first_name'] = $user->first_name;
$data['email'] = $user->email;
$data['token'] = str_random(100); // Generate random string values - limit 100
$data['type'] = 'welcome';
$data['url'] = url().'/';
$data['locale'] = App::getLocale();
$password_resets = new PasswordResets;
$password_resets->email = $user->email;
$password_resets->token = $data['token'];
$password_resets->created_at = date('Y-m-d H:i:s');
$password_resets->save();
$data['subject'] = trans('messages.email.confirm_email_address');
Mail::send('emails.email_confirm', $data, function($message) use($data){
$message->to($data['email'], $data['first_name'])->subject
($data['subject']);
});
return true;
}
In this code I am simply doing sign up but I have got this error when click on sign up button Address in mailbox given [API base URL: ] does not comply with RFC 2822, 3.6.2.. How can I solve this problem? Please help me.
Thank You
in some similar situations I realized that the error was fired for sender email address and not destination email address.
In your config/mail.php you are not setting sender email credentials.
Regards.

CakePHP send email

I've got a problem with sending mail using CakePHP. Everythings giong well, but i didn't receive any single mail , i tired to send to 2 different emails .
//WebsitesController.php
App::uses('AppController','Controller');
App::uses('CakeEmail','Network/Email');
class WebsitesController extends AppController
{
public $helpers = array('Html','Form','Session');
public $components = array('Email','Session');
public function contact()
{
$this->set('dane', $this->Website->findById(4));
}
public function contact_email()
{ /* all data is taken from contact.ctp, I debuged all data below and it's correct */
$useremail = $this->data['Website']['useremail'];
$usertopic = $this->data['Website']['usertopic'];
$usermessage = $this->data['Website']['usermessage'];
$Email = new CakeEmail();
$Email->from(array($useremail => ' My Site'));
$Email->to('wigan#mail.com');
$Email->subject($usertopic); // all data is correct i checked several times
$Email->send($usermessage);
if($Email->send($usermessage))
{
$this->Session->setFlash('Mail sent','default',array('class'=>'alert alert-success'));
return $this->redirect(array('controller'=>'websites','action'=>'contact'));
}
$this->Session->setFlash('Problem during sending email','default',array('class'=>'alert alert-warning'));
}
}
//contact.ctp
<fieldset>
<?php
echo $this->Form->create('Website',array('controller'=>'websites','action'=>'contact_email'));
echo $this->Form->input('useremail',array('class'=>'form-control'));
echo $this->Form->input('usertopic',array('class'=>'form-control'));
echo $this->Form->input('usermessage',array('class'=>'form-control'));
echo $this->Form->submit('Send',array('class'=>'btn btn-default'));
echo $this->Form->end();
?>
</fieldset>
all seems to be fine, even if statement in function contact_email is approved.
configuration ( i'm working on localhost, xampp, netbeans 7.4)
public $smtp = array(
'transport' => 'Smtp',
'from' => array('site#localhost' => 'My Site'),
'host' => 'localhost',
'port' => 25,
'timeout' => 30,
'username' => 'user',
'password' => 'secret',
'client' => null,
'log' => false,
//'charset' => 'utf-8',
//'headerCharset' => 'utf-8',
);
try this, you didn't set the config
public function contact_email()
{ /* all data is taken from contact.ctp, I debuged all data below and it's correct */
$useremail = $this->data['Website']['useremail'];
$usertopic = $this->data['Website']['usertopic'];
$usermessage = $this->data['Website']['usermessage'];
$Email = new CakeEmail();
$Email->config('smtp')
->emailFormat('html')
->from($useremail)
->to('wigan#mail.com')
->subject($usertopic); // all data is correct i checked several times
if($Email->send($usermessage))
{
$this->Session->setFlash('Mail sent','default',array('class'=>'alert alert-success'));
return $this->redirect(array('controller'=>'websites','action'=>'contact'));
} else {
$this->Session->setFlash('Problem during sending email','default',array('class'=>'alert alert-warning'));
}
}
Please follow the steps:
step 1: In this file (app\Config\email.php)
add this:
public $gmail = array(
'transport' => 'Smtp',
'from' => array('site#localhost' => 'Any Text...'),
'host' => 'ssl://smtp.gmail.com',
'port' => 465,
'timeout' => 30,
'username' => 'youremail#example.com',
'password' => 'yourPassword',
'client' => null,
'log' => false,
//'charset' => 'utf-8',
//'headerCharset' => 'utf-8',
);
step 2: Add an email template (app\View\Emails\html\sample.ctp)
<body>
<h1>Email Testing: <?php echo $first_name?></h1>
</body>
step 3: Change the code in your method as shown below:
public function send_my_email() {
App::uses('CakeEmail', 'Network/Email');
$Email = new CakeEmail();
$Email->config('gmail'); //configuration
$Email->emailFormat('html'); //email format
$Email->to('receiveremail#ex.com');
$Email->subject('Testing the emails');
$Email->template('sample');//created in above step
$Email->viewVars(array('first_name'=>'John Doe' ));//variable will be replaced from template
if ($Email->send('Hi did you receive the mail')) {
$this->Flash->success(__('Email successfully send on receiveremail#ex.com'));
} else {
$this->Flash->error(__('Could not send the mail. Please try again'));
}
}

getting error Could not send email in cakephp

I've been trying to send emails with Pear on xampp through Gmail, and after spending hours setting it all up and figuring out all the errors I was getting, I thought I was so close, until I started getting this error:
controller action
public function automail() {
App::uses('CakeEmail', 'Network/Email');
$ret_msg = null;
try {
$is_call_email = true;
$subject = "case detail";
$comment = "Ready to Review";
$email_to = "exmaple#gmail.com";
if ($is_call_email == true) {
$email_to = str_replace(' ', '', $email_to);
$email_addresses = preg_split('/[;,]/', $email_to);
$this->log($is_call_email,'bool');
$email = new CakeEmail();
$email->from(array($this->Session->read('Auth.User.email') => $this->Session->read('Auth.User.name')))
->to($email_addresses)
->subject($subject)
->send($comment);
$this->log($subject,'subject');
}
} catch (Exception $ex) {
$ret_msg = $ex->getMessage();
$this->log($ex->getLine(), 'emailError');
}
$this->log('Return msg is = ' . $ret_msg, 'shared');
return;
in email.php
<?php
class EmailConfig {
public $default = array(
'host' => 'ssl://smtp.gmail.com',
'port' => 465,
'username' => 'example#gmail.com', //example#gmail.com
'password' => 'secret',
'transport' => 'Smtp',
'from' => array('exampe#gmail.com' => 'Nam Email'),
'log' => true
);
}
from and to both are same email addresses because i was send in my account for testing...
please help me or any advice for how to send email using cakephp....
You need to specify $email->config. Like:
$email->config('default')
->from(array($this->Session->read('Auth.User.email') => $this->Session->read('Auth.User.name')))
->to($email_addresses)
->subject($subject)
->send($comment);

Categories