I ve tried everything, i don't know how to fix this, so, i am using this Swift Mailer library to send a confirmation email. So here is the code from my index.php
if($confirm){
//include the swift class
include_once 'inc/php/swift/swift_required.php';
//put info into an array to send to the function
$info = array(
'username' => $username,
'email' => $email,
'key' => $key);
//send the email
if(send_email($info)){
//email sent
$action['result'] = 'success';
array_push($text,'Thanks for signing up. Please check your email for confirmation!');
}else{
$action['result'] = 'error';
array_push($text,'Could not send confirm email');
}
}
And my send_email function is in another php file functions.php
//send the welcome letter
function send_email($info){
//format each email
$body = format_email($info,'html');
$body_plain_txt = format_email($info,'txt');
//setup the mailer
$transport = Swift_MailTransport::newInstance(smtp.gmail.com,465,ssl)
->setUsername('my email here')
->setPassword('my password');
$mailer = Swift_Mailer::newInstance($transport);
$message = Swift_Message::newInstance();
$message ->setSubject('Welcome to Site Name');
$message ->setFrom(array('somedomain.com' => 'somethinghere'));
$message ->setTo(array($info['email'] => $info['username']));
$message ->setBody($body_plain_txt);
$message ->addPart($body, 'text/html');
$result = $mailer->send($message);
return $result;
}
The error that i am getting is
Fatal error: Call to undefined method Swift_MailTransport::setUsername() in /srv/disk7/something/www/something/signup/inc/php/functions.php on line 31
How can i fix this? I am a beginner in php.
I'm guessing Swift_MailTransport::newInstance(smtp.gmail.com,465,ssl) fails to create the expected instance.
BTW, shouldn't it be Swift_MailTransport::newInstance('smtp.gmail.com',465,'ssl') (i.e. smtp.gmail.com and ssl in quotes)?
Swift_MailTransport, from the documentation...
...sends messages by delegating to PHP's internal mail() function.
In my experience -- and others' -- the mail() function is not particularly predictable, or helpful.
Another thing it does not have is a setUsername or setPassword method.
I'd say you want to use Swift_SmtpTransport
One more thing; as pointed out by others, your string arguments should be quoted, ie
$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, 'ssl');
Looks like you haven't included the Swift Mail class.
Related
I am using the below code and I got emails but from email address not coming from setFrom() function. Can anyone help me to set desire from email address in SMTP sendmail() function?
include_once 'Mailer.php';
$body = 'TEST, Testing of email.';
$subject = 'Email Demo';
$email = 'test1#gmail.com';
$fromEmail = 'test2#gmail.com';
$objMail = new Mailer();
$objMail->mail->AddReplyTo('test3#gmail.com', 'Test 3');
$objMail->mail->SetFrom('testfrom1#gmail.com', 'Test From 1');
$objMail->mail->AddAddress('test4#gmail.com', 'Test 4');
$objMail->mail->IsHTML(true);
$objMail->sendmail($email, 'Test', $subject, $body);
This has been answered many times before. Gmail does not let you set arbitrary from addresses, though you can add a limited number of preconfigured aliases in your account.
So I am trying to setup swift mailer to work with the Mandrill API, but it keeps throwing the following error:
Failures:Array ( [0] => example#email.com ) (I have a proper email in this place in my code)
My code is as follows:
$subject = 'Hello from Mandrill, PHP!';
// approved domains only!
$from = array('example2#email.com' =>'Your Name');
$to = array(
'example#email.com' => 'Recipient1 Name'
);
$text = "Mandrill speaks plaintext";
$html = "Mandrill speaks HTML";
$transport = Swift_SmtpTransport::newInstance('smtp.mandrillapp.com', 587);
$transport->setUsername(getenv('my#mandrillemail.com'));
$transport->setPassword(getenv('mymandrillpass'));
$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');
// Pass a variable name to the send() method
if (!$swift->send($message, $failures))
{
echo "Failures:";
print_r($failures);
}
What is going wrong?
Try using SSL and port 465.
$xport = Swift_SmtpTransport::newInstance('smtp.mandrillapp.com', 465, 'ssl');
$xport->setUsername('mandrilluser')
->setPassword('mandrillpass');
See if this works for you.
My problem ended up being that I had to change from using the actual Mandrill account password to the API in the ->setPassword() variable.
Since this is purely an error with your credentials, cross check if the password you are using is actually the token generated by Mandrill or not .
Password doesn't mean the 'password' of your account !!
I am doing some testing prior to working on some production code and need to figure out how to do an auto e-mail.
The below script runs fine and the result of the send method returns 1, as if it sends. However, nothing ever makes it to the recipient.
require_once '/home/absolut2/lib/swift_required.php';
//Create the Transport
$transport = Swift_SmtpTransport::newInstance('mail.mysite.com', 25)
->setUsername('myuser')
->setPassword('password')
;
/*
You could alternatively use a different transport such as Sendmail or Mail:
//Sendmail
$transport = Swift_SendmailTransport::newInstance('/usr/sbin/sendmail -bs');
//Mail
$transport = Swift_MailTransport::newInstance();
*/
//Create the Mailer using your created Transport
$mailer = Swift_Mailer::newInstance($transport);
//Create a message
$message = Swift_Message::newInstance('Subject')
->setFrom(array('rp#mysite.com' => 'RP'))
->setTo(array('rp#gmail.com'))
->setBody('Here is the message itself');
//Send the message
$result = $mailer->send($message);
echo "Messages sent: " . $result;
The code itself seems fine, so I guess something else is wrong. Either check the spam queue of the recipient or maybe just the address was rejected.
Find out if addresses were rejected.
You can do that with this code:
if (!$mailer->send($message, $failures)) {
echo "Failures:";
print_r($failures);
}
I am trying this code (from http://swiftmailer.org/docs/sending.html):
require_once 'lib/swift_required.php';
//Create the Transport
$transport = Swift_SmtpTransport::newInstance('localhost', 25);
//Create the Mailer using your created Transport
$mailer = Swift_Mailer::newInstance($transport);
//Create a message
$message = Swift_Message::newInstance('Wonderful Subject')
->setFrom(array('john#doe.com' => 'John Doe'))
->setBody('Here is the message itself')
;
//Send the message
$failedRecipients = array();
$numSent = 0;
$to = array('receiver#domain.org', 'other#baddomain.org' => 'A name');
foreach ($to as $address => $name)
{
$message->setTo(array($address => $name));
$numSent += $this->send($message, $failedRecipients);
}
printf("Sent %d messages\n", $numSent);
The problem is that if I sent an email to a bad domain swiftmailer recognize it as a correct sent email and $failedRecipients is empty. In my mail box I have returned a failure notice.
Why does Swiftmailer not recognize this mail as as a failure, and does not populate $failedRecipients Array?
Swiftmailer only takes care to hand the email over to the mail-server. Everything else is not related to Swiftmailer.
What you get is a bounce message, and you need to process them on your own, because the email itself actually was a syntactically mail address that was not rejected by the first server.
That btw is the case for any other mailing library and even the php mail function. You might be looking for a bounce processing application or code.
Related: Bounce Email handling with PHP?
I working on a form whereby when the user enter in their email account and click on send, an email will be sent to their email account.
I have everything worked out. Just that it doesnt send the email to my account. Anyone have any ideas? Is there a configuration that I left out or something?
This is the sample from my controller:
public function retrieveemailAction(){
$users = new Users();
$email = $_POST['email'];
$view = Zend_Registry::get('view');
if($users->checkEmail($_POST['email'])) {
// The Subject
$subject = "Email Test";
// The message
$message = "this is a test";
// Send email
// Returns TRUE if the mail was successfully accepted for delivery, FALSE otherwise.
// Use if command to display email message status
if(mail($email, $subject, $message, $headers)) {
$view->operation = 'true';
}
} else {
$view->operation = 'false';
}
$view->render('retrieve.tpl');
}
I recommend you use Zend_Mail instead of mail(). It handles a lot of stuff automatically and just works great.
Do you have a SMTP server? Trying to send mail without your own SMTP server could be causing the mail to not be sent.
This is what I use for sending mails using Zend_Mail and Gmail:
In Bootstrap.php, I configure a default mail transport:
protected function _initMail()
{
try {
$config = array(
'auth' => 'login',
'username' => 'username#gmail.com',
'password' => 'password',
'ssl' => 'tls',
'port' => 587
);
$mailTransport = new Zend_Mail_Transport_Smtp('smtp.gmail.com', $config);
Zend_Mail::setDefaultTransport($mailTransport);
} catch (Zend_Exception $e){
//Do something with exception
}
}
Then to send an email I use the following code:
//Prepare email
$mail = new Zend_Mail();
$mail->addTo($email);
$mail->setSubject($subject);
$mail->setBody($message);
$mail->setFrom('username#gmail.com', 'User Name');
//Send it!
$sent = true;
try {
$mail->send();
} catch (Exception $e){
$sent = false;
}
//Do stuff (display error message, log it, redirect user, etc)
if($sent){
//Mail was sent successfully.
} else {
//Mail failed to send.
}
First of all i would switch to using Zend_Mail. Second i would use a real mail account on an smtp server somewhere and send from that. A lot of times there are restrictions on sending from the server itself, but using an actual mail server usually fixes this.
There's a very useful screencast covering Zend_Mail available on ZendCasts
http://www.zendcasts.com/introduction-to-zend_mail/2010/02/
In line $mail->setBody($message);, change it to $mail->setBodyText($message);