Sending email from Magento to fails - zend-framework

I am trying to send an email from a custom module in magento, however it fails to send it.
do i need to include anything or should i make some configuration with my hosting?
Here you can see my code:
$mail = new Zend_Mail();
$mail->setBodyText($mailbody);
$mail->setFrom('admin#gmail.com', 'admin');
$mail->addTo('email#gmail.com', 'client');
$mail->setSubject('Error report');
try {
$mail->send();
Mage::getSingleton('core/session')->addSuccess('Your request has been sent');
}
catch (Exception $e) {
Mage::getSingleton('core/session')->addError('Unable to send.');
}

It's better to use Magento's models for sending email. This way you know it's being send correctly, and get userful errors when it fails
The most simply way:
$email = Mage::getModel('core/email_template');
$email->setSenderEmail('sender#email.com');
$email->setSenderName('name');
$email->setTemplateSubject('Subject');
$email->setTemplateText('emailbody');
$email->send('receiver#mail.com', 'receiver name');
Remember that you host my not support sending mail, or that your provider is blocking port 25. This will result in a message in your exception.log
If you want to see how the final email looks, print $email->getProcessedTemplate() onto your screen

You have to configure your smtp in php.ini
What is your provider ? It's possible to find the smtp server's url, and put it in your php.ini file.

Related

passing data to mailgun webhooks in laravel

Im using laravel and configured mailgun to send mails
I want to use webhooks to track them.
so I need to send data with the message so I can track it using the web hook
for example attach a message id to each mail I send
tried to follow the mailgun documnation but no luck
this is my code for sending the mail
$data = array('course_name' => $course_name,'grade' => $grade,'email' => $stud->email,
"v:messageId" => "123");
Mail::send('emails.stud_feedback',$data, function ($message) {
$message->to($this->email)->subject( $this->course);
$message->attach($this->file, ['as' => 'feedback']);
});
according to the documnation the web hook should post me the message id,
but Im not getting it.
what am I doing wrong?
solved by setting headers to the mail:
$message->getHeaders()->addTextHeader('X-Mailgun-Variables', "{'messageid:123}'}");
Use This :
Mail::send('emails.test',[]), function ($message) use ($subject, $from, $emails) {
dd($message->getSwiftMessage()->getId());
});

Cakephp : add more recipients before send an email

I have to send an email to several recipients, but only if my current email address is marked as 'out of office'. In order to do that I will need to check the condition before sending the email and to retrieve my backups and add them in $to .
I currently managed to find a quick fix, I've modified CakeEmail, but it seems a little wrong to alter this file.
Any ideea of how I can do this without modifying CakeEmail?
Thank you,
In your configuration (core.php) file you create a new configuration
Configure:write( 'notify_people', array( 'email1#example.com' => 'Email1', 'email2#example.com' => 'Email2' );
Then you use the CakeEmail::to() to add recipients before sending an email. So you update all your $email->to() calls to read the configuration value instead.
$email = new CakeEmail();
$email->to( Configure::read( 'notify_people' ) );
//TODO: configure sender, subject etc...
//finally send the email
$email->send( $bodyOfTheMessage );
When you need to change the list of recipients you only have to do it once in the configuration file.

Laravel 4 Email in development environment

Mail::pretend() logs only that a sending mail was pretended. Is it possible to dump output of the rendered email to a file or log during development?
a great option I've started using is https://papercut.codeplex.com/
you set your smtp settings to route to this simple program & it intercepts the emails in an Outlook-like app, as if the email really sent out
If you cache the rendered view in a variable then you can do it, like
$renderedView = View::make('email.index', $data); // email(folder)/index(file);
// Now you can dump the output ($renderedView) to a file
File::put('path/to/file', $renderedView)
$data['content'] = $renderedView;
Mail::send('emails.welcome', $data, function($message)
{
// code here
});

Sending Email in DNN

I'm trying to send an email in a DNN module I'm making. However, though it doesn't crash the email isn't being sent. I think it has to do with the From Email I'm attempting to use. I'm not 100% sure what email I should be using for the from which is the first parameter.
Protected Sub Submit_Click(sender As Object, e As EventArgs) Handles Submit.Click
DotNetNuke.Services.Mail.Mail.SendEmail("support#localhost", "myemail#site.com", "EmailTest", "Hello world!")
End Sub
The More likely problem is you don't have your SMTP settings properly configured. To configure your SMTP settings, Login as Host. Then, go to Host -> Settings and fill out the fields under "SMTP Server Settings" and save them. There's a test link there as well to verify they are working correctly.
This is probably pretty late to the party, but I often use the Mail.SendMail() method, and then manually pass all the STMP information like below, and then when debugging I check the message that comes back. (As of DotNetNuke 5.5)
Dictionary<string, string> hostSettings = HostController.Instance.GetSettingsDictionary();
string server = hostSettings["SMTPServer"];
string authentication = hostSettings["SMTPAuthentication"];
string password = hostSettings["SMTPPassword"];
string username = hostSettings["SMTPUsername"];
// using the Mail.SendMail() method allows for easier debugging.
var message = Mail.SendMail(from, user.Email, String.Empty, subject, body, String.Empty, "HTML", server, authentication, username, password);
Late to the game as well, but I just ran into a similar issue earlier today...
The DNN sendMail or sendEmail method handle the exceptions on their own, and add it to their DNN logs. Unfortunately, they never return said exceptions to the main code where you are calling the functions - hence why your code executes just fine!
You can look further into their exceptions table, or Admin Logs in the UI, for more info on the particular issue you are having.
I changed my code to use System.Net to send emails and collect all of the info you need from DotNetNuke.Entities.Host.Host object in DNN. This way, we can handle the error and have our code work around it :) I ended up with something like this (it's in c# but you can do the same in VB.Net with a slightly different syntax):
//make the email
MailMessage mail = new MailMessage("From#me.com","to#a.com,to#b.com,to#c.com");
mail.Subject = "test subject";
mail.Body = "actual email";
string dnnServerInfo = DotNetNuke.Entities.Host.Host.SMTPServer;
// The above looks like "server.com:port#", or "smtp.server.com:25"
//so we find the colon to get the server name, and port using the index below
int index = dnnServerInfo.IndexOf(':');
//make the SMPT Client
SmtpClient smtp = new SmtpClient();
smtp.Host = dnnServerInfo.Substring(0, index);
smtp.Port = Int32.Parse(dnnServerInfo.Substring(index + 1, dnnServerInfo.Length - index - 1));
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new System.Net.NetworkCredential(DotNetNuke.Entities.Host.Host.SMTPUsername, DotNetNuke.Entities.Host.Host.SMTPPassword);
smtp.EnableSsl = DotNetNuke.Entities.Host.Host.EnableSMTPSSL;
//send the email
smtp.Send(mail);
I used part of the original code from "SendMail" found here to come up with this: https://stackoverflow.com/a/19515503/6659531
Good luck to anybody who comes across this :)

Zend Mail Exception - unroutable Address - why?

Hi guys I've built a simple cron program which runs in php using the zend framework. It sends emails periodically to members of a website with updates. It was working fine when all of a sudden it just dies out upon emailing to a particular email and all I get is this error message:
PHP Fatal error: Uncaught exception 'Zend_Mail_Protocol_Exception' with message 'Unrouteable address ' in /web/content/library/Zend/Mail/Protocol/Abstract.php:431
Whats going on and why is this happening for this particular email?
The email seems fine though i.e it isn't malformed or so. Also how can I prevent something like this from stopping the cron job to proceed with other emails.
What about placing it in a try catch block ?
First of all you should validate email addresses, Afterwards you should try a catch block like this:
try {
/*
* Set up Testing environment for Smtp mail
* Handle Mail exceptions Different
*/
$mail = new Zend_Mail();
$mail->send();
} catch (Zend_Mail_Transport_Exception $ex) {
$this->addError('There was an error sending e-mail to the new admin !');
} catch (Exception $ex) {
$this->addError('There was an error processing your request');
}