I'm bringing the (Laravel-based) website for an event that's coming up soon, up to date. Part of this was improving the mailing function, for which I decided to use Mandrill's SMTP with SwiftMailer. Everything worked perfectly while working locally. However, since we pushed everything to a live (well, testing, but on the same server as live) staging area, no mails get sent anymore.
Everything seems to break down after I make a send() command in PHP. Even a simple print command doesn't do anything. There's also no errors being reported, except if I go look in my console, where the request returns a 500 Internal Server Error, without any other errors.
At the moment, these are the function I'm using only to test, which works and sends perfectly on local and then prints, but just gives a white screen on the staging area...
Route::any('test', function()
{
testMail();
//this print works perfectly locally but shows nothing on staging
print ('boe');
});
function testMail(){
$to = array('private#email.address' => 'My Name');
$subject = 'test mail';
$text = "test mail hier";
$htmlTekst = "<b>boe</b><i>spaghetti</i>";
$view = View::make('mailTemplate',
['naam' => 'Jeroen Cuvelier',
'tekst' => $htmlTekst,
'username' => 'my email address',
'password' => 99999,
'siteUrl' => rootUrl(),
'header2' => '']);
$html = $view->render();
sendEmail($text, $html, $to, $subject, "", "");}
function sendEmail($text, $html, $to, $subject, $attachment, $attachmentName)
{
//$subject = 'Subject!';
$from = array('event#email.company' =>'Company event');
/*
SMTP-settings
*/
$transport = Swift_SmtpTransport::newInstance('smtp.mandrillapp.com', 25);
$transport->setUsername('MY_MANDRILL_USERNAME');
$transport->setPassword('MY_MANDRILL_PASSWORD');
$swift = Swift_Mailer::newInstance($transport);
$message = new Swift_Message($subject);
$message->setFrom($from);
$message->setBody($html, 'text/html');
$message->setTo($to);
$message->addPart($text, 'text/plain');
if ($attachment != "")
{
$toAttach = Swift_Attachment::fromPath($attachment);
if ($attachmentName!="")
{
$toAttach->setFilename($attachmentName);
}
$message->attach($toAttach);
}
//neither of these messages print on staging
if ($recipients = $swift->send($message, $failures))
{
echo 'Message successfully sent!';
} else {
echo "There was an error:\n";
print_r($failures);
}
}
Turns out the problem was not in PHP or anything I had done. The server just didn't allow SMTP calls. A call to the company hosting that fixed my issues.
Related
I've created a sending of email using codeigniter. on the past months it was working fine. When I checked it again it doesn't send emails anymore.
Here's My code:
public function send_mail($registration_info_id){
$config_mail = $this->main_m->get_all('emails');
$all_mails="";
foreach($config_mail as $email){
$all_mails.= $email['email'].',';
}
$supply_email = rtrim($all_mails,",");
$registration_info = $this->main_m->get_where('registration_info', array('registration_info_id' => $registration_info_id));
$personal_info = $this->flight_travel_request_m->personal_info_per_person($registration_info_id);
$cc_details = $this->main_m->get_where('payment_credit_card_details', array('registration_info_id' => $registration_info_id));
$full_name = $personal_info['firstname']." ".$personal_info['lastname'];
$data = array(
'registration_info' => $registration_info,
'personal_info' => $personal_info,
'cc_details' => $cc_details
);
//$this->load->view('email_acknowledgement/acknowledgement', $data);
$message=$this->load->view('email_acknowledgement/acknowledgement', $data, true);
$this->load->library('email');
$this->email->from('sample#site.com', 'SiteName');
$this->email->to($personal_info['email'], $full_name);
$this->email->reply_to('sample#site.com', 'SiteName');
$this->email->bcc($supply_email);
$this->email->subject('Arrival Departure Transportation Service');
$this->email->message($message);
if(!empty($attachment)){$this->email->attach($attachment);}
$this->email->set_mailtype('html');
if($this->email->send())
{
echo 'Email sent.';
}
else
{
show_error($this->email->print_debugger());
}
}
it echoes the Email Sent however I cant receive any email on my email address.
it was working fine previously.
Thanks in advance
Please try it.
$config['protocol'] = 'smtp';
$config['validate'] = 'FALSE';
I hope that works for you
And see this link Codeigniter $this->email->send() not working while mail() does
I'm using Magento v1.9.1, with a MailGun plugin installed.
Plugin url:
https://www.magentocommerce.com/magento-connect/mailgun-for-email-delivery.html
Here you can see the MailGun plugin on the admin area:
Then I used the following code to send a test email (test_email.php):
<?php
// Invoke the Magento environment
require_once( 'app/Mage.php' );
Mage::app();
//Getting the Store E-Mail Sender Name.
$senderName = Mage::getStoreConfig('trans_email/ident_general/name');
//Getting the Store General E-Mail.
$senderEmail = Mage::getStoreConfig('trans_email/ident_general/email');
$customerEmail = "<personal_email#hidden_on_purpose>";
$text = "Hello, this is a test.";
//Sending E-Mail to Customers.
$mail = Mage::getModel('core/email')
->setToName($senderName)
->setToEmail($customerEmail)
->setBody($text)
->setSubject('Subject :')
->setFromEmail($senderEmail)
->setFromName($senderName)
->setType('html');
try{
//Confimation E-Mail Send
$mail->send();
}
catch(Exception $error)
{
Mage::getSingleton('core/session')->addError($error->getMessage());
return false;
}
?>
Then the email arrives to me but it is not sent via MailGun. I know that because on the header of the received email is the server where the application lives and on the MailGun log the email is not logged.
I think the MailGun plugin is working on the backend admin area only, but it is not getting called when sending an email with Magento.
I also did a test modifying the file:
./app/code/community/FreeLunchLabs/MailGun/Model/Mailgun.php
by adding the line:
die("Send stopped on file: ./app/code/community/FreeLunchLabs/MailGun/Model/Mailgun.php");
Like:
...
public function send($message) {
die("Send stopped on file: ./app/code/community/FreeLunchLabs/MailGun/Model/Mailgun.php");
$domain = $message->getStore()->getConfig('mailgun/general/domain');
$apiKey = $message->getStore()->getConfig('mailgun/general/key');
$files = null;
if(count($message->getAttachments())) {
foreach($message->getAttachments() as $attachment) {
$files[] = $attachment;
}
}
$sendResponse = $this->mailgunRequest('messages', $domain, $apiKey, $message->getMessage(), Zend_Http_Client::POST, false, $files);
if($message->getStore()->getConfig('mailgun/events/store')) {
Mage::getModel('freelunchlabs_mailgun/email')->saveInitialSend($message, $sendResponse);
}
return $sendResponse;
}
...
But when running the file: test_email.php, the stopped message inside the die(...) on the code above doesn't appear.
Any idea on how to solve/debug this?
Ok, after some try/error experiments I found the answer: that MailGun extension applies only to transactional emails as stated on its description. You cannot send custom emails using MailGun straightforward like on my initial code: test_email.php. If you wanna send some custom email using MailGun you should create a template before and then use that template like in the following code where I created the template with id: 1
<?php
// Invoke the Magento environment
require_once( 'app/Mage.php' );
Mage::app();
$templateId = 1;
$receiveName = "<Your Name Here>";
$receiveEmail = "<your email here>";
$storeId = Mage::app()->getStore()->getStoreId();
$emailTemplate = Mage::getModel("core/email_template")->load($templateId);
$vars = array(
"name" => $receiveName,
"email" => $receiveEmail,
"phone" => "+13052491037",
"comment" => "This is my comment: Hello World!"
);
$emailTemplate->getProcessedTemplate($vars);
/*
echo "<pre>\n";
print_r($emailTemplate);
echo "</pre>\n";
die();
//*/
$emailTemplate->setSenderName(Mage::getStoreConfig("trans_email/ident_general/name", $storeId));
$emailTemplate->setSenderEmail(Mage::getStoreConfig("trans_email/ident_general/email", $storeId));
$emailTemplate->send($receiveEmail, $receiveName, $vars);
?>
Hope this helps somebody.
I am trying to get this mail sent on registration using SwiftMailer, however it keeps throwing this error.
I am using this add-on for osCommerce:
http://addons.oscommerce.com/info/901
I have followed the exact instructions as per what he said. Also added permissions still the same thing.
I am posting the code below:
CODE:
if (EMAIL_TRANSPORT == 'smtp') {
include_once(DIR_FS_CATALOG.DIR_WS_CLASSES . 'SwiftEmailer/swift_required.php');
$body = $this->html;
$body = preg_replace("[]",'',$body);
$host_name = explode('://',EMAIL_SMTP_HOST_SERVER);
if(count($host_name)>1){
$transport = Swift_SmtpTransport::newInstance($host_name[1], EMAIL_SMTP_PORT_SERVER, $host_name[0])
->setUsername(EMAIL_SMTP_USERNAME)
->setPassword(EMAIL_SMTP_PASSWORD);
} else {
$transport = Swift_SmtpTransport::newInstance(EMAIL_SMTP_HOST_SERVER, EMAIL_SMTP_PORT_SERVER)
->setUsername(EMAIL_SMTP_USERNAME)
->setPassword(EMAIL_SMTP_PASSWORD);
}
$mailer = Swift_Mailer::newInstance($transport);
$message = Swift_Message::newInstance($subject)
->setFrom(array($from_addr => $from_name))
->setTo(array($to_addr => $to_name))
->setMaxLineLength(50);
if($body){
$message->setBody($body, 'text/html', CHARSET);
}
if($this->text){
$message->setBody($this->text, 'text/plain', CHARSET);
}
else{
$message->setBody($this->html_text, 'text/html', CHARSET);
}
$mailer->send($message);
}
If anyone could help me sort out this issue that would be great. Thank you
My page continues to error out (Error 324 - Chrome) when attempting to send e-mails with attachments using PHP's PEAR Mail extension. Although the page error's out - I do receive one of the approximately 800 e-mails. Here's what I'm working with:
function email_statements($type) {
switch($type) {
// Determine the type of statement to be e-mailed
case 'monthly':
// Array holding all unique AP e-mail addresses
$ap_email_addresses = array();
include('../path/to/db/connection/params.php');
// Grab the unique AP e-mail address set to receive statements
$stmt = $mysqli->prepare("SELECT email FROM statements GROUP BY email ORDER BY email ASC");
$stmt->execute();
$stmt->bind_result($ap_email_address);
// Add unique e-mail address to AP E-mail Addresses array
while($row = $stmt->fetch()) $ap_email_addresses[] = $ap_email_address;
$stmt->close();
$mysqli->close();
// Verify we grabbed the e-mail addresses
if(count($ap_email_addresses) == 0) {
// Exit and return error code if unable to grab e-mail addresses
$return_message = 1;
exit;
}
// E-mail addresses grabbed - proceed
else {
// PDF formatted date
date_default_timezone_set('America/New_York');
$formatted_date = date('m_d_Y');
// Now we have the unique e-mail addresses - associate those with the account numbers
include('../path/to/db/connection/params.php');
foreach($ap_email_addresses as $email_address) {
$file_names = array();
$stmt = $mysqli->prepare("SELECT customer_number FROM statements WHERE email = ?");
$stmt->bind_param("s", $email_address);
$stmt->execute();
$stmt->bind_result($ap_account_number);
// Constructs the name of the statement (PDF) file to be sent
while($output = $stmt->fetch()) $file_names[] = $ap_account_number . '_' . $formatted_date . '.pdf';
// Send e-mails
include('Mail.php');
include('Mail/mime.php');
// Include SMTP authentication parameters
include('../path/to/email/info.php');
// Set the e-mail recipients
$recipients = 'example#example.com';
// Set the e-mail subject
$subject = 'Monthly Account Statement';
// Create the e-mail body
$html =
'<html>
<body>
<p>Test E-mail</p>
</body>
</html>';
// Construct the message headers
$headers = array(
'From' => $from,
'Subject' => $subject,
'Content-Type' => 'text/html; charset=UTF-8',
'MIME-Version' => '1.0'
);
$mimeparams = array();
$mimeparams['text_encoding'] = '8bit';
$mimeparams['text_charset'] = 'UTF-8';
$mimeparams['html_charset'] = 'UTF-8';
$mimeparams['head_charset'] = 'UTF-8';
// Create a new instance
$mime = new Mail_mime();
// Specify the e-mail body
$mime->setHTMLBody($html);
// Attach the statements (PDF)
foreach($file_names as $attached_file) {
$file = '../path/to/the/pdf/file/' . $attached_file;
$file_name = $attached_file;
$content_type = "Application/pdf";
$mime->addAttachment ($file, $content_type, $file_name, 1);
}
// Create the body
$body = $mime->get($mimeparams);
// Add the headers
$headers = $mime->headers($headers);
// Create SMTP connect array to be passed
$smtp = Mail::factory('smtp', array ('host' => $host, 'port' => $port, 'auth' => true, 'username' => $username, 'password' => $password));
// Send the message
$mail = $smtp->send($recipients, $headers, $body);
if(PEAR::isError($mail)) echo 'Error';
else echo 'Sent';
// Close this account and cycle through the rest
$stmt->close();
}
$mysqli->close();
}
break;
}
}
Now I thought maybe I wasn't giving the script enough time to execute - so I set it ridiculously high set_time_limit(99999999) to ensure it had plenty of time, although it normally times out within 10-15 seconds. So basically it works like this, I have an internal DB that stores customer account numbers and e-mail addresses. Accounts can have the same e-mail address (It's sent to their company's AP department) - aka one e-mail address receives statements for multiple branches. From their each statement is already constructed with the format being ACCOUNTNUMBER_MM_DD_YYYY.pdf.
So I'm simply trying to have a short message in the body, and attach the monthly statements. Again, not sure why it's failing, and even though the script halts, I do receive ONE of the e-mails (I'm sending them all to myself at the moment just to test). Can anyone see where I may have an issue?
I discovered the issue. As of PHP 5.0+ you cannot have multiple instances of the same include file - aka Mail.php was included as it looped. Once it was moved outside of the loop, the issue was resolved.
I'm trying to implement PEAR's Mail_Queue package to queue some emails for a web application. I've used the documentation at http://pear.php.net/manual/en/package.mail.mail-queue.mail-queue.tutorial.php to write a small test script.
My problem is that the database is not being updated, and it's not producing errors.
EDIT
// mail_queue db options
$db_options['type'] = 'mdb2';
$db_options['dsn'] = DSN;
$db_options['mail_table'] = 'mail_queue';
// mail_queue sending options
$mail_options['driver'] = 'smtp';
$mail_options['host'] = 'smtp.gmail.com';
$mail_options['port'] = 25;
$mail_options['localhost'] = $host;
$mail_options['auth'] = true;
$mail_options['user'] = MAILUSER;
$mail_options['pass'] = MAILPASS;
require "Queue.php";
$mail_queue =& new Mail_Queue($db_options,$mail_options);
$from = 'someone#domain.ca';
$to = 'martin#starmedia.ca';
$message = 'This is a test';
$headers = array('From' => $from,
'To' => $to,
'Subject' => 'Someone has sent you an email!');
$mime =& new Mail_mime();
$mime->setTXTBody($message);
$body = $mime->get();
$headers = $mime->headers($headers,true);
print $mail_queue->put($from,$to,$headers,$body);
This produces the error Mail Queue Error: Cannot connect to database . However I checked all of the connection information and it's correct. Also, adding if (PEAR::isError($mail)) die($mail->getMessage()); produces no errors!
OK, I finally have my mail queue file working. Here are the steps I took to get it to function:
1. Enable error messages
To enable error handling, I added this snippet:
function handle_pear_error($e) {
die($e->getMessage() . ' ' . print_r($e->getUserInfo(), true));
}
PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, 'handle_pear_error');
Once I added this, I reloaded the page and I was getting the following error:
Call to undefined function: MDB2_Driver_mysql::_isNewLinkSet()
2. Update MDB2's MySQL driver
I searched that error and found that it's usually a result of either not having an up-to-date MDB2 library or its MySQL driver.
So I updated both and it's working!