I use PHPMailer to send some email to one ore more detinations.
However I encountered a problem. Here comes:
This is my code:
$mail2->AddAddress($cliemail);
$mail2->AddCC('important#destination.com');
$mail2->SetFrom('important#destination.com', "...");
$mail2->AddReplyTo('important#destination.com', "...");
$mail2->Subject = 'Email subject... '.$wnume;
$mail2->AltBody = 'To be able to see this email please...';
$mail2->MsgHTML(file_get_contents('contents.html'));
$mail2->AddAttachment('teste/quiz.pdf'); // attachment
$mail2->Send();
} catch (phpmailerException $e) {
echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
echo $e->getMessage(); //Boring error messages from anything else!
}
So, my email must be sent from important#destination.com (it is another email of course)
And it gets sent to $cliemail, but it does not arrive to the CC (important#destination.com)
So apparently I cannot send an email to myself... Is this normal?
What could be the cause?
I tried to remove the AddCC and try to simply send the email directly to important#destination.com but it still does not arrive.
Is this some setting in my email account that I must do in order to be able to receive an email from myself?
So:
When I send the email to a third party email address using the above
code, it works
When I send the email to myself (the sender) (the same address that is the "SetFrom") it does not arrive
Even if I set the CC with my address, it still does not arrive, while it arrives at the main destination address ($cliemail)
Please help
Related
So I am want to send an email and record in the database that it was sent successfully, here is what I do:
First, try sending an email to the user containing the product information
Second, check if the email was sent successfully. If yes, then record in the database that it was sent successfully.
But if sending the email failed (an exception was thrown) I want to catch that exception and return an error message.
My question is:
Is there a case that the email gets sent but still throws an exception?
So by that the code returns error thinking that the email wasn't sent .. but it was actually sent and the exception was throw later after that.
// pseudo code
try{
$is_sent = send_email();
if($is_sent){
$db->email_was_sent();
}
}catch(Exception $e){
return 'Email was not sent. An exception';
}
Is there a case that the email gets sent but still throws an exception?
It depends.
If email is sent for a single recipient, any 3 of these situation could result:
email is delivered to recipient
email failed to be delivered to recipient
an exception was raised
For this case, it would be undocumented behaviour of the SwiftMailer email client
to have an email sent but still throw an exception.
If email is sent to several recipients, any 3 of these situation could result:
email is delivered to all recipients
email failed to be delivered to one or more recipient(s)
an exception was raised
For this other case, email could be delivered to some recipients and still raise an exception.
https://swiftmailer.symfony.com/docs/sending.html#using-the-send-method
AbstractSmtpTransport::send() shows that email may fail to be sent for one or more of the recipients.
https://github.com/swiftmailer/swiftmailer/blob/v6.2.1/lib/classes/Swift/Transport/AbstractSmtpTransport.php#L178
I am building a website for a group of kitchen chefs that allows new members to subscribe without any action from the owner.
When subscribing, new users should receive an e-mail with an activation link, in order to validate their address.
In the beginning I was using the function mail(), but a few addresses weren't receiving anything from the website, so I switched to php mailer.
php mailer code (no errors shown during execution, before I had echoes to check)
require_once ('layout/phpmailer/class.phpmailer.php');
$mail = new PHPMailer(true);
try {
$mail->AddReplyTo("$NoreplyMailURL","$NoreplyMailNAME");
$mail->AddAddress($mdest);
$mail->SetFrom("$NoreplyMailURL","$NoreplyMailNAME");
$mail->AddReplyTo("$NoreplyMailURL","$NoreplyMailNAME");
$mail->Subject = "$mobj";
$mail->AltBody = strip_tags ($mmex); // optional - MsgHTML will create an alternate automatically
$mail->MsgHTML($mmex);
$mail->Send();
}catch (phpmailerException $e) {
} catch (Exception $e) {
}
$NoreplyMailURL is the (existing) web site e-mail address
$NoreplyMailNAME is something like "Chef Enrico"
$mobj is a sentence like "new password for our website" or "account activation link"
$mmex is the message text formatted in html
Gmail, some hotmail and other users are receiving our e-mails,
some hotmail, bluewin.ch and libero.it are not.
Do you have any suggestions on how to make this code stronger? Is it possible that if receiving providers does not support HTML formatting, the e-mail is not shown? I guess they should see all tags if that was the case, am I wrong?
Thank you very much in advance!
I need to know if PHPMailer was unable to send an email. But even sending to a fake email address returns true:
$phpmailer = new PHPMailer( true );
$phpmailer->setFrom( "myemail#myemailladdy.com", "myemail#myemailladdy.com" );
//This is definitely not reachable
$phpmailer->addAddress( "fake#shdsabdasdiuahsdiuhaiduhasidsjdfake.com", "IJustPressedRandomKeys" );
$phpmailer->Subject = "fake";
$phpmailer->Body = "fake";
echo "Is Mail: " . $phpmailer->IsMail();
//This prints "1"
echo "Was Sent: " . $phpmailer->send();
Why is this returning 1/true?
(When the email is valid, I do recieve the emails, so PHPMailer is setup correctly)
PHPMailer does not know whether an email address is real or not. The mail server won't know until it sends the email and gets a rejection response. But the handoff between the server and PHP has already been terminated by that point.
There is no real way to verify an email address exists without sending an email to it and getting either a response or having the user enter a unique token into a web form. The closest you can get is verifying MX records or other DNS information that verifies a domain exists, etc. But that will not be perfect and will have false positives as well as letting fake emails through if the domain is valid.
I wos thinking about it for a while... and i think i have a nice solution.
If there will be some kind big trouble:
if(!$mail->Send()) {
echo $mail->ErrorInfo; // this is important for you
// other functions...
}
or if will be success?
else {
$smtp_msg = 'ALL OK'; // sets the message you want to see
if ($mail->ErrorInfo != '') { // check if there wos any other error
$smtp_msg = $mail->ErrorInfo; // if yes - show it to me
}
// else is optional but no need couse if there wos no error we already set $smtp_msg = 'ALL OK';
return $smtp_msg;
}
Or even better you could try to use codes of exrrors to show youre own messages...
Or... use try/catch like here:
Error handling with PHPMailer
This could be helpfull!
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.
I'm using PHPmailer to send an email from a website to the website owner. It works fine to some addresses (e.g. my Gmail account), and it used to work to the owner's address, but he's recently changed from POP to IMAP and now he doesn't receive emails from the website. He does receive emails from other sources. This is the code:
$mail = new PHPMailer(true);
try {
$mail->AddAddress($to, 'Example To');
$mail->SetFrom('example#example.com', 'Example');
$mail->AddReplyTo('example#example.com', 'Example');
$mail->Subject = $subject;
$mail->AltBody = 'To view the message, please use an HTML compatible email viewer!'; // optional - MsgHTML will create an alternate automatically
$mail->MsgHTML($message);
$mail->Send();
//echo "Message Sent OK</p>\n";
} catch (phpmailerException $e) {
echo $e->errorMessage(); //Pretty error messages from PHPMailer
} catch (Exception $e) {
echo $e->getMessage(); //Boring error messages from anything else!
}
Any advice much appreciated.
Thanks!
G
That has nothing to do with the PHP code. The IMAP protocol is just responsible for fetching the mails from the server as a user (with IMAP the mails stay on server ... you have an open stream for a long time ...).
So: did he switch to another email provider? Maybe it is in spam. Check the maillog! ;-)