Laravel Swift Mailer Exception for bad email address? - php

I am having a super strange issue, I will explain in detail why it is strange after the facts...
The code...
Mail::send('emails.cron.loggedinusers', $data, function($message){
// Get Admin user emails
$adminUserEmails = DB::table('users')->where('user_role', 'admin')->lists('email');
// Build nice comma separated list of Admin Emails addresses
// and also wrap each email in quotes
$filter = function($adminUserEmails){ return "'$adminUserEmails'"; };
$toEmails = array_map($filter, $adminUserEmails);
$toEmails = implode(', ', array_values($toEmails));
print_r($toEmails);
// Send email to Admins Only
$message->from('one#email.com', 'Jason');
$message->to('one#email.com')->cc($toEmails);
$message->subject('[TIMECLOCK ALERT] Users are logged into the Timeclock!');
});
When I run this code above which gets 4 Email address from the database and tries to email to them, I get this nasty error message...
Keep in mind I have changed the actual email values shown here to protect there identity but the REAL emails in the script are all legit emails...
Swift_RfcComplianceException
Address in mailbox given ['one#email.com', 'two#email.com',
'three#email.com', 'four#email.com']
does not comply with RFC 2822, 3.6.2.
Now if I replace the variable $toEmails with the actual string text...
$message->to('one#email.com')->cc('one#email.com', 'two#email.com', 'three#email.com', 'four#email.com');
Then it sends the emails without an issue and no error messages!
This is strange and weird to me because I am literally printing the $toEmails variable to screen and copy/pasting it's output into the email code above and it mails just fine but as soon as I use the variable instead of the text string, I get that awful error message above. I cannot make any sense of why this would do this, please if you have any ideas please share with me???

The error message you get is a little misleading here and perhaps worth a bug-report. RFC 2822, 3.6.2. is about originator fields while to and cc aren't such an originator field, from would be.
However the other part of the message is telling exactly what the problem is:
Address in mailbox given ['one#email.com', 'two#email.com', 'three#email.com', 'four#email.com'] does not comply ...
And that's correct. It's not a mailbox, it's a mailbox-list. Swift mailer expects a mailbox here, not a list of mailboxes. And that's what the message is telling you. You failed to provide a mailbox here. You perhaps thought you could provide a list of mailboxes here, but obviously that gave you the exception so this won't work.
And you already did find the solution, by not providing a list but by adding one mailbox after the other, one per parameter.
This is more a question here what the interface of the $message class expects than anything else. And as the misleading part of the error message was not noted by you, I guess you were not mislead by that part.

Related

Weird error when trying to send MAIL Laravel. Address in mailbox given [] does not comply with RFC 2822, 3.6.2

So i'm trying to send an email to a fixed email address
Here is the code :
//This piece of code is executed when i press a button inside an HTML form
$announcement = $request->input("announcement"); //This comes from an HTML text input
$subject = "Hello this is the title of the email";
$data = [
'email_body' => "This is the body of the email where the message is displayed : <br><br>".$announcement
];
Mail::send('emails.email_view', $data, function ($message) use ($subject) {
$message->from(env("MAIL_FROM_ADDRESS"), 'Some name');
$message->to("validEmail#gmail.com")->subject($subject);
});
When this piece of code is executed it shows me the error mentioned in the title :
Address in mailbox given [] does not comply with RFC 2822, 3.6.2.
The thing is when i refresh the page(url) where the error is displayed it obviously prompts me to Confirm Form Resubmission and when i confirm, the code is executed without a problem , sending the email as it is supposed to do in the first place.
I've tried storing the email in a variable and using trim() or str_replace() to clear the string from possible whitespace, i've tried sending it inside the to() function as an array as mentioned in a question about the same problem still no luck.
Also the code 1 out of 40 tries doesn't display the error and works as supposed to work.
Im' using laravel 5.5
I'm so lost what should i do, where should i look ?
Someone helped me find the solution so i'm going to post it here in case someone in the future needs this.
Don't use env() helper anywhere except the config files, use the config() helper instead - ensuring you actually have a MAIL_FROM_ADDRESS environment variable defined for config caching, or some sensible default (in config/mail.php)
$message->from(config("mail.from.address"), 'Some name');
Documentation link.

PHPMailer do not send if email is invalid

I'm using PHPMailer to send emails via contact form.
When I enter an invalid email address in the input field, the email is still sent. I want to perform both client-side and server-side validations.
Does PHPMailer have a built-in validation system to check for invalid email address? If the email entered is invalid I want to return an error and not send the email.
The easiest and most correct way to validate email addresses is to use filter_var. Rather than relying on a patched PHPMailer, you could write a function to validate them before you send them to PHPMailer.
function validateEmailAddr($addr) {
// note: do not attempt to write any regex to validate email addresses;
// it will invariably be wrong
return filter_var($addr, FILTER_VALIDATE_EMAIL);
}
You said that you could enter something like 'sdjfygsdfisdf' and still get the email sent to you.
That's odd. Because adding any email address ('to', 'cc', 'bcc', 'replyto') in PHPMailer will go through the addOrEnqueueAnAddress() function, which does include validation checks. Adding a 'from' address uses different code, but also does validation checks.
The most obvious thing here is that you're not actually doing any error checking to trap for those errors.
Depending on whether you've got PHPMailer using exceptions or not, you might just be getting a false value returned from functions like setFrom() when you give it a bad address. If you ignore that value and carry on anyway, then yes, the email will still be sent.
So you need to add some error handling. Check for function call returning false.
However my preferred suggestion would be to switch to using exceptions for your error handler -- PHPMailer can do this just by setting a flag. This will make error handling easier, as you won't need to check for false on every single function call; just wrap the whole thing in a try catch block, and do your error handling in one go at the end.

How to remove X-CMAE-Envelope from php mail

xample#gmail.com
X-CMAE-Envelope: MS4wfP8FXd8/R+a/LSU6TL5fZ2U9j6XNOlqH2ChNeZRC9M65GyLWs79yxh/WSVP1mWgmTrSR1jubA85EorlFhPmvIANJv+g8Dvba+4+i5Epzjt6Q3cuOetV2
yQT63E6PAR3l9SpC0BsxP9MXrvBLXdYDMIrGANJWNZNOR8b5focPdjP4
[Mail Message]
Whenever I send a mail using PHP, X-CMAE-Envelope is automatically adding in mail body. How can I remove it?
It has been a while ago since this was asked, but if someone else gets the same problem:
I noticed that the envelope message disappears when you start the message body with an extra empty line.
\r\n\r\n Marks the end of the headers and start of the message body.
CMAE stands for Cloudmark Authority Engine and is an e-mail security product (anti-spam). This and others headers are added by this software at a server level. You should check it with your sysadmin.
https://www.cloudmark.com/en/knowledgebase/cloudmark-authority-for-spamassassin/Order-of-startup-for--CMAE-spamd-and-the-MTA
This is not a direct answer, but I didn't find much on the web for this.
While using Python sending an email through gmail to a user#vzpix.com email, it appears the message included the X-CMAE-Envelope based on what the message was. If the message was not only text but included a date and time - then it included the X-CMAE-Envelope otherwise it was not included. With that being said, it appears server-based.
I know this was from a while ago but I recently had the same issue, specifically a colon ":" triggers this. But if you just start your message with \n it fixes it

MODX not sending emails

I'm using modMail class to send custom emails. I have followed the guidelines on MODX site and used the following code which I placed in a snippet:
$message = $modx->getChunk('myEmailTemplate');
$modx->getService('mail', 'mail.modPHPMailer');
$modx->mail->set(modMail::MAIL_BODY,$message);
$modx->mail->set(modMail::MAIL_FROM,'me#example.org');
$modx->mail->set(modMail::MAIL_FROM_NAME,'Johnny Tester');
$modx->mail->set(modMail::MAIL_SUBJECT,'Check out my new email template!');
$modx->mail->address('to','user#example.com');
$modx->mail->address('reply-to','me#xexample.org');
$modx->mail->setHTML(true);
if (!$modx->mail->send()) {
$modx->log(modX::LOG_LEVEL_ERROR,'An error occurred while trying to send the email: '.$modx->mail->mailer->ErrorInfo);
}
$modx->mail->reset();
The snippet has been modified to contain message from custom chunk as well as email addresses have been replaced with the correct ones. The snippet sent email once and never again. I have no idea what causes such behavior which prevents it from sending emails.
I have read that using the reset function $modx->mail->reset(); resets email fields and allows the email to be sent again yet I have a feeling that it causes problem here.
The snippet is called uncached on the page [[!email]]
Does anyone have an idea why the emails are not being sent, even though it worked once?
if there is an error in your chunk or in processing your chunk, modx is never going to get to thepoint where it logs an error. try something like:
if (!$modx->mail->send()) {
$modx->log(modX::LOG_LEVEL_ERROR,'An error occurred while trying to send the email: '.$modx->mail->mailer->ErrorInfo);
}else{
$modx->log(modX::LOG_LEVEL_ERROR,'This mail was sent: '.$message);
}
to see if it logs something. but otherwise what you have there is exactly correct - try to take the $message variable out and send just a string. if it sent mail once, then something else must be wrong. I'd start looking at mail server logs, headers, spam [gmail??] etc.

php mail function cannot send to name.sirname#doman.com ??i

I'm having trouble when sending emails thorough the mail() function.
I have a script that works perfectly fine for an email address like name#domain.com but when the first part of the email is something with a dot like name.surname#domain.com it doesn't work and returns this error :
Warning: mail() [function.mail ]: SMTP server response: 554 : Recipient address rejected: Relay access denied in confirmed.php on line 119
I am using real email address but have changed it in the above example.
Any thoughts - I'm not a php master but surely there is an easy way to send emails to address with a 2 part first section??
Thanks in advance
Ali
It is not PHP's fault. It is your SMTP-server. Check mail log i.e. /var/log/mail.log and see if it puts out anything. My best guess is that your relaying is missconfigured.
If the code below fails with this error, then DeeD is partially correct - but it's not relaying which is broken - the address re-writing rules are completely ^&*(ed up.
<php
mail('name.surname#domain.com','hello','test');
?>
Also try:
However this would be a phenomonally stupid error on the part of the person who set up the MTA. I susepct its much more likely that code elsewhere may be modifying the address before the call to mail(...) or that your analysis is incomplete. If this is the case, then neither of the tests above will return the original error - instrument your code to find out where the address is being changed.
Alternatively, if the MTA really doesn't like a . in the name - go buy a cattle prod for the person who configured it.
C.

Categories