I'm using PHPMailer and $mail->Send() is returning an error, my problem is when I use this email-string "noreply#pleasenoreply.com" within $mail->SetFrom(), but in the other hand it works fine with almost any other email i.e "hello#hello.com".
After debugging the code I found out that the problem is in the file class.phpmailer.php over the function ValidateAddress(). It seems that the email "noreply#pleasenoreply.com" is not valid by FILTER_VALIDATE_EMAIL nor the preg_match
PHPMailer - class.phpmailer.php - line 550:
public static function ValidateAddress($address) {
if (function_exists('filter_var')) { //Introduced in PHP 5.2
if(filter_var($address, FILTER_VALIDATE_EMAIL) === FALSE) {
return false;
} else {
return true;
}
} else {
return preg_match('/^(?:[\w\!\#\$\%\&\'\*\+\-\/\=\?\^\`\{\|\}\~]+\.)*[\w\!\#\$\%\&\'\*\+\-\/\=\?\^\`\{\|\}\~]+#(?:(?:(?:[a-zA-Z0-9_](?:[a-zA-Z0-9_\-](?!\.)){0,61}[a-zA-Z0-9_-]?\.)+[a-zA-Z0-9_](?:[a-zA-Z0-9_\-](?!$)){0,61}[a-zA-Z0-9_]?)|(?:\[(?:(?:[01]?\d{1,2}|2[0-4]\d|25[0-5])\.){3}(?:[01]?\d{1,2}|2[0-4]\d|25[0-5])\]))$/', $address);
}
}
Why is that possible?? does anyone have any idea what is going on??? why this email "noreply#pleasenoreply.com" is not allow?
my problem is when I use this email-string "noreply#pleasenoreply.com" within $mail->SetFrom()
I don't know why that specific address is being rejected and others aren't, but generally, you need to specify not only a valid E-Mail address as the from address, but one that is handled on the mail server you're sending the message from.
Otherwise, either the sending server is going to deny sending, or the receiving server is very likely to throw away the message as spam.
The usual policy is to specify noreply#yourdomain.com (yourdomain.com being your website domain). On some servers, you need to actually set up that address to be allowed to send mail from it.
Related
I am learning PHP and from what I understand the mail function has a to parameter that needs to comply with a certain string format - php doc. I have read that if I parse an empty string then the mail function will return false (not 1). However, when I try this the mail function never fails. Is there something I have missed?
Code:
<?php
if (mail('', 'mySubject', 'myMessage')) {
echo "Success!";
} else {
echo 'Failure!';
}
?>
The output is "Success!"
Only by removing the argument entirely gets the else statement to execute. Does the to parameter need to be of a certain string format like the documentation states and if not, then how can I get this function to fail? Thanks
The mail() function does not validate the input. It more or less just takes the data and hands it over to the system mailer daemon.
If that handoff was successful, the method returns true.
It it likely that your local mailer daemon will log an error that it couldn't process the email
php mail() function is just a middle-man between your mail daemon and your code.
You need to manually validate email addresses, typically using something like
if( filter_var( $email_address ,FILTER_VALIDATE_EMAIL ) )
{
mail(parameters);
}
else {
// handle error
}
Like #skaveRat said, you'll most probably find something in your email daemon log that this mail was not sent.
I am new to php.
I have used a mail() function in order to send mails.
I know for a fact that the mail function is used just for queuing up the mail to the server and further has no hand in it.
I wanted to ask whether there is any method of knowing as to what was the error if the mail has not been queued to be sent on the server.(Sending of the mail being an entirely different problem).
Considering my code to be:
if(mail($to,$subject,$content,$headers))
{
echo"Sent";
}
else
{
print_r(error_get_last());
echo"not sent";
}
I have seen this question but the answer doesn't solve my problem.
Thanks in advance and apologies if the question appears to be baseless to you.
I am using PHPMailer to send email using a contact form on my web page. I am using the code
$msg = $mail->Send();
echo $msg;
but nothing is printed out.. Obviously I have all my email/domain settings above these lines. Funny thing is that the email is sending correctly, but I get no response (true OR false) from the send method, thus I cannot run an if statement to redirect to another page according to whether the email has been sent or not..
Replace your $mail->Send() call with:
if(!$mail->Send()) {
echo $mail->ErrorInfo;
}
The most likely reason you're getting no output when you echo $msg is because the result is false. When you echo false it will display nothing. Use var_dump instead of echo.
I'm attempting to read an mbox email archive exported from another server locally, via file access, but for whatever reason everything I've tried fails. Is there some magical trick to parse a local file and access it with PHP's built-in IMAP functionality?
You should be able to use PHP's built-in IMAP functionality. Have you tried something like this:
function openLocal($file_path) {
$mbox = imap_open("$file_path",'','');
if (!mbox) {
$errorMsg = imap_last_error(); // do something with the error...
return false;
} else {
return true;
}
}
And call this with the respective correct path:
openLocal('/home/email/temp/mailbox')
I didn't find the accepted answer sufficient, though it did point me in the right direction.
PHP's IMAP library can be used to parse local .mbox files, such as the ones from Gmail exports.
Importantly, the path must be absolute, it cannot be relative to the current folder.
$imap = imap_open($path_to_mbox, '', '');
You can then get retrieve information with imap functions, such as the subject:
$headers = imap_headerinfo($imap, 0); // Second parameter is the message number
$subject = $headers->subject;
I would like to be able to get an array of emails and make sure each email is sent. (e.g. Array > send each email > result) I kind of changed the question here because this is more important, plus I have added a 50 rep. point. Codewise how can I do this?
Apart from still using the mail() function, you probably want to setup a cron job for sending out the mails. For spooling mail send jobs use a separate database table. Or if it's about some sort of mailing list functionality, then a simple recipient list will do.
If you just want to send out a bunch of the same email at once, you
could call implode() on your array of emails to turn it into a string:
$to_string = implode(', ', $to_array);
Or, if you want to try something more complicated, you could use a
foreach loop to cycle through each email and to keep track of
successes and failures:
$success = array();
$failure = array();
foreach ($to_array as $to_email)
{
if (mail($to_email, $subject, $message, ...))
$success[] = $to_email;
else
$failure[] = $to_email;
}
I'm guessing your original question had to do with sending out all these
emails every day or something without necessitating you hitting a
button. If you have ssh access, see what happens if you type:
crontab -e
If you get some sort of error, you will have to speak with your system
administrator about cron. If you get a file, then you can use cron. This
is not a part of your current question, though, so I'll leave it.
The same way. You must have code that sends an email to an email address. Whether they are on the site or not, it is the same code. You just need to know their email address.
EDIT: If you are wondering how you would trigger the email to be sent, maybe you want to schedule it using a cron job, for example send an email every day at midnight.
This says it all, really: http://php.net/manual/en/function.mail.php
You just need an outgoing mail server installed (postfix, exim, sendmail)
Easy way to send an email:
$to = "usermail#test.com";
$from = "my_email#mydomain.com";
$subject = "Hello!";
$contents = "This is an test mail!";
mail($to, $subject, $contents, "From: $from");
If you don't have access to cron jobs then you will probably struggle to run without user interaction.
A common method for dealing with this is to run on every nth page load, or every so-often. This only works if you have a site that's visited about as often as you want to send email. You'll also want to use an ACID-compliant database. Pseudo-code follows.
if (1 == rand(1,100)) { // run once every 100 page loads
$emails = get_emails_to_send();
mark_emails_as_sent($emails);
$results = send_emails($emails);
mark_failures_as_needing_to_be_sent($results);
}
Alternately, you can run it on a timer:
if (time() - get_last_time_run() > $run_at_least_once_every_this_many_seconds) {
$emails = get_emails_to_send();
mark_emails_as_sent($emails);
$results = send_emails($emails);
mark_failures_as_needing_to_be_sent($results);
}
Or you can combine both with a &&. This depends on how often your page gets hit.
If you want to send email more often than you get page hits... too bad ;). Or have an external service call the page every so often.