Showing Invalid Email in CakePHP - email

Hi please check my CakePHP code.
Code
//$from="my_email#domain.com";
$from="CakePHP Email<my_email#domain.com>";
$to="someone_email#domain.com";
$sendMessage="This is CakePHP test message";
$sendMessage =$message;
$Email = new CakeEmail();
$Email->template('default')
->emailFormat('html')
->to($to)
->from($from)
->bcc('learnphpchinu#gmail.com')
->subject('CakePHP test subject')
->send($sendMessage);
return true;
This email is not working properly. I am getting error message "Invalid email address CakePHP Email<my_email#domain.com>".
If i have used my_email#domain.com instead of CakePHP Email<my_email#domain.com> the email working fine but the from email is showing my_email in mail box.
I want to show CakePHP Email in my from email address. How could i fix that issue. Help me?

As it takes array as an argument for from name,change your $from to
$from = array('my_email#domain.com' => 'CakePHP Email');
Reference.

Try this >
if (filter_var($d['email_to'], FILTER_VALIDATE_EMAIL)) {
$this->Email->sendAs = 'html';
$this->Email->from = $d['email_from'];
$this->Email->to = $d['email_to'];
$this->Email->subject = $d['subject'];
$this->Email->send($d['message']);
}

Related

preserve recipients in the sendgrid API

I'm sending an email to multiple recipients using the sendgrid API !
<?php
$email = new SendGrid\Email();
$sendgrid = new SendGrid('API_KEY');
$email
->addTo(array('first#mail.com','second#mail.com'..))
->setFrom('my#mail.com')
->setSubject('Subject goes here')
->setText('Hello World!')
->setHtml('<strong>Hello World!</strong>')
;
$sendgrid->send($email);
Now, I wanna know if there is any way to hide the second email in the "To" header for the first email?
Use the SMTP API for mail-merge type functionality.
Using sendgrid-php, that looks like:
$email = new SendGrid\Email();
$email
->addSmtpapiTo('foo#bar.com')
->addSmtpapiTo('another#another.com', 'Mike Bar')
;
$sendgrid->send($email);
Or for an array:
$email = new SendGrid\Email();
$emails = array("foo#bar.com", "Brian Bar <bar#example.com>", "other#example.com");
$email->setSmtpapiTos($emails);
$sendgrid->send($email);

Zend Framework Send Mail either Text or Html

Hey I'm using something like this to send either a text mail or and html but it send both in the html version . What i'm doing wrong ? How can i do to send only text in case the email can't receive html ?
$htmlPart = new MimePart($htmlBody);
$htmlPart->type = "text/html";
$textPart = new MimePart($textBody);
$textPart->type = "text/plain";
$body = new MimeMessage();
$body->setParts(array($textPart, $htmlPart));
$message = new Message();
$message->addFrom($from, "My Corp.")
->addTo($email)
->setSubject(mb_convert_encoding($subject,"UTF-8"));
$message->setBody($body);
$message->setEncoding("UTF-8");
$options = /* … */
$transport = new SmtpTransport();
$transport->setOptions($options);
$transport->send($message);
Add:
$message->getHeaders()->get('content-type')->setType('multipart/alternative');
after your setBody() call. Everything else looks okay. If it's still not working after that, please add some more information to your question about specifically what you're seeing in the email client.

Bulk email in cakephp using sendgrid

I am using cakePHP to send smtp email with sendgrid. I would like to be able to execute a single send() to multiple recipients and allow sendgrid to use the vars to replace -name- with the names from a array.
In my cakephp controller method I am testing:
protected function fwtEmail() {
$config = 'sendGrid';
$subject = "test";
$Email = new CakeEmail('sendGrid');
$names = array('user#domain.com'=>'John','user2#domain' =>'Paul');
$Email->To($names);
$vars = array('Paul', 'John');
$Email->viewVars(array('name' => $vars));
$Email->from( array('admin#testdomain.com' => 'Jim') );
$Email->subject($subject);
$template = 'bulk';
$Email->template($template, 'default');
$Email->sendAs = 'both';
return $Email->send();
}
Here is a post on the SendGrid blog that does what you describe.
edit: apologies, this appears not to work in the latest cakephp. I'm trying to find a solution for you.

How to send email message to two email id Address in zend Mail

Hi I am new in zend framework.
I have 2 email id's and i want to send message to 2 mail ids in zend mail
this is my code
$to1='abcd#gmail.com'
$to2='xyz#gmail.com'
$mailObj = new Zend_Mail()
$mailObj->setSubject($subject)
mailObj->setBodyHtml($message)
$mailObj->addTo($to1, $name='test')
$mailObj->setFrom($from, $name = null)
$mailObj->send()
You have to add Recipients using array like below
$recipients = array('abcd#gmail.com','xyz#gmail.com')
$message = new Zend_Mail();
$message->setFrom('fake#email.com', 'My Fake Mailing List')
->setSubject($subject)
->setBodyText($body);
foreach($recipients as $each_recipient){
$message->addTo($each_recipient);
}
$message->send();
for detail documentation you can check Zend_Mail - adding recipients
let me know if i can help you more.
You can define receiver as an array, for example:
$mailObj->addTo(array('address1#example.com', 'address2#example.com'), 'test');

How do I AddAddress automatically in PHPmailer from form php info?

This has got to be a simple one, but I've been searching all over and nothing seems relevant to my problem: or i'm not able to interpret advice for others adequately.
I'm getting the error "You must provide at least one recipient email address. Message could not be sent."
If I put a specific email address in 'AddAddress' field the message is sent. I want to have an email sent to the user who has submitted their form as an automatic email response thanking user for registering... Thus the ($_POST['email']) in the 'AddAddress' field. This works in Mail() php function. Why not PHP MAiler? What am I missing?
Here is my code with sensitive bits taken out (Relevant bit - I think - in bold):
<?php
$host="localhost"; // Host name
$username=""; // Mysql username
$password=""; // Mysql password
$db_name=""; // Database name
$tbl_name=""; // Table name
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// Get values from form
$email=$_POST['email'];
// Insert data into mysql
$sql="INSERT INTO $tbl_name(email,) VALUES('$email',)";
$result=mysql_query($sql);
// if successfully insert data into database, displays message "Successful".
if($result){
}
else {
echo "ERROR";
}
?>
<?php
require("class.phpmailer.php");
$mail = new PHPMailer();
$mail->IsSMTP(); // set mailer to use SMTP
$mail->Host = "localhost"; // specify main and backup server
$mail->SMTPAuth = false; // turn on SMTP authentication
$mail->Username = ""; // SMTP username
$mail->Password = ""; // SMTP password
$mail->From = "";
$mail->FromName = "";
**$mail->AddAddress = ($_POST['email']);**
$mail->WordWrap = 50; // set word wrap to 50 characters
$mail->IsHTML(true); // set email format to HTML
$mail->Subject = "";
$mail->Body = "";
$mail->AltBody = "";
if(!$mail->Send())
{
echo "Message could not be sent. <p>";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}
echo "Message has been sent";
?>
<?php
// close connection
mysql_close();
?>
Many thanks in advance for your advice. Hopefully this will help others too...
Ed
Simple one... just change this line
$mail->AddAddress = ($_POST['email']);
to this:
$mail->AddAddress($_POST['email']);
as it is a method, as opposed to a variable, on the class! :)
there are some good examples you can use on the PHPMailer GitHub