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.
Related
I'm building a members style site using WordPress. When a user registers their default role is "subscriber" once we manually approve their account we change the user role to "private_event_member", we need to send the user an email to tell them we have changed their role. I found the following code snippet and added it to the functions.php file
function user_role_update( $user_id, $new_role ) {
$site_url = get_bloginfo('wpurl');
$user_info = get_userdata( $user_id );
$to = $user_info->user_email;
$subject = "Role changed: ".$site_url."";
$message = "Hello " .$user_info->display_name . " your role has changed on ".$site_url.", congratulations you are now an " . $new_role;
wp_mail($to, $subject, $message);
}
add_action( 'set_user_role', 'user_role_update', 10, 2);
This failed to send the email as expected. So to be sure I decided to install a plugin called WP Mail Log and then also WP Mail SMTP, I configured the Sendblue SMTP option. I've tested this and all other emails like for example user registration notifications and new orders are being sent and recorded successfully in the logs, these are being received. The above mentioned code however seems to do nothing.
This seems to be a widely used piece of code that should work so can anyone explain to me why this snippet behaves differently from other mail on the server? It doesn't even appear in the sending logs so as far as I can see it's not doing anything at all. Has the set_user_role action I'm hooking into changed? What could be the cause?
Any help much appreciate!
You should use profile_update hook for this. Read more here - https://developer.wordpress.org/reference/hooks/profile_update/
function notify_user_on_role_change($user_id,$old_user_data,$userdata) {
// Getting role before update
foreach($old_user_data->roles as $role):
$old_role = $role;
endforeach;
// error_log(print_r($userdata,true)); // debug
//If we change role send email
if($old_role != $userdata['role']):
$user_info = get_userdata( $user_id );
$to = $user_info->user_email;
$subject = "Profile Updated";
$message = "Hello, your user role is changed to ".$userdata['role']."";
wp_mail( $to, $subject, $message);
endif;
}
add_action('profile_update','notify_user_on_role_change',10,3);
Send notification only if you change to specific user role
function notify_user_on_role_change($user_id,$old_user_data,$userdata) {
// Getting role before update
foreach($old_user_data->roles as $role):
$old_role = $role;
endforeach;
// error_log(print_r($old_role,true)); // debug
//If we change role send email
if($old_role != 'private_event_member' && $userdata['role'] == 'private_event_member'):
$user_info = get_userdata( $user_id );
$to = $user_info->user_email;
$subject = "Profile Updated";
$message = "Hello, your user role is changed to ".$userdata['role']."";
wp_mail( $to, $subject, $message);
endif;
}
add_action('profile_update','notify_user_on_role_change',10,3);
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.
I have written a custom plugin (that creates a custom post type) and allows any user to submit a new post from a form on my website. To prevent bots, I have setup an e-mail confirmation code which they must click, where this changes the post status from Draft to Published.
Unfortunately the wp_mail() code shown below seems to be executing this confirmation URL automatically. As soon as the post is submitted, it is set to Draft until it reaches this code, and then it automatically publishes.
Removing this block however makes everything work as expected. Does anyone have any idea as to the reason and how to fix it?
$confirm_url = site_url(). '/verification?id=' . $post_id . '&hash=' . $hash;
// Send a verification e-mail to the user to confirm publication
$subject = 'Please confirm your Slicer Profile submission';
$body = $confirm_url;
wp_mail( $profile_email, $subject, $body );
This has been resolved, wanted to share the solution for anyone else that may stumble into this themselves. The site_url() was stored in its own variable and the forward slash in the URL string was not properly escaped, which seemed to have been causing the issue.
This has now been updated to the following and works perfect.
$site_url = site_url();
$confirm_url = $site_url. '\/verification?id=' . $post_id . '&hash=' . $hash;
// Send a verification e-mail to the user to confirm publication
$subject = 'Please confirm your Slicer Profile submission';
$body = $confirm_url;
wp_mail( $profile_email, $subject, $body );
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 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).