Swiftmailer errors - php

I`ve started to explore a Swiftmailer, and faced this issue.Added some info about errors
I just copied code from swiftmailer documentation, and adapted. When I try to send a message, it gives a messages about errors. I dont know, where is a problem, thats why ive attached all my code:
<?php
error_reporting(E_ALL);
ini_set('display_errors', 'On');
require '../../../../vendor/autoload.php';
// Create the Transport
$transport = (new Swift_SmtpTransport('smtp.gmail.com', 587, 'tls'))
->setUsername('mymail#gmail.com')
->setPassword('my_app_pass_from_google');
// Create the Mailer using your created Transport
$mailer = new Swift_Mailer($transport);
// Create a message
$message = (new Swift_Message('Wonderful Subject'))
->setFrom(['mymail#gmail.com' => 'Me'])
->setTo(['mymail#gmail.com' => 'Also me'])
->setBody('Here is the message itself')
;
// Send the message
$result = $mailer->send($message);
if ($result){
echo 'Nice';
}
else{
echo 'bruh';
}
?>
P.S. Tried to change ports in code, also in php.ini. I use MAMP.
Tried to change ports in code, also in php.ini. I use MAMP. Also tried SMTP of other service(not gmail.com), there I have same problem, but page have been downloading slowly(dont know why)

Related

Yii2 Swiftmailer can't connect to SMTP sometimes, returns Internal Server Error

Sometimes my program can send a message and sometimes not, according to one of my colleagues it is because of the connectivity here in our place. Sometimes I can't ping the SMTP address.
Now I tried retrying to send the message in a loop, yet it doesn't because the program will stop because of an internal server error.
$mail_params = $params['mail'];
$transport = \Swift_SmtpTransport::newInstance($mail_params['smtp'], $mail_params['port'], $mail_params['encryption'])
->setUsername($mail_params['user'])
->setPassword($mail_params['password']);
$mailer = \Swift_Mailer::newInstance($transport);
$message = \Swift_Message::newInstance($subject)
->setFrom([Yii::$app->params['notificationEmail'] => 'TMC Scheduler'])
->setTo([$to])
->setBody(trim($email_message));
while ($i = 0) {
$i = $mailer->send($message);
}
return $i
I've been stuck here 4 days. Do you guys have any ideas? I'm running out of it.

Cannot get Swiftmailer to work on a PHP site

I am new to PHP and Swiftmailer.
What I am trying to do is set up a PHP site on a webserver and use SwiftMailer to send e-mails.
The code I got does work on my local XAMPP server, but will produce the error message:
"Fatal error: Class 'Swift_Attachment' not found in /[address to my php file]"
when executed from the on-line webserver.
Here is my code:
<?php
// Get this directory, to include other files from
$dir = dirname(__FILE__);
// Get the contents of the pdf into a variable for later
ob_start();
require_once($dir.'/pdf_selbst_lir.php');
$pdf_html = ob_get_contents();
ob_end_clean();
// Load the dompdf files
require_once($dir.'/dompdf/dompdf_config.inc.php');
$dompdf = new DOMPDF(); // Create new instance of dompdf
$dompdf->load_html($pdf_html); // Load the html
$dompdf->render(); // Parse the html, convert to PDF
$pdf_content = $dompdf->output(); // Put contents of pdf into variable for later
// Get the content of the HTML email into a variable for later
ob_start();
require_once($dir.'/Templates/html.php');
$html_message = ob_get_contents();
ob_end_clean();
// Swiftmailer
require_once($dir.'/swiftmailer-5.0.1/lib/swift_required.php');
// Create the attachment with your data
$attachment = Swift_Attachment::newInstance($pdf_content, 'pdfname.pdf', 'application/pdf');
// Create the Transport
$transport = Swift_SmtpTransport::newInstance('mymailserver', 587, 'tls')
->setUsername('username')
->setPassword('password')
;
// Create the Mailer using your created Transport
$mailer = Swift_Mailer::newInstance($transport);
// Create the message
$message = Swift_Message::newInstance()
->setFrom(array('senderaddress' => 'Sender Name'))
->setSubject('subject')
->addBcc('somebccaddress')
->setBody($html_message, 'text/html')
->attach($attachment);
try {
$message->addTo('somerecipient');
}
catch (Swift_RfcComplianceException $e)
{
// Address was invalid
echo "Email address not correct.";
}
// Send the message
if ($mailer->send($message))
$success = true;
else
$error = true;
?>
Note that when I comment out all the attachment-related stuff, the error message switches to
"Fatal error: Class 'Swift_SmtpTransport' not found in /[address to my php file]" and points to the "$transport" line.
So it appears the overall SwiftMailer is not working, and this is not attachment-related.
Help would be greatly appreciated!
I don'see ->setTo( your_mail_dest) and ->send()
try with a simple send like this
$message = Swift_Message::newInstance()
->setFrom(array('senderaddress' => 'Sender Name'))
->setTo( **** your_mail_dest ****)
->setSubject('subject')
->addBcc('somebccaddress')
->setBody($html_message, 'text/html')
->attach($attachment)
->send();
Case closed.
It turned out that the code was working fine after all. All I needed to do was upgrade Swiftmailer (now running on 5.4.2-DEV). Sorry for the hassle and thanks scaisEdge for helping!

Can not send email threw SwiftMailer with Mandrill

Since today, many of my php applications can not send email using SwiftMailer and different Mandrill accounts.
I've got this code, and the send function in the last if stop the script..
// Instance message
$message = Swift_Message::newInstance();
$message->setSubject("subject")
->setFrom(array('noreply#email.test' => 'test'))
->setTo(array('a_valid_email' => 'name'))
->setBody("test", 'text/html')
->setPriority(2);
$smtp_host = 'smtp.mandrillapp.com';
$smtp_port = 587;
$smtp_username = 'valid_username';
$smtp_password = 'valid_password';
// SMTP
$smtp_param = Swift_SmtpTransport::newInstance($smtp_host , $smtp_port)
->setUsername($smtp_username)
->setPassword($smtp_password);
// Instance Swiftmailer
$instance_swiftmailer = Swift_Mailer::newInstance($smtp_param);
$type = $message->getHeaders()->get('Content-Type');
$type->setValue('text/html');
$type->setParameter('charset', 'iso-8859-1');
//Here the send function stop event and I did not go inside the if
if ($instance_swiftmailer->send($message, $fail)) {
echo 'OK ';
}else{
echo 'NOT OK : ';
print_r($fail);
}
Thank you in advance to help me to solve this problem..
If your code used to work, and now doesn't work, and you didn't change your code, then it's probably not a problem with your code. Check your Mandrill account validity - sometimes they suspend accounts for suspicious-appearing usage.

CakePHP email not working properly - how to track the error

I using cakephp email component. In my live server $this->Email->send() return success. but mail is not receiving. what is the problem?? i need to find whats the error ?
My controller not have any model this may cause any problem for emails ?
$this->Email->from = 'Mysitename <no-reply#mysite.com';
$this->Email->to = 'sample#gmail.com';
$this->Email->subject = "This is test";
$this->Email->template = 'template_name';
$this->Email->sendAs = 'html';
ob_start();
if($this->Email->send())
{
$this->log(' Mail Success');
}
else
{
$this->log('Something broke in mail');
}
ob_end_clean();
You can set delivery to debug to see an output of your message to make sure it's fine:
$this->Email->delivery = 'smtp';
And you also need to setFlash('email') to see the output in your view:
echo $this->Session->flash('email');
As far as emailing from a live server goes - there's a very good chance the server or IP is blacklisted and you'll need to get it to pass various checks before your sent messages can be received:
http://www.digitalsanctuary.com/tech-blog/debian/setting-up-spf-senderid-domain-keys-and-dkim.html

Swift-Mailer error, "Address in mailbox given [] does not comply with RFC"

I have built a simple PHP contact form that is supposed to send mail trough the Swift-Mailer script.
Problem is I keep getting this error
Uncaught exception
'Swift_RfcComplianceException' with
message 'Address in mailbox given []
does not comply with RFC 2822, 3.6.2.'
Which I guess means I am using an invalid e-mail address. But since I am using myaddress#gmail.com to test the scrip the problem is probably somewhere else. This is my configuration:
Where the mail is sent to:
$my_mail = 'mymail#mydomain.com';
$my_name = 'My Name';
The content of the message:
$name = trim($_POST['name']);
$email = trim($_POST['email']);
$message = trim($_POST['message']);
$date = date('d/m/Y H:i:s');
$ipaddress = $_SERVER['REMOTE_ADDR'];
$content = $message.'\n\nSent on: '.$date.' From: '.$ipaddress;
The function i use to send the mail using swiftmailer:
function send_mail () {
require('/path/to/swift_required.php');
//The means of transport
$transport = Swift_SmtpTransport::newInstance('mail.mydomain.com', 25);
$transport->setUsername('myusername');
$transport->setPassword('mypass');
$mailer = Swift_Mailer::newInstance($transport);
//The message
$mail = Swift_Message::newInstance();
$mail->setSubject('Hello');
$mail->setFrom(array($email => $name ));
$mail->setTo(array($my_mail => $my_name));
$mail->setBody($content, 'text/plain');
//Sending the message
$test = $mailer->send($mail);
if ($test) {
echo '<p>Thank you for contacting us '.$name.'! We will get in touch soon.</p>';
}
else {
echo '<p>Something went wrong. Please try again later.</p>';
}
}
As you can see it is really simple form with three fields, name, mail and message. I also have other validation set up for each of contact form fields, but I think it is of little concern here.
Thank you for the help.
Edit:
Just test with using gmail as the smtp server. Unfortunately it still gives the same exact results.
All your data variables (addresses, names...) appear to be global. Global variables cannot be read from within functions unless you pass them as parameters (the recommended way) or use the global keyword (or the $GLOBALS array).

Categories