MyBB Community Forums

Full Version: direct use of my_mail() function vs mailqueue
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
In one of my plugins I am using the mailqueue to send emails on new posts, however I need to send them with a reply-to header set to the poster email.

One issue is that the send_mail_queue() routine overwrites the Reply-To: header I set in the headers attribute of the mail queue I insert. This occurs in the set_common_headers() function of the mail handler.

So that leaves me with using the my_mail function directly as it supports a $return_email parameter. However, I use this function, the message body looses all the new lines and just shows \r\n\ in the message body.

I find this odd since I am sending the same $db->escape_string($post['message']); variable to both the my_mail() function as well as inserting it into the mail queue

This gives me the correct reply-to, but leaves \r\n as text in the message without any line breaks:
			my_mail($db->escape_string($notifyuser['email']), 
					$db->escape_string($emailsubject), 
					$db->escape_string($emailmessage), 
					$db->escape_string($post['username'])."<".$db->escape_string($post['email']).">",
					"", 
					"",
					false,
					"text", 
					"", 
					$db->escape_string($post['email'])
					);

this gives me the correct message body with line breaks, but overwrites the reply-to:
			$new_email = array(
				"mailto" => $db->escape_string($notifyuser['email']),
				"mailfrom" => $db->escape_string($post['username'])."<".$db->escape_string($post['email']).">",
				"subject" => $db->escape_string($emailsubject),
				"message" => $db->escape_string($emailmessage),
				"headers" => 'Reply-To: '.$db->escape_string($post['email'])
			);
			$db->insert_query("mailqueue", $new_email);

I can not see where in the my_mail() process that the message is getting double escaped.

Maybe I am just missing the obvious. Can anyone help?
Would nl2br() not work, to convert the \r\n characters to <br />? Not sure if they'll display as raw-text or HTML inside the email though. E-mail formatting isn't really one of my strong points.

I also looked through the my_mail and send_mail_queue functions and I can't see why the reply-to header isn't getting passed along, but I've never really used MyBB's mail functions so maybe I'm missing something.
(2012-02-17, 08:20 PM)Beardy Wrote: [ -> ]Would nl2br() not work, to convert the \r\n characters to <br />? Not sure if they'll display as raw-text or HTML inside the email though. E-mail formatting isn't really one of my strong points.

Since I want to send as plain text, it will just display as <br />

(2012-02-17, 08:20 PM)Beardy Wrote: [ -> ]I also looked through the my_mail and send_mail_queue functions and I can't see why the reply-to header isn't getting passed along, but I've never really used MyBB's mail functions so maybe I'm missing something.

In the mail handler is a function called set_common_headers() that tests for a $return_email and uses that as the reply to, if that is not provided it uses the mybb setting for email, but it does this AFTER inserting the headers I specify basically overriding the Reply-To I specify.

But I can not specify the $return_email value when using the built-in mail queue.

Try using htmlspecialchars() on the message variable instead of $db->escape_string.

Actually, that'll display some characters weird in the e-mail since it won't be displaying HTML. strip_tags() might be better.

I did something like this with strip_tags() and received the e-mail fine:


$message = strip_tags("asd
message on a new line

blank line above<br />");

my_mail(
	"[email protected]", 
	"This is a subject", 
	$message, 
	"John Doe"."<[email protected]>",
	"", "", false, "text", "", 
	"[email protected]"
); 

[Image: a7YE8.png]
the weird thing is that I already use mybb's htmlspecialchars_uni on the message and it works fine when going through the mail queue.

there is something different about how send_mail_queue() processes the message as compared to a direct use of my_mail, which send_mail_queue uses as well.
$db->escape_string() probably shouldn't be used when you don't actually put it into a database.

Apart from that you have a space missing before < in the from address.

Other than that I don't see anything out of the ordinary, but I didn't test it myself. Maybe you should add a debug in the my_mail function directly and log the exact parameters its called with and see if you can find a difference this way
(2012-02-17, 10:24 PM)frostschutz Wrote: [ -> ]$db->escape_string() probably shouldn't be used when you don't actually put it into a database.

Apart from that you have a space missing before < in the from address.

Other than that I don't see anything out of the ordinary, but I didn't test it myself. Maybe you should add a debug in the my_mail function directly and log the exact parameters its called with and see if you can find a difference this way

maybe the $db->escape_string is causing it. I just want it to be safe if the post message has html in it. i guess the htmlspecialchars handles that though.

i will try it again
(2012-02-17, 11:31 PM)pavemen Wrote: [ -> ]maybe the $db->escape_string is causing it. I just want it to be safe if the post message has html in it. i guess the htmlspecialchars handles that though.

i will try it again

If you don't mind using the my_mail function directly after a post has been created (instead of trying to find a solution for the send_mail_queue() problem), my earlier post should work for you.

htmlspecialchars() will display certain characters (<, >, etc) in HTML ASCII, and since the e-mail is being sent as plain-text they won't be displayed properly.

Using strip_tags() will only remove HTML tags (<a></a>, <br />, <hr />, etc) and won't affect single characters.
i think you want to send email with some headers and <br /> or \n to create new line.

i needed in my email and i did it as following:
$admin_email_message = 'this is test <br /> and you must see a new line in your email.';

$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
my_mail($from, $admin_email_subject, $admin_email_message , $from , '' ,$headers);


now my email is showing in html mode and <br /> is working fine