How to prevent an error to display to user - php

I have a simple code for sending mails. sometime $host might not available or my SMTP server might be down but what happens in these cases swiftmailer throws lot of exceptions and $result despite of returning true or false give me a complete mess of errors.
so how i can turn off the errors for this page of code but not for the whole library?
$transport = Swift_SmtpTransport::newInstance(self::$host, 25)
->setUsername(self::$username)
->setPassword(self::$password);
//Create the Mailer using your created Transport
$mailer = Swift_Mailer::newInstance($transport);
$message = Swift_Message::newInstance("Custom Sheets");
$message->setFrom(array('EDB#abc.com.pk' => 'Approval of Custom Duty Sheet'));
$message->setTo($to);
$message->setBody($html,'text/html');
//Send the message
$result = $mailer->send($message);

You can catch that exception and handle it:
try {
$transport = Swift_SmtpTransport::newInstance(self::$host, 25)
->setUsername(self::$username)
->setPassword(self::$password);
//Create the Mailer using your created Transport
$mailer = Swift_Mailer::newInstance($transport);
$message = Swift_Message::newInstance("Custom Sheets");
$message->setFrom(array('EDB#abc.com.pk' => 'Approval of Custom Duty Sheet'));
$message->setTo($to);
$message->setBody($html,'text/html');
//Send the message
$result = $mailer->send($message);
} catch(Exception $e) {
// handle error here
}
Turning off errors / warnings / notices in general:
error_reporting(-1);

Related

SMTP use in PHPMailer

I am using the last version of PHPMailer to send emails from my web. I can send some emails without SMTP parameters, but i need to give the SMTP configuration to my object PHPMailer(). The functions that sends this emails are in the same file.
Why some emails could need SMTP configurations and not others?
Regards
function sendEmailWithoutSMTP(){
$bodyIntern = file_get_contents('emails/myemail.html');
$mailIntern = new PHPMailer(true);
$mailIntern->CharSet = "UTF-8";
try {
//Recipients
$mailIntern->AddAddress('myemail#myemail.com', 'My address');
$mailIntern->SetFrom('myemail#myemail.com', 'My address');
$mailIntern->AddReplyTo('myemail#myemail.com', 'My address');
//Content
$mailIntern->Subject = 'My subject';
$mailIntern->AltBody = '';
$mailIntern->MsgHTML($bodyIntern);
$mailIntern->Send();
} catch (phpmailerException $e) {
//echo $e->errorMessage();
} catch (Exception $e) {
//echo $e->getMessage();
}
}
function sendEmailWithSMTP($email){
require_once 'smtp/getsmtp.php';
$smtp= get_smtp($email);
$bodyClient = file_get_contents('emails/myotheremail.html');
$mailClient = new PHPMailer(true);
$mailClient->CharSet = "UTF-8";
try {
//Server settings
$mailClient->IsSMTP();
$mailClient->SMTPDebug = SMTP::DEBUG_OFF;
$mailClient->SMTPAuth = true;
$mailClient->SMTPSecure = $smtp["secure"];
$mailClient->Host = $smtp["host"];
$mailClient->Username = $smtp["username"];
$mailClient->Password = $smtp["pass"];
$mailClient->Port = $smtp["port"];
$mailClient->Timeout= 30;
//Recipients
$mailClient->AddAddress('myemail#myemail.com', 'My address');
$mailClient->SetFrom('myemail#myemail.com', 'My address');
$mailClient->AddReplyTo('myemail#myemail.com', 'My address');
//Content
$mailClient->Subject = 'My subject';
$mailClient->AltBody = '';
$mailClient->MsgHTML($bodyIntern);
$mailClient->Send();
} catch (phpmailerException $e) {
//echo $e->errorMessage();
} catch (Exception $e) {
//echo $e->getMessage();
}
$this->sendEmailWithoutSMTP();
}
PHPMailer has several different ways of sending messages.
First of all there is the default, which uses PHP's built-in mail() function behind the scenes. This does not use SMTP, but hands the message to a sendmail program using a path set in your php.ini file. This sending method does not require any SMTP setup, and SMTP-related params (e.g. Username, Host) will just be ignored, but it does require that you have a local mail server.
isSendmail() is like a manually-constructed equivalent to PHP's built-in function, but it gives you a bit more control if you are using some unusual local mail server. Generally you should not use this.
If you call isSMTP(), it will try to use SMTP directly, and all of the SMTP parameters will be used. Even if you do have a local mail server, this is the recommended route because it's faster, safer, and easier to debug. If you need to send through a remote mail server (such as gmail), this is your only option.
So in your code, the first function is sending using mail(), and the second uses SMTP.

php laravel swift email sending problems

I am trying to send an email threw a laravel controller
this is my code:
$email_sender = 'endritsheholli#gmail.com';
$email_pass = 'blablablablablabla';
$email_to = 'endritsheholli.a#gmail.com';
// Backup your default mailer
$backup = \Mail::getSwiftMailer();
try{
//https://accounts.google.com/DisplayUnlockCaptcha
// Setup your gmail mailer
$transport = (new Swift_SmtpTransport('smtp.gmail.com', 587))
->setUsername($email_sender)
->setPassword($email_pass);
$mailer = new Swift_Mailer($transport);
// Set t
he mailer as gmail
\Mail::setSwiftMailer($gmail);
$data['emailto'] = $email_sender;
$data['sender'] = $email_to;
//Sender dan Reply harus sama
$message = (new Swift_Message('Reset Password'))
->setFrom([$email_sender => 'EndritSheholli'])
->setTo([$email_to , 'endritsheholli.a#gmail.com' => 'ESheholli'])
->setBody('Here is the message itself')
;
$result = $mailer->send($message);
}catch(\Swift_TransportException $e){
$response = $e->getMessage() ;
echo $response;
}
the error that is returned is:
Class 'App\Http\Controllers\Swift_SmtpTransport' not found
I have installed the composer package and I have tried a lot of proposals in the internet but I cannot find a solution.
You must declare Swift_SmtpTransport in use section or write full namespace of class in your code. For example:
use Swift_SmtpTransport;
// other code
or
$transport = new \Swift_SmtpTransport

PHP GMail SMTP Failure

I have used this swiftmailer
The PHP code seems to work but ultimately this does nothing.
I hope someone dealt with this repo could help me.
<?php
require_once 'swiftmailer/vendor/autoload.php';
// Create the Transport
$transport = (new Swift_SmtpTransport('smtp.gmail.com', 465))
->setEncryption('ssl')
->setUsername('alice#wonderland')
->setPassword("zxcfA!0987")
;
// Create the Mailer using your created Transport
$mailer = new Swift_Mailer($transport);
// Create a message
$message = (new Swift_Message('Wonderful Subject'))
->setFrom(['john#doe.com' => 'John Doe'])
->setTo(['bob#cling.com' => 'Bob'])
->setBody('Here is the message itself')
;
// Send the message
$result = $mailer->send($message);

PHP SwiftMailer Not sending

I am doing some testing prior to working on some production code and need to figure out how to do an auto e-mail.
The below script runs fine and the result of the send method returns 1, as if it sends. However, nothing ever makes it to the recipient.
require_once '/home/absolut2/lib/swift_required.php';
//Create the Transport
$transport = Swift_SmtpTransport::newInstance('mail.mysite.com', 25)
->setUsername('myuser')
->setPassword('password')
;
/*
You could alternatively use a different transport such as Sendmail or Mail:
//Sendmail
$transport = Swift_SendmailTransport::newInstance('/usr/sbin/sendmail -bs');
//Mail
$transport = Swift_MailTransport::newInstance();
*/
//Create the Mailer using your created Transport
$mailer = Swift_Mailer::newInstance($transport);
//Create a message
$message = Swift_Message::newInstance('Subject')
->setFrom(array('rp#mysite.com' => 'RP'))
->setTo(array('rp#gmail.com'))
->setBody('Here is the message itself');
//Send the message
$result = $mailer->send($message);
echo "Messages sent: " . $result;
The code itself seems fine, so I guess something else is wrong. Either check the spam queue of the recipient or maybe just the address was rejected.
Find out if addresses were rejected.
You can do that with this code:
if (!$mailer->send($message, $failures)) {
echo "Failures:";
print_r($failures);
}

Swiftmailer 4 does not retrieve bounces as $failedRecipients

I am trying this code (from http://swiftmailer.org/docs/sending.html):
require_once 'lib/swift_required.php';
//Create the Transport
$transport = Swift_SmtpTransport::newInstance('localhost', 25);
//Create the Mailer using your created Transport
$mailer = Swift_Mailer::newInstance($transport);
//Create a message
$message = Swift_Message::newInstance('Wonderful Subject')
->setFrom(array('john#doe.com' => 'John Doe'))
->setBody('Here is the message itself')
;
//Send the message
$failedRecipients = array();
$numSent = 0;
$to = array('receiver#domain.org', 'other#baddomain.org' => 'A name');
foreach ($to as $address => $name)
{
$message->setTo(array($address => $name));
$numSent += $this->send($message, $failedRecipients);
}
printf("Sent %d messages\n", $numSent);
The problem is that if I sent an email to a bad domain swiftmailer recognize it as a correct sent email and $failedRecipients is empty. In my mail box I have returned a failure notice.
Why does Swiftmailer not recognize this mail as as a failure, and does not populate $failedRecipients Array?
Swiftmailer only takes care to hand the email over to the mail-server. Everything else is not related to Swiftmailer.
What you get is a bounce message, and you need to process them on your own, because the email itself actually was a syntactically mail address that was not rejected by the first server.
That btw is the case for any other mailing library and even the php mail function. You might be looking for a bounce processing application or code.
Related: Bounce Email handling with PHP?

Categories