Im using phpmailer to send emails and i check that when i add some addresses and just one of them is an invalid address (not exists) E.G. "asdfasfasf#asdfasdfsfsfs.commm" and send the email i see that the email was sent (to correct addresses) and i have no idea how to check if one of the adresses is wrong to be able to log that issue before sending the email.
The code to send and add addresses is this:
foreach($options['emails'] as $email){
$mmail->AddAddress($email[0], $email[1]);
}
if (!$mmail->Send()) {
echo "error";
}else {
echo "sent";
}
Thanks in advance
Take a look at filter_var to validate the syntax:
if (filter_var($email[0], FILTER_VALIDATE_EMAIL)) {
// email address is considered valid
Note that there are ways to connect to the recipients SMTP server and ask if the email actually exists (see https://code.google.com/p/php-smtp-email-validation/ for example) however many email servers won't honor these queries anymore, due to spammer abuse.
Related
I'm currently working on laravel mail. I've sending email structure with attachment like this:
$document = Document::find($request->document_id);
Mail::send('email.senddocs', array('project'=>$document->project->name,'type'=>$document->documentType->type), function($message) use($request,$document){
$file_name = public_path().'/documents/'.$document->project->p_key.'/'.$document->file_name;
$message->from('us#example.com', 'DDMS');
$message->to($request->email);
$message->attach($file_name);
});
I've already visited here. But, the process over there always returning success.
What I actually want is to know if mail is send or not. In my case, Mail sending can fail due to fake email like akdjbaksdjf#jhbasdhadfs.com or by some other errors occurred.
How can I know mail is sent or not ?
Any help is appreciated
This questions is asked several times here:
Laravel 5 - check if mail is send
You can use the failure method for this:
if( count(Mail::failures()) > 0 ) {
foreach(Mail::failures as $email_address) {
echo "$email_address <br />";
}
} else {
echo "Mail sent successfully!";
}
This only checks if a email was send. You can not handle not existing email adresses with this method.
You can use mailgun for this problem for example.
Another way is to use a php class which connects to a smtp server, and will check.
PHP class:
https://www.phpclasses.org/package/6650-PHP-Check-if-an-e-mail-is-valid-using-SMTP.html
Some Informationen of checking email adresses: How to check if an email address exists without sending an email?
I have a scenario in which I have to add multiple addresses in a carbon copy (CC) field from a PHPMailer E-mail. If an E-mail is not valid, currently mail does not get sent.
I want it so that if an E-mail address is not valid, it will skip adding that E-mail address in $mail->addCC($cc);, and simply send to the other recipients.
Are there any alternative ways to achieve this within PHPMailer? If so, please suggest them to me.
You can send emails to cc-recipients as to main recipients like this:
/* Prepare PHPMailer object */
...
/* Send email to main recipient */
...
/* Form cc array */
$ccs= [
"FirstCC" => "first#mail.com",
"SecondCC" => "second#mail.com"
];
/* Send email to each cc as to main */
foreach ($ccs as $name => $address)
{
$mail->clearAddresses();
$mail->AddAddress($address, $name);
$mail->Send();
}
This way will provide assurance that all valid recipients get email
PHPMailer automatically validates all addresses you give it. The only way you can tell if an address is actually going to be accepted is to try to send to it, and if it fails, don't try to send to it next time. If you want to send to multiple CCs (where all recipients can see all other addresses in the message), do this:
...
$mail->addAddress('me#example.com');
foreach ($ccs as $cc) {
$mail->addCC($cc);
}
$mail->send();
addCC will automatically skip actually invalid addresses (returning false for any that are invalid), but it's not possible to establish whether addresses will be accepted for delivery at this point - you have to attempt to deliver to find out.
I'm trying to email new registered users for email verification (PHP) but i don't get it, why would an email be sent to SPAM, i already checked out similar questions and all answers are about Headers,
It seems a bit complicated for me to get known to those headers and how are they being verified,
By sender website ? lets say i sent as user#google.com and the actual server domain is domain.com, how would it know? and is it one of the main reasons why it goes to spam ?
I am using VPS, does it has anything to do with it ?
I'm just trying to understand the clear/simple reasons of why would an email be checked as spam
and what if i sent from the server IP and not the domain itself
Most of the mail servers will do Reverse DNS lookup to prevent people from domain.com pretending to be from otherdomain.com. It will check if the IP address from which the email was sent resolves to the same domain name of the email sender. Yahoo and other big companies will also use DKIM to verify you.
Often your message can end up in Bulk/Spam if it doesn't have much content, or if you sent a lot of the same content to one server.
Here's a good article about what web developers should know about sending email that might help you understand the subject.
1) Check headers. You could use any email sending library such as PHPMailer (http://code.google.com/a/apache-extras.org/p/phpmailer/wiki/PHPMailer#Documentation_and_Resources)
2) Check hosting server. If your is using shared hosting then most probably it has been blacklisted by the email domain.
Configure an email address on your domain, replace me#mydomain.com with your newly created email address on your domain andid#hotmailOrgmail.com with your Hotmail/Gmail id in the following script.
Also replace Your Name with your name in the following script and test it on your server:
<?php
$myName = "Your Name";
$myEmailAddressonDomain = "me#mydomain.com";
$myPreferredEmailAddresson = "id#hotmailOrgmail.com";
$mail = $_POST['email_field'];
$clientName = $_POST['name_field'];
$subject = $_POST['subject_field'];
$text = $_POST['message_field'];
$headers = 'From: "$name" <$yourEmailAddressonDomain>'.PHP_EOL.'Reply-To: '.$_POST['mail'].PHP_EOL;
$to = '"$yourname" <$myPreferredEmailAddresson>';
$message = $text.PHP_EOL.PHP_EOL."---".PHP_EOL."From: ".$name." <".$mail.">";
/* Server-side form validations */
$err = "Error with ";
if (!checkLen($name)) {
$err.='Name';
} else if (!checkLen($mail) && !checkEmail($mail)) {
$err.='Email';
} else if (!checkLen($subject)) {
$err.='Subject';
} else if (!checkLen($text)) {
$err.='Message';
}
if (strlen($err)>11) {
echo $err.' field';
exit;
}
/* end validations */
elseif (mail($to, $subject,$message, $headers)) {
echo "<span style='color: #336600'>Your message has been sent.</span>";
} else {
echo "An error occurred, please try again.";
}
function checkLen($str,$len=1)
{
return isset($str) && mb_strlen(strip_tags($str),"utf-8") > $len;
}
function checkEmail($str)
{
return preg_match("/^[\.A-z0-9_\-\+]+[#][A-z0-9_\-]+([.][A-z0-9_\-]+)+[A-z]{1,4}$/", $str);
}
?>
The email will land on your Hotmail/Gmail inbox (or any non-spam) folder via your domain's email address.
Note: Clicking Reply in the received email would show you the client's email address (as we have set in Reply-To header above)
Make appropriate changes and you are good to go.
as you are operating VPS, you may consider setting up DKIM and SPF on your server, they are used by mail services like Gmail to classify your server as a legitimate server.
I am pretty new to PHP, How do I validate an email address to have an IBM domain only? Valid address are:
XXXXX#us.ibm.com
XXXXX#in.ibm.com
XXXXX#ro.ibm.com
XXXXX#ibm.com
The PHP should be able to accept any of the above.
Please help
Thanks
Split on # then verify with a regex.
list($username,$domain) = explode('#',$email);
if (preg_match('/ibm\.com$/',$domain))
{
echo "yup it's ibm.";
}
First thing to check, is if the email address you got from the email headers is valid i.e.:
John Adams <john#us.ibm.com>
"John Adams" <john#us.ibm.com>
john#us.ibm.com
Then you can verify if it came from an IBM domain by:
if(preg_match('/#([a-z]{2}\.)?ibm.com/i', $emailaddress)) {
# email is from an ibm domain
}
Finally and this is a hard part, make sure that the email was sent from a server owned by IBM, because even me can send you an email with "arvin#ibm.com" as the email address indicated in the From field.
You can list all the IP addresses of IBM's email servers then check if the email originated from them using the email's header fields. Or if your server does SPF checking you can check if the Received-SPF field of the email is "pass".
I'm running a website with more than 60 000 registered users. Every week notifications are send to these users via email, now I've noticed some of the mail addresses do not exists anymore eg. the domain address is valid but the email name en asdas# is not valid anymore since person does not work at a company anymore etc. Now I'm looping through the database and doing some regular expression checks and checking if the MX records exist with the following two functions
function verify_email($email){
if(!preg_match('/^[_A-z0-9-]+((\.|\+)[_A-z0-9-]+)*#[A-z0-9-]+(\.[A-z0-9-]+)*(\.[A-z]{2,4})$/',$email)){
return false;
} else {
return true;
}
}
// Our function to verify the MX records
function verify_email_dns($email){
list($name, $domain) = split('#',$email);
if(!checkdnsrr($domain,'MX')){
return false;
} else {
return true;
}
}
If the email address is in an invalid format or the domain does not exists I delete the users account. Are there any methods I could use to check if the email address still exists or not if the domain name is valid and the email address is in the correct format? For example abc#test.com does not exist anymore but test.com is a valid domain name.
NOTE: If a mail is send to users and the email address does not exist anymore i get a email in my inbox resulting in 1000 per day which I'm trying to avoid.
The standard way is to connect to the remote mailserver and send it a VRFY command. However, some servers don't allow that because it makes it much easier for spammers to find out valid e-mail addresses. You can also try sending it a RCPT TO command (you'll get a 550 response if the address is invalid), but they tend to block you if you do that too many times, for the same reason
If you're already getting bounced e-mails in your inbox, it seems like you could just parse those and automatically remove people from your list that are causing delivery failures