block sending emails that are not from a specific domain - php

I would like to filter emails sent. Emails are sent with the PHP mail() function. I would like, without modifying any PHP file if possible, to let emails out only emails that are to a specific domain, and not others. I don't have access to the SMTP server.

Just in case this helps someone ... If the emails are sent after a form is submitted (or similar action), you could change the action attribute of the form html element to point to a new php file that acts as a filter. Once passed (if so) you redirect to the "proper" destination to send the emails. The filtering could be something as easy as:
$good = "*#mydomain.foo, *#localhost";
$good = explode(',', $good);
if (pattern_grep($_POST['email'], $good)) {
// action
}

You should be able to look at the associative array for the "to" field and use the php regex class to match domains that you blacklist.

Related

Preg match fails on some urls - not emails

I have the code below in the submitted form section of a php file. It is meant to catch any emails that contain a url and reject them.
if (preg_match("/(\b(((https?|ftp|file|):\/\/)|www[.])[-A-Z0-9+&##\/%?=~_|!:,.;]*[-A-Z0-9+&##\/%=~_|])/i", $msg)) {
return false;
}
return true;
But I received an email with dozens of lines like this:
[url=http://example.рф]шкафы купе[/url]
I tried sending a message with one of the lines in the original email and the code blocked it. Why didn't it stop this spammer?
Based on the replies, which I deeply appreciate, I changed the code to the following. All I really want is to block url's. It doesn't matter what the path and parameters are so I think this catches all possibilities. This is used instead of php filters because the filters page says it can't catch URN's.
if (preg_match("/(\b(((https?|ftp|file|[-A-Z0-9]|):\/\/)|www[.]))/i", $msg)) {

Change hyperlink when sending group mails

When sending a group mail about assigning to a newsletter, I need to create a hyperlink button which redirects to: example.com/action.php?id=11 ... email=<%email>
I need to change the <%email> area depending on the recipient's email...
I've done some research, but haven't come up with any solution.
Thanks for any response.
assign variable in code eg
$email = "someone#somewhere.com";
include the variable with the string eg.
echo "example.com/action.php?id=11&email=$email";
You can keep something like <%email> as placeholder text in mail body of each mail text in group mail and later you can replace this with your original email by using str_replace() function when you actually sending mail in your loop before sending mail.
provide complete code you are trying to do so we can help you more with it.

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.

Is it possible to retrieve emails sent by php mail() on the server?

I am using PHP's mail() function to send emails from my application. Is there any way to see the emails that have been sent if I SSH into the server? I need the actual body of the emails not just a record of the action. This is probably a longshot but any help would be appreciated.
EDIT: I should have been clearer. This is for messages already sent in the past. I can definitely take steps to log or otherwise report sent messages in the future but is there a way to retrieve messages that have already been sent?
The mail function doesn't append mails to a Sent mailbox in the server, so I'd say no: as it is you can't find the body of emails sent through PHP.
However you might create your own mail function to log somewhere the content you're sending.
Also there are 2 lines of config in php.ini which makes me think that you could append it to an existing mailbox with the right config.
;sendmail_path =
; Force the addition of the specified parameters to be passed as extra parameters
; to the sendmail binary. These parameters will always replace the value of
; the 5th parameter to mail(), even in safe mode.
;mail.force_extra_parameters =
I usually add an argument to my mail function to indicate that I want to write a record of the email along with the content into an HTML file. That way, when I am testing, I can browse to a temp folder and see what the email would have looked like. Here's some mock code:
function send_email($to=false,$message=false,$from=false,$test=false) {
... do stuff to send email ...
if ($test == true) {
$out = 'Mail to: '.$to.'<br />From: '.$from.'<br />Time: '.date('m/d/Y h:i', time()).'<hr />'.$message;
// use a random string for a file name
$fname = random_string(20);
.. write the $out var to a file named $fname ...
}
}
If you have access to the email address that this is being sent to, then you can use PHPs IMAP functions.
http://www.php.net/manual/en/book.imap.php

batch email send with custom content

I want to send out batch mails using SwiftMailer but just wondering what the best option for this would be. The problem is the email content needs to be customized, i.e. there will be a salutation at the top, and a custom link.
Here is my current OOP code:
foreach($suppliers as $supplier)
{
$quote=new Quote();
$quote->enquiry_id=$enquiry->id;
$quote->supplier_id=$supplier->id;
if($quote->save())
{
$supplier_emails[]=$supplier->email;
}
}
$message=new SwiftMailMessage;
$message->setTo($supplier_emails);
$message->setFrom($params['adminEmailFromAddress'] => $params['adminEmailFromName']);
$message->setBody('Here is the message itself')
App::app()->mail->batchSend($message);
I am using a container for SwiftMailer. So as you can see, I can easily specify an array of email address to send the message to. How can I now customize the content? The variables I need to include in the content are $supplier->name and $supplier->link.
Personally I can't see how this can be done, other than sending each email individually in the foreach() loop. IF that is the case, then is it not better to just use the internal PHP mail() function?
You could try using the Decorator plugin:
http://swiftmailer.org/docs/decorator-plugin

Categories