My task is to create an online store. When the user has added goods to the basket and begins the process of placing an order, he/she and one of the managers will be sent an email about the order using Swiftmailer.
For some reason only the email to the manager arrives; the one to the user does not.
This is my code:
public static function mailOrder($order_id, $user_email){
$transport = (new Swift_SmtpTransport(App::$app->getProperty('smtp_host'), App::$app->getProperty('smtp_port'), App::$app->getProperty('smtp_protocol')))
->setUsername(App::$app->getProperty('smtp_login'))
->setPassword(App::$app->getProperty('smtp_password'))
;
$mailer = new Swift_Mailer($transport);
ob_start();
require APP . '/views/mail/mail_order_admin.php';
require APP . '/views/mail/mail_order_user.php';
$bodyAdmin = ob_get_clean();
$bodyUser = ob_get_clean();
$message_client = (new Swift_Message("Your order №{$order_id} on " . App::$app->getProperty('shop_name')))
->setFrom([App::$app->getProperty('smtp_login') => App::$app->getProperty('shop_name')])
->setTo($user_email)
->setBody($bodyUser, 'text/html')
;
$message_admin = (new Swift_Message("Сделан заказ №{$order_id}"))
->setFrom([App::$app->getProperty('smtp_login') => App::$app->getProperty('shop_name')])
->setTo(App::$app->getProperty('admin_email'))
->setBody($bodyAdmin, 'text/html')
;
$result = $mailer->send($message_client);
$result = $mailer->send($message_admin);
unset($_SESSION['cart']);
unset($_SESSION['cart_qty']);
unset($_SESSION['cart_sum']);
unset($_SESSION['cart.currency']);
$_SESSION['success'] = 'Thank for Order';
}
From the error message you posted, it would suggest that whatever is returned from App::$app->getProperty('admin_email') is not an email address – it may be returning the property name as the email address, which is no use to anyone. Check it with:
var_dump(App::$app->getProperty('admin_email'));
and then make sure you have the correct value in your config.
Related
Since today, many of my php applications can not send email using SwiftMailer and different Mandrill accounts.
I've got this code, and the send function in the last if stop the script..
// Instance message
$message = Swift_Message::newInstance();
$message->setSubject("subject")
->setFrom(array('noreply#email.test' => 'test'))
->setTo(array('a_valid_email' => 'name'))
->setBody("test", 'text/html')
->setPriority(2);
$smtp_host = 'smtp.mandrillapp.com';
$smtp_port = 587;
$smtp_username = 'valid_username';
$smtp_password = 'valid_password';
// SMTP
$smtp_param = Swift_SmtpTransport::newInstance($smtp_host , $smtp_port)
->setUsername($smtp_username)
->setPassword($smtp_password);
// Instance Swiftmailer
$instance_swiftmailer = Swift_Mailer::newInstance($smtp_param);
$type = $message->getHeaders()->get('Content-Type');
$type->setValue('text/html');
$type->setParameter('charset', 'iso-8859-1');
//Here the send function stop event and I did not go inside the if
if ($instance_swiftmailer->send($message, $fail)) {
echo 'OK ';
}else{
echo 'NOT OK : ';
print_r($fail);
}
Thank you in advance to help me to solve this problem..
If your code used to work, and now doesn't work, and you didn't change your code, then it's probably not a problem with your code. Check your Mandrill account validity - sometimes they suspend accounts for suspicious-appearing usage.
A little background,
I'm creating a simple function that emails all users whenever the admin of a blog creates a new announcement. I want to gather all emails using an sql query and inputting them all inside the message body or perhaps looping sending the emails one at a time (though that may seem like it would take longer).
So far this is my code:
public function emailAll()
{
$this->set_mail_settings();
$message = new YiiMailMessage;
$request = Yii::app()->db->createCommand("
SELECT email FROM persons WHERE party_id
")->queryRow();
$message->subject = 'Star Cruises - Login Information';
$message->addTo('sendTo#someone.com');
$message->from = Yii::app()->params['adminEmail'];
Yii::app()->mail->send($message);
exit();
}
private function set_mail_settings()
{
$sysParam = SystemParameters::model()->getSystemParameters(
array("smtp_host", "smtp_port", 'smtp_user','smtp_password')
);
Yii::app()->mail->transportOptions['username'] = $sysParam['smtp_user'];
Yii::app()->mail->transportOptions['password'] = $sysParam['smtp_password'];
Yii::app()->mail->transportOptions['host'] = $sysParam['smtp_host'];
Yii::app()->mail->transportOptions['port'] = $sysParam['smtp_port'];
}
The emailAll() function is called whenever an email is used.
My problem is the $request. I don't know how I would gather all the emails and putting them into the $message->addTo();
UPDATE: I was doing it fine until I reached this point:
public function emailAll()
{
$this->set_mail_settings();
$message = new YiiMailMessage;
$emails = Yii::app()->db->createCommand("SELECT group_concat(email) as em FROM persons")->queryRow();
$email_ids = explode(",",$emails["em"]);
$message->setBcc($email_ids);
$message->setBody('Sample');
$message->subject = 'New Announcement!';
//$message->addTo('blah#blah.com');
$message->from = Yii::app()->params['adminEmail'];
Yii::app()->mail->send($message);
}
private function set_mail_settings()
{
$sysParam = SystemParameters::model()->getSystemParameters(
array("smtp_host", "smtp_port", 'smtp_user','smtp_password')
);
Yii::app()->mail->transportOptions['username'] = $sysParam['smtp_user'];
Yii::app()->mail->transportOptions['password'] = $sysParam['smtp_password'];
Yii::app()->mail->transportOptions['host'] = $sysParam['smtp_host'];
Yii::app()->mail->transportOptions['port'] = $sysParam['smtp_port'];
}
Then I got an error on this line from YiiMail.php:
189 $msg = 'Sending email to '.implode(', ', array_keys($message->to))."\n".
And it stated that : array_keys() expects parameter 1 to be array, null given.
I understand what it wants, but I don't understand WHY this error occurred?
You can get all email ids with use of below query and use BCC instead of To for security reason.
$emails = Yii::app()->db->createCommand("SELECT group_concat(email) as em FROM persons WHERE party_id = $party_id")->queryRow();
$email_ids = explode(",",$emails["em"]);
$message->setBcc($email_ids); // use bcc to hide email ids from other users
$message->addTo("noreply#yourdomain.com"); //as To is required, set some dummy id or your own id.
Im using the below php code to send an email to one address and bcc 2 other addresses. It sends to the recipient fine but I can only get it to send to one of the 2 bcc addresses. (see comments in code for what ive tried)
Oddly enough though, $result comes back as 3 so it seems that its trying to send the second bcc email but it never comes through.
<?php
$tracker='tracking#pnrbuilder.com';
$subject = $_POST['subject'];
$sender = $_POST['sender'];
$toEmail=$_POST['toEmail'];
$passedInEmail=stripslashes($_POST['message']);
$passedInEmail=preg_replace('/ /',' ',$passedInEmail);
require_once('swiftLib/simple_html_dom.php');
require_once('swiftLib/swift_required.php');
$transport = Swift_MailTransport::newInstance();
$mailer = Swift_Mailer::newInstance($transport);
// Create the message
$message = Swift_Message::newInstance();
//turn the meesage into an object using simple_html_dom
//so we can iterate through and embed each image
$content = str_get_html($passedInEmail);
// Retrieve all img src tags and replace them with embedded images
foreach($content->find('img') as $e)
{
if($e->src != "")
{
$value = $e->src;
$newValue = $message->embed(Swift_Image::fromPath($value));
$e->src = $newValue;
}
}
$message->setSubject($subject);
$message->setFrom($sender);
$message->setTo($toEmail);
//this is my problem
$message->setBcc(array('tracking#pnrbuilder.com',$sender));
//as it is above only "sender" gets the email
//if I change it like this:
//$message->setBcc($tracker,$sender);
//only "tracker" gets the email
//same if I change it like this:
//$message->setBcc($sender);
//$message->addBcc($tracker);
$message->setReplyTo(array('flights#pnrbuilder.com'));
$message->setBody($content,'text/html');
$result = $mailer->send($message);
if ($result=3) {
echo 'Email Sent!';
}
else {
echo 'Error!';
}
?>
What is the proper way to do this?
You can find the swiftmailer tutorial here
example:
$message->setBcc(array(array('some#address.tld' => 'The Name'),array('another#address.tld' => 'Another Name')));
Try setting the names for the email addresses and see if it makes any difference.
This ended up being an issue on the server side, I contacted my hosting provider (GoDaddy) who were able to make some changes on their end fixing the problem. Thank you to all those who tried to help!
I want my MediaWiki to send a 'Thank You' mail to the author when he creates a new article.
Is any extension available for this method?
Alternatively: I am creating article from a special page. So it is possible to add my own extension and write an email script. But I am confused how to get the email ID of the author.
Use the UserMailer and MailAddress classes:
global $wgPasswordSender, $wgPasswordSenderName;
$from = new MailAddress( $wgPasswordSender, $wgPasswordSenderName );
$to = new MailAddress( $user );
$subject = "Thank you!";
$text = "This is a test. Blah blah blah...";
$status = UserMailer::send( $to, $from, $subject, $text );
if ( $status->isGood() ) {
// Great, it worked!
} else {
// Something went wrong, deal with it...
// The $status object will have more information.
}
The MailAddress constructor takes either a User object or an address and a name. $wgPasswordSender is the default e-mail address used by MediaWiki for sending password reset e-mails and other such things; you've hopefully configured it in your LocalSetting.php.
I have built a simple PHP contact form that is supposed to send mail trough the Swift-Mailer script.
Problem is I keep getting this error
Uncaught exception
'Swift_RfcComplianceException' with
message 'Address in mailbox given []
does not comply with RFC 2822, 3.6.2.'
Which I guess means I am using an invalid e-mail address. But since I am using myaddress#gmail.com to test the scrip the problem is probably somewhere else. This is my configuration:
Where the mail is sent to:
$my_mail = 'mymail#mydomain.com';
$my_name = 'My Name';
The content of the message:
$name = trim($_POST['name']);
$email = trim($_POST['email']);
$message = trim($_POST['message']);
$date = date('d/m/Y H:i:s');
$ipaddress = $_SERVER['REMOTE_ADDR'];
$content = $message.'\n\nSent on: '.$date.' From: '.$ipaddress;
The function i use to send the mail using swiftmailer:
function send_mail () {
require('/path/to/swift_required.php');
//The means of transport
$transport = Swift_SmtpTransport::newInstance('mail.mydomain.com', 25);
$transport->setUsername('myusername');
$transport->setPassword('mypass');
$mailer = Swift_Mailer::newInstance($transport);
//The message
$mail = Swift_Message::newInstance();
$mail->setSubject('Hello');
$mail->setFrom(array($email => $name ));
$mail->setTo(array($my_mail => $my_name));
$mail->setBody($content, 'text/plain');
//Sending the message
$test = $mailer->send($mail);
if ($test) {
echo '<p>Thank you for contacting us '.$name.'! We will get in touch soon.</p>';
}
else {
echo '<p>Something went wrong. Please try again later.</p>';
}
}
As you can see it is really simple form with three fields, name, mail and message. I also have other validation set up for each of contact form fields, but I think it is of little concern here.
Thank you for the help.
Edit:
Just test with using gmail as the smtp server. Unfortunately it still gives the same exact results.
All your data variables (addresses, names...) appear to be global. Global variables cannot be read from within functions unless you pass them as parameters (the recommended way) or use the global keyword (or the $GLOBALS array).