As a extended version to this question : PHPMailer(): Called Mail() without being connected
Can't we use all the configurations for the PHPmailer in an array, Some thing like this:
$mail = array(
'SMTPAuth' => true,
'Host' => 'smtp.millicenthotel.com',
'Port' => 25,
'Username' => 'info#millicenthotel.com',
'Password' => 'gboskeritysoldier1'
);
I tried something like this and it not work. I'm looking for a cleaner way of doing this.
Thanks in Advance.
you have an action hook called phpmailer_init
from codex :
The wp_mail function relies on the PHPMailer class to send email
through PHP's mail function. The phpmailer_init action hook allows you
to hook to the phpmailer object and pass in your own arguments.
example ( codex again ):
add_action( 'phpmailer_init', 'my_phpmailer_example' );
function my_phpmailer_example( $phpmailer ) {
$phpmailer->IsSMTP(); //switch to smtp
$phpmailer->Host = 'smtp.example.com';
$phpmailer->Port = 25;
$phpmailer->Username = 'yourusername';
$phpmailer->Password = 'yourpassword';
}
Related
can you help me figure out a way to use the email class of CI4 to send an email with an HTML as the message?
in Codeigniter 3 it is simple and goes like this:
$email->message($this->load->view('html_page_to_send_from_view_folder',$data,true));
but in Codeigniter 4 I tried doing this:
$email->setMessage(echo view('html_page_to_send_from_view_folder',$data,true));
It gives out an error:
syntax error, unexpected echo(T_ECHO), expecting ')'
I also tried putting the view in a variable like so:
$echo_page = echo view('html_page_to_send_from_view_folder',$data,true);
$email->setMessage($echo_page);
but it just throws the same error, I was searching all over the internet and the documentation but couldn't find a way to do this. Help please.
I tried this but got no error, and also got no email :'(
$echo_page = view('html_page_to_send_from_view_folder',$data,true);
$email->setMessage($echo_page);
According to this, if you want to use some template as your message body, you should do something like this:
// Using a custom template
$template = view("email-template", []);
$email->setMessage($template);
CodeIgniter 4 documentation states:
setMessage($body)
Parameters: $body (string) – E-mail message body
Returns: CodeIgniter\Email\Email instance (method chaining)
Return type: CodeIgniter\Email\Email
Sets the e-mail message body:
$email->setMessage('This is my message');
Okay I got it now I made it work by adding this code $email->setNewLine("\r\n"); at the end just after the setMessage:
$email->setMessage($my_message);
$email->setNewLine("\r\n");
and also I set the SMTP port 587 instead of 465:
$config['SMTPPort']= 587;
ALSO, for the setMessage, I did it like this:
$my_message = view('html_page_to_send_from_view_folder',["id" => $data['id']]);
$email->setMessage($my_message);
really weird man....
A. Firstly,
Instead of:❌
$echo_page = echo view('html_page_to_send_from_view_folder',$data,true);
Use this:✅
$echo_page = view('html_page_to_send_from_view_folder',$data);
Notice the luck of an echo statement and not passing true as the third argument of the view(...) hepler function.
B. Secondly, to submit HTML-based emails, ensure that you've set the mailType property to html. This can be achieved by using the setMailType() method on the Email instance. I.e:
$email->setMailType('html');
Alternatively, you could set the "mail type" by passing an array of preference values to the email initialize() method. I.e:
public function sendMail(): bool
{
$email = \Config\Services::email();
$email->initialize([
'SMTPHost' => 'smtp.mailtrap.io',
'SMTPPort' => 2525,
'SMTPUser' => '479d7c109ae335',
'SMTPPass' => '0u6f9d18ef3256',
'SMTPCrypto' => 'tls',
'protocol' => 'smtp',
'mailType' => 'html',
'mailPath' => '/usr/sbin/sendmail',
'SMTPAuth' => true,
'fromEmail' => 'from#example.com',
'fromName' => 'DEMO Company Name',
'subject' => 'First Email Test',
]);
$email->setTo('to#example.com');
$email->setMessage(view('blog_view'));
$response = $email->send();
$response ? log_message("error", "Email has been sent") : log_message("error", $email->printDebugger());
return $response;
}
I have a local Wordpress website that sends emails successfully, however if I try to send an email from a Codeception integration test, I do not receive any emails. (By the way, I don't think the problem has to do with Wordpress, which is why I'm posting here rather than Wordpress Stack Exchange).
I've configured Wordpress like this:
add_action( "phpmailer_init", array( $this, "configure_mailer" ) );
function configure_mailer( $mailer ) {
$mailer->isSMTP();
$mailer->Host = "smtp.gmail.com";
$mailer->SMTPAuth = true;
$mailer->Username = "myemail#gmail.com";
$mailer->Password = "mypassword";
$mailer->SMTPSecure = "tls";
$mailer->Port = 587;
$mailer->IsHTML( true );
}
Now if I try to send an email with wp_mail (which internally uses a PHPMailer instance configured as above) from my Wordpress code, eg. via an API request, I do receive the email:
// This is the custom API route that sends the email
register_rest_route( "mynamespace/v1", "/send-email", [
"methods" => \WP_REST_Server::CREATABLE,
"permission_callback" => "__return_true",
"callback" => function() {
$result = wp_mail( "myemail#example.com", "Test subject", "Test message", array( "From: MySite <myemail#example.com>");
return new \WP_REST_Response( $result );
}
]);
// This is the API request
POST https://example.com/wp-json/mynamespace/v1/send-email
The above request returns true because wp_mail returns true (as it does when an email is sent successfully), and I receive the email.
If I try to send the email from a Codeception integration test, the test succeeds because wp_mail still returns true, but I do not receive the email. (The \Codeception\TestCase\WPTestCase comes from wpbrowser, a package I use for testing Wordpress with Codeception).
class EmailTest extends \Codeception\TestCase\WPTestCase {
public function testSendsEmail() {
$result = wp_mail( "myemail#example.com", "Test subject", "Test message", array( "From: MySite <myemail#mysite.com>");
$this->assertTrue( $result );
}
}
I am trying to send an email via Zend Mail Transport SMTP
I tried to browse sample codes and use it. What I have now is this code
$config = array('auth' => 'login',
'username' => 'sample#gmail.com',
'password' => 'samplepassword',
'port' => 587,
'ssl' => 'tls');;
$transport = new Zend_Mail_Transport_Smtp('smtp.gmail.com', $config);
$mail = new Zend_Mail();
$mail->setBodyHtml('Hi From Dave.');
$mail->setFrom('noreply.networklabs#networklabs.com.ph');
$mail->addTo('john.decena#nokia.com', 'john.decena#nokia.com');
$mail->setSubject('Profile Activation');
$mail->send($transport);
Apparently, this code gives me error
Class 'User\Controller\Zend_Mail_Transport_Smtp' not found
May I know what I'm missing? because I tried to study their codes but they didn't have that error. And what credentials did I have to use.? Thanks in advance.
This really looks like a namespace problem.
I don't know which version of ZF2 you are using, but you need to add an use statement at the beginning of your file, something like
use Zend\Mail\Transport\Smtp;
and the create the new instance with
$transport = new Smtp('smtp.gmail.com', $config);
I'm pretty new to CakePHP and this is my first attempt setting up an email form.
Keeping the example simple:
<?php
App::uses('AppController', 'Controller');
App::uses('CakeEmail', 'Network/Email');
class EmailController extends AppController {
public function send_email($from, $subject, $message) {
$Email = new CakeEmail();
$Email->from($from)
->to('[my personal email]')
->subject($subject);
if($Email->send($message)) {
$result = 'Your email has been sent.';
} else {
$result = 'Your email failed to send.';
}
$this->set('result', $result);
$this->set('params', '('.$from.'|'.$subject.'|'.$message.')');
}
}
send_email.ctp
<?php echo $result;?>
<br>
<?php echo $params;?>
I'm getting "Your email has been sent.", the $params look as I expect, and I am not seeing any errors... but I'm not getting the email. Any idea why this might happen?
Before this you need to define Email configuration in email.php under Config folder
Here we have gmail configuration for example
class EmailConfig {
public $gmail = array(
'host' => 'ssl://smtp.gmail.com',
'port' => 465,
'username' => 'username#gmail.com',
'password' => '*****',
'transport' => 'Smtp'
);
}
then you can use this setting in controller like
$Email= new CakeEmail('gmail');
Inshort you have to configure SMTP according to requirement. I hope this will be handy for you. Thanks
Why does setReplyTo($reply_to_mail) send email to $reply_to_mail? Shouldn't it just add email adress to reply-to field in the email message?
Currenyly if sending mail from website form and filling reply-to field, message sends to reply-to email and to our admin email.
Why does it duplicates email? Should send only to our admin email.
class Helper_Mail extends Zend_Controller_Action_Helper_Abstract
{
public function direct($email,$from,$message,$title,$replyto='')
{
$this->sendmail($email,$from,$message,$title,$replyto);
}
private function sendMail($email,$from,$message,$title,$replyto)
{
/* Configuring SMTP settings */
$config = array(
'auth' => 'login',
'ssl' => 'tls',
'username' => 'adminmail#gmail.com',
'password' => 'password',
'port' => 587);
$smtpHost = new Zend_Mail_Transport_Smtp('smtp.gmail.com',$config);
Zend_Mail::setDefaultTransport($smtpHost);
$mail = new Zend_Mail('UTF-8');
$mail->setBodyHtml($message);
$mail->setFrom('adminmail#gmail.com', $from);
$mail->addTo($email);
$mail->setSubject($title);
if(!empty($replyto))
{
$mail->setReplyTo($replyto);
}
try
{
$mail->send();
}
catch(Zend_Mail_Exception $e)
{
echo $e->getMessage();
}
}
}
You can use Zend_Mail::setReplyTo() if you are using a version of Zend > 1.8
If not (<= 1.8) you should use Zend_Mail::addHeader('Reply-To', 'replymail#example.com')
It was a bug, fixed in new versions. ;)