简体   繁体   中英

Send a mail to formatted email address using Google Apps Engine and PHP

require_once 'google/appengine/api/mail/Message.php';

use google\appengine\api\mail\Message;

// ...

$message_body = "...";

$mail_options = [
    "sender" => "admin@example.com",
    "to" => "user@example.com",
    "subject" => "Your example.com account has been activated.",
    "textBody" => $message_body
];

try {
    $message = new Message($mail_options);
    $message->send();
} catch (InvalidArgumentException $e) {
    // ...
}

I have used the above code for sending email from Google Apps Engine(for PHP Hosting)and its is working successfully.

But i need a mail send to formatted email address like Contact Name <user@example.com>

I have try with the following code but it is not working and i got exception 'InvalidArgumentException' with message 'Invalid 'to' recipient: <user@example.com>'

$mail_options = [
    "sender" => "admin@example.com",
    "to" => "Contact Name &lt;user@example.com&gt;",
    "subject" => "Your example.com account has been activated.",
    "textBody" => $message_body
];

and

$mail_options = [
    "sender" => "admin@example.com",
    "to" => "Contact Name <user@example.com>",
    "subject" => "Your example.com account has been activated.",
    "textBody" => $message_body
];

Is it possible?

The error you have there ( Invalid sender address ) is relating to the sender of the email ( admin@example.com in your example code), rather than the to address. The Mail service documentation includes the following security restriction on sender addresses :

For security purposes, the sender address of a message must be the email address of an administrator for the application or any valid email receiving address for the app (see Receiving Mail). The sender can also be the Google Account email address of the current user who is signed in, if the user's account is a Gmail account or is on a domain managed by Google Apps.

You can manage your app's administrators in the App Engine Console.

If you take a look at that first, then your examples may work (it's certainly possible with the Java API, but I'm not familiar with the PHP version).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM